当前位置: 代码迷 >> 综合 >> Leetcode 415. Add Strings (python+cpp)
  详细解决方案

Leetcode 415. Add Strings (python+cpp)

热度:16   发布时间:2023-11-26 06:57:37.0

Leetcode 415. Add Strings

  • 题目
  • 解法:
  • follow up 1:两个string相减
  • follow up2:两个string相加,但是有可能有负号

题目

在这里插入图片描述

解法:

这边默认肯定是不能用强制类型转换的
python:

class Solution:def addStrings(self, num1: str, num2: str) -> str:ans = ''pt1 = len(num1)-1pt2 = len(num2)-1carry = 0while pt1>=0 and pt2>=0:v1 = ord(num1[pt1]) - ord('0')v2 = ord(num2[pt2]) - ord('0')v = (v1+v2+carry)%10carry = (v1+v2+carry)//10ans = str(v)+anspt1 -= 1pt2 -= 1while pt1>=0:v1 = ord(num1[pt1]) - ord('0')v = (v1+carry)%10carry = (v1+carry)//10ans = str(v)+anspt1 -= 1while pt2>=0:v2 = ord(num2[pt2]) - ord('0')v = (v2+carry)%10carry = (v2+carry)//10ans = str(v)+anspt2 -= 1if carry:ans = str(carry)+ansreturn ans

C++:
C++代码注意两个点,首先两个string也是可以用加号直接连接的。其次某个数字转成char时,要先+'0’才是正确的。因为char(x)中,x应该对应这个数字的ascii码值才可以

class Solution {
    
public:string addStrings(string num1, string num2) {
    string res = "";int carry = 0;int pt1 = num1.size()-1;int pt2 = num2.size()-1;int v1,v2,v;while (pt1>=0 || pt2>=0){
    v1 = pt1>=0 ? num1[pt1]-'0' :0;v2 = pt2>=0 ? num2[pt2]-'0':0;v = (v1+v2+carry)%10;carry = (v1+v2+carry)/10;res = char(v+'0')+res;pt1--;pt2--;}if (carry) res = char(carry+'0')+res;return res;}
};

follow up 1:两个string相减

对于两个string相减,肯定要分类讨论成四种情况:正-负,负-正,正-正,负-负,我们一一来讨论

  • 正-正:如果两个字符串长度不相同,那么长的一定比短的大。用长得去减短的,需要很小心地处理借位,代码如下:
num1 = '105'
num2 = '99'pt1 = len(num1)-1
pt2 = len(num2)-1
carry = 0
ans = ''
while pt1>=0 and pt2>=0:v = ord(num1[pt1]) - ord(num2[pt2]) + carryif v>=0:remain = v%10carry = 0else:remain = v+10carry = -1ans = str(remain)+anspt1 -= 1pt2 -= 1while pt1>=0:v = ord(num1[pt1]) - ord('0') + carryif v>=0:remain = v%10carry = 0else:remain = v+10carry = -1ans = str(remain) + anspt1 -= 1
print(ans)

这边前面还要加判断长度和符号的操作,后面结果数出来还要去掉前面的0

  • 负-正,正-负,对于这两种情况,都可以转化为加法来做,只需要提前判断符号是正是负,最后加上即可
  • 负-负,需要先判断结果是正是负,然后两个数字做上面的减法,最后加上符号
    所以这四种情况都可以归结成两个正数相加或者相减的操作

follow up2:两个string相加,但是有可能有负号

也是分情况进行讨论,归结成两种基本情况

完整代码:

def add_string(num1,num2):p1 = len(num1)-1p2 = len(num2)-1carry = 0ans = ''while p1>=0 or p2>=0:v1 = ord(num1[p1])-ord('0') if p1>=0 else 0v2 = ord(num2[p2])-ord('0') if p2>=0 else 0v = (v1+v2+carry)%10carry = (v1+v2+carry)//10ans = str(v)+ansp1 -= 1p2 -= 1if carry:ans = str(carry)+ansreturn ansdef substring(num1,num2):p1 = len(num1) - 1p2 = len(num2) - 1carry = 0ans = ''while p1>=0 or p2>=0:v1 = ord(num1[p1]) - ord('0') if p1 >= 0 else 0v2 = ord(num2[p2]) - ord('0') if p2 >= 0 else 0v = v1-v2-carryif v<0:v = v+10carry = 1else:carry = 0ans = str(v) + ansp1 -= 1p2 -= 1for i in range(len(ans)):if ans[i] != '0':return ans[i:]def compare(num1,num2):if len(num1)>len(num2):return 1if len(num1)<len(num2):return -1if len(num1)==0 or len(num2)==0:return 0if num1[0]>num2[0]:return 1if num1[0]<num2[0]:return -1return compare(num1[1:],num2[1:])def main(num1,num2):if num1[0] != '-' and num2[0] != '-':return add_string(num1,num2)if num1[0] == '-' and num2[0] == '-':return '-' + add_string(num1[1:],num2[1:])if num2[0] == '-':num1,num2 = num2,num1num1 = num1[1:]if compare(num1,num2)==0:return 0if compare(num1,num2)==1:return '-' + substring(num1,num2)if compare(num1,num2)==-1:return substring(num2,num1)print(main('0', '-9'))
print(main('10', '-9'))
print(main('29', '99'))
print(main('-10', '-9'))
print(main('-1000', '2'))
print(main('-1000', '1000'))