鱼C论坛

 找回密码
 立即注册
查看: 2569|回复: 7

两个大整数相加,lintcode(655)

[复制链接]
发表于 2017-6-22 20:56:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这是lintcode上的一道题目,两个大整数相加,看题目说明,应该是两个用字符串表示的正整数,不能直接转换为整型,不能用内建函数,不知道怎么做了?求大神解答谢谢
http://www.lintcode.com/zh-cn/problem/big-integer-addition/
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.

注意事项

The length of both num1 and num2 is < 5100.
Both num1 and num2 contains only digits 0-9.
Both num1 and num2 does not contain any leading zero.
You must not use any built-in BigInteger library or convert the inputs to integer directly.

class Solution:
    # @param {string} num1 a non-negative integers
    # @param {string} num2 a non-negative integers
    # @return {string} return sum of num1 and num2
    def addStrings(self, num1, num2):
        # Write your code here
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-6-22 21:16:47 | 显示全部楼层
模拟竖式计算
注意进位
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-22 22:14:02 | 显示全部楼层
class Solution:
    # @param {string} num1 a non-negative integers
    # @param {string} num2 a non-negative integers
    # @return {string} return sum of num1 and num2
    def addStrings(self, num1, num2):
        # Write your code here
        carry = 0
        maxlen = max(len(num1),len(num2))
        cov_int = lambda s:(b-48 for b in reversed(bytes(s.rjust(maxlen,'0'),'ascii')))
        rslt = []
        for a,b in zip(cov_int(num1),cov_int(num2)):
            carry,n = divmod((a+b+carry),10)
            rslt.append(n)
        if carry>0:
            rslt.append(carry)

        return ''.join(str(i)for i in reversed(rslt))
        

print(Solution().addStrings('999','456'))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-6-22 22:35:27 | 显示全部楼层

Traceback (most recent call last): File "Main.py", line 14, in result = solution.addStrings(num1, num2) File "Solution.py", line 11, in addStrings for a,b in zip(cov_int(num1),cov_int(num2)): File "Solution.py", line 9, in cov_int = lambda s:(b-48 for b in reversed(bytes(s.rjust(maxlen,'0'),'ascii'))) TypeError: str() takes at most 1 argument (2 given) EXITCODE=1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-22 22:48:13 | 显示全部楼层
nexthunghung 发表于 2017-6-22 22:35
Traceback (most recent call last): File "Main.py", line 14, in result = solution.addStrings(num1,  ...

我这里运行正常
而且那一句没用到 str() ,怎么会提示 str() 出错呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-22 23:18:57 | 显示全部楼层
我写的是python3 的代码

你应该是用python2 ,所以有错误
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-6-23 10:52:55 | 显示全部楼层
SixPy 发表于 2017-6-22 23:18
我写的是python3 的代码

你应该是用python2 ,所以有错误

我在电脑上是可以运行,可是copy放到lintcode上就通过不了了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-23 11:02:55 | 显示全部楼层
nexthunghung 发表于 2017-6-23 10:52
我在电脑上是可以运行,可是copy放到lintcode上就通过不了了

明白原理就行
可能lintcode的后台是py2吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-2-21 04:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表