LeetCode--415--字符串相加
2024-08-28 18:36:04
问题描述:
给定两个字符串形式的非负整数 num1
和num2
,计算它们的和。
注意:
num1
和num2
的长度都小于 5100.num1
和num2
都只包含数字0-9
.num1
和num2
都不包含任何前导零。- 你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。
方法:
class Solution(object):
def addStrings(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
res = ""
c = 0
i , j = len(num1) -1,len(num2) - 1
while i >= 0 or j >= 0:
if i >= 0:
c += ord(num1[i]) - ord('')
i -= 1
if j >= 0:
c += ord(num2[j]) - ord('')
j -= 1
res = str(c % 10) + res
c //= 10
if c:
res = str(c) + res
return res
2018-10-01 09:04:24
最新文章
- weex scroller
- marquee 标签 文字滚动
- JAVA递归算法
- android获取string.xml的值(转)
- WebService 实现BS环境与BS环境传递参数,根据参数生成txt文档
- Windows 下启动Apache服务(转)
- printf 缓冲区问题
- C语言使用正则表达式
- phpadmin
- ASP.NET MVC 音乐商店 - 目录
- php将中文插入数据库出现乱码
- WPF依赖属性2
- 面试题中遇到的算法与js技巧
- Java中浮点数的精度问题 【转】
- async/await套路编程
- maven dependencies
- Python开发【笔记】:获取目录下所有文件
- commit 流程
- IAR EWARM : Debugging with CMSIS-DAP
- Redis的Docker镜像
热门文章
- java中读取配置文件的方法
- Python3 tkinter基础 Listbox delete 删除单个、所有元素
- (转)Redis &; EhCache
- Linux/shell: remove adjacent similar patterns
- 题解——loj6279 数列分块入门3 (分块)
- Optical Flow related Tutorials
- Docker、Kubenets使用前配置
- 测试与CMMI质量体系
- jQuery中的$(window).load()与$(document).ready()以及jquery $(document).ready() 与window.onload的区别
- repr() 和 str() 函数