gpt4 book ai didi

python - Python代码在一个编辑器中工作,而不在另一个编辑器中工作

转载 作者:行者123 更新时间:2023-12-03 08:34:09 25 4
gpt4 key购买 nike

我是学习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().()) #


引用文献
  • 有关更多详细信息,请参见Discussion Board,在这里您可以找到许多经过解释的公认解决方案,其中包含各种languages,包括低复杂度算法和渐近runtime/memory分析12
  • 关于python - Python代码在一个编辑器中工作,而不在另一个编辑器中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63622411/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com