我复制粘贴并对该方法使用相同的输入,我在我的机器上得到了正确的结果,但 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))
这是因为你在leetcode上选择了Python而不是Python3作为语言。您当前正在版本 2.7.18 上运行代码,该版本的行为与最新版本不同。