smj007

Untitled

Jun 21st, 2022
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. class Solution:
  2.    
  3.     def getPermutation(self, n: int, k: int) -> str:
  4.        
  5.         self.count = 0
  6.        
  7.         def render_permutations(nums):
  8.            
  9.             if not len(nums):
  10.                 self.count += 1
  11.                 if self.count == k:
  12.                     self.found_ans = True
  13.                 return ""
  14.            
  15.             for index in range(len(nums)):
  16.                 res = str(nums[index]) + render_permutations(nums[:index] + nums[index+1:])
  17.                 if self.found_ans:
  18.                     return res
  19.                    
  20.                 res = ""
  21.                    
  22.             return res
  23.        
  24.         self.found_ans = False
  25.         nums = [i for i in range(1, n+1)]
  26.        
  27.         res = render_permutations(nums)
  28.        
  29.         return res
Advertisement
Add Comment
Please, Sign In to add comment