python - Python 代码在一个编辑器中工作,而不是在另一个编辑器中工作
问题描述
我是学习 Python 的新手。在这段代码中,我试图添加 2 个数字字符串。在我的编辑器 PyCharm 的第 13 行,我写了下面的语句。它没有用。我试着写总结: int = int(x) +int(y) + int(left_over)
它奏效了。但是,在 leetcode 上,它不起作用。它说第 14 行有一个无效的语法。在 leetcode 中,我也是用 Python 编写的,而不是 Python3。
class Solution(object):
def addStrings(num1, num2):
last = ""
left_over = 0
i = len(num1) - 1
j = len(num2) - 1
while (i >= 0) or (j >= 0):
x = 0
y = 0
if i >= 0:
x = num1[i]
if j >= 0:
y = num2[j]
**sumation = x + y + left_over
left_over = sumation // 10
last = str(sumation % 10) + last
i -= 1
j -= 1
if left_over > 0:
last = str(left_over) + last
return last
解决方案
这一行有一个小错误
**sumation = x + y + left_over
我猜你想做类似的事情:
summation = int(x) + int(y) + left_over
此外,您可能忘记了传递self
。addStrings()
def addStrings(self, num1, num2):
刚刚测试了你的解决方案,通过了 LeetCode:
class Solution(object):
def addStrings(self, num1, num2):
last = ""
left_over = 0
i = len(num1) - 1
j = len(num2) - 1
while (i >= 0) or (j >= 0):
x = 0
y = 0
if i >= 0:
x = num1[i]
if j >= 0:
y = num2[j]
summation = int(x) + int(y) + left_over
left_over = summation // 10
last = str(summation % 10) + last
i -= 1
j -= 1
if left_over > 0:
last = str(left_over) + last
return last
print(Solution().addStrings("100", "500"))
印刷,
600
这也可以正常工作:
class Solution:
def addStrings(self, num1, num2):
length1, length2 = len(num1), len(num2)
reversed_num1, reversed_num2 = num1[::-1], num2[::-1]
max_length = max(length1, length2)
added_num = ''
carry = index = 0
while index < max_length or carry:
digit1 = int(reversed_num1[index]) if index < length1 else 0
digit2 = int(reversed_num2[index]) if index < length2 else 0
added_digit = (digit1 + digit2 + carry) % 10
carry = (digit1 + digit2 + carry) // 10
added_num += str(added_digit)
index += 1
return added_num[::-1]
这也是我使用的 Python 模板:
from typing import List
import collections
import itertools
import functools
import math
import string
import random
import bisect
import re
import operator
import heapq
import queue
from queue import PriorityQueue
from itertools import combinations, permutations
from functools import lru_cache
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from collections import Counter
class Solution(object):
def addStrings(self, num1, num2):
last = ""
left_over = 0
i = len(num1) - 1
j = len(num2) - 1
while (i >= 0) or (j >= 0):
x = 0
y = 0
if i >= 0:
x = num1[i]
if j >= 0:
y = num2[j]
summation = int(x) + int(y) + left_over
left_over = summation // 10
last = str(summation % 10) + last
i -= 1
j -= 1
if left_over > 0:
last = str(left_over) + last
return last
print(Solution().addStrings("100", "500")) # 600
# print(Solution().()) #
# print(Solution().()) #
# print(Solution().()) #
# print(Solution().()) #
参考
推荐阅读
- aws-glue - AWS Glue 自定义分类器
- visual-studio - 在 Visual Studio 2019 中重新打开关闭的选项卡(撤消关闭)?
- flutter - 滚动未来的 ListView
- flutter - 未处理的异常:RangeError(索引):无效值:有效值范围为空:0
- forms - Symfony 完全忽略了重定向和表单的处理
- facebook - Chatfuel 或 Manychat 等聊天机器人平台如何在后端创建 Facebook 应用程序来服务聊天机器人?
- python - 如何以表格格式编写日志记录我希望它们在 python 中按列显示而不使用 pandas
- scala - Scala 泛型类型扩展了 Comparable 接口
- swiftui - 用于在触摸上执行操作的 SwiftUI 方法
- angular - 角,访问,写管