为什么LeetCode和PyCharm、Jupyter Notebook的结果不同
我复制粘贴了代码,并且用相同的输入来测试这个方法。在我的电脑上能得到正确的结果,但在LeetCode上却得到了不同的结果。这是LeetCode上关于排列序列的代码。
class Solution(object):
def getPermutation(self, n, k):
list_ = []
my_num = ''
for i in range(n):
list_.append(str(i + 1))
fact = 1
n_f = n
k_f = k
for i in range(n_f):
fact *= (i+1)
diveded = int(fact/n_f)
if n_f == n:
no = (k/diveded)
if int(no) == no:
no -= 1
no = int(no)
else:
no = int(no)
my_num += list_[no]
list_.remove(list_[no])
n_f -= 1
while len(my_num) < n:
fact = 1
for i in range(n_f):
fact *= (i + 1)
no_ = int((k_f-1)%diveded)
no = int(no_/fact)
if int(no) == no and no_ > 0:
no -= 1
no = int(no)
else:
no = int(no)
used = 0
for i in range(len(my_num)):
if int(my_num[i]) < int(list_[no]) and no > 0:
used += 1
no -= used
diveded = int(fact / n_f)
while list_[no] in my_num:
no += 1
else:
my_num += list_[no]
list_.remove(list_[no])
n_f -= 1
else:
return my_num
solu = Solution()
print(solu.getPermutation(3,3))
1 个回答
1
这是因为你在leetcode上选择了Python而不是Python3。你现在运行的代码是在2.7.18这个版本上,而这个版本的表现和更新的版本是不同的。