LeetCode-60-Permutation Sequence 暴力递归
2017-09-20 15:37
423 查看
class Solution(object):
ans=""
def getPermutation(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
nums=range(1,n+1)
ans=[]
self.solve(nums,k)
return self.ans
def solve(self,nums,k):
if k<=0:
for i in range(len(nums)-1,-1,-1):
self.ans+=chr(nums[i]+ord('0'))
return
Len=len(nums)
total=self.nMulti(Len)
one=total/Len
p=(k-1)/one
print one,p
self.ans+=chr(nums[p]+ord('0'))
print self.ans
k%=one
del nums[p]
self.solve(nums,k)
def nMulti(self,n):
if n==1:return 1
return n*self.nMulti(n-1)
ans=""
def getPermutation(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
nums=range(1,n+1)
ans=[]
self.solve(nums,k)
return self.ans
def solve(self,nums,k):
if k<=0:
for i in range(len(nums)-1,-1,-1):
self.ans+=chr(nums[i]+ord('0'))
return
Len=len(nums)
total=self.nMulti(Len)
one=total/Len
p=(k-1)/one
print one,p
self.ans+=chr(nums[p]+ord('0'))
print self.ans
k%=one
del nums[p]
self.solve(nums,k)
def nMulti(self,n):
if n==1:return 1
return n*self.nMulti(n-1)
相关文章推荐
- leetcode——60——Permutation Sequence
- Leetcode 60: Permutation Sequence
- [Leetcode]#60 Permutation Sequence
- [leetcode-60]Permutation Sequence(C)
- LeetCode(60) Permutation Sequence
- LeetCode 60 - Permutation Sequence
- LeetCode(60) Permutation Sequence
- LeetCode#60 Permutation Sequence
- LeetCode(60): Permutation Sequence (C++)
- [leetcode 60] Permutation Sequence
- [leetcode-60]Permutation Sequence(C)
- leetcode-60-Permutation Sequence
- LeetCode(60) Permutation Sequence
- LeetCode 60 Permutation Sequence
- [LeetCode#60]Permutation Sequence
- LeetCode Permutation Sequence
- leetcode — permutation-sequence
- Leetcod-60_Permutation Sequence-全排列秒解-【C++】
- Permutation Sequence--LeetCode
- leetcode:Permutation Sequence (顺序排列第k个序列) 【面试算法题】