自己写的:
class Solution: def addStrings(self, num1: str, num2: str) -> str: # 将两个字符串转换为整数 num1_int = self.strToInt(num1) num2_int = self.strToInt(num2) # 将两个整数相加,并将结果转换为字符串 res = self.IntToStr(num1_int + num2_int) return res # 将字符串转换为整数 def strToInt(self, num_str): num_li = [] # 遍历字符串,将每个字符转换为整数并存储在列表中 for i in num_str: num_li.append(int(i)) n = len(num_li) res = 0 # 将整数列表转换为整数 for i in range(n): res += num_li[i] * 10 ** (n - i - 1) return res # 将整数转换为字符串 def IntToStr(self, num_int): str_save = '' flag = True # 循环直到整数为0 while flag: # 将整数的每一位取出并添加到字符串中 str_save = str(num_int % 10) + str_save num_int //= 10 # 检查是否还有剩余的数字 if num_int > 0: flag = True else: flag = False return str_save
标签:int,res,self,leedcode,整数,num,str From: https://www.cnblogs.com/yyyjw/p/18163548