Tag:Force buckle

  • Leetcode 1409. Query keyed permutation – Python

    Time:2021-7-27

    1409. Query keyed arrangement Three solutions Solution I violence class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: m = min(m, max(queries)) p = [i+1 for i in range(m)] ans = [] for x in queries: i = p.index(x) ans.append(i) x = p.pop(i) p.insert(0, x) return ans Solution two deque # from collections import […]

  • Full Permutation (Python 3)

    Time:2020-6-10

    Title Description: given a sequence without repeating numbers, return all possible permutations. Example:Input: [1,2,3]Output:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Solutions: The code is as follows: class Solution: def permute(self, nums: List[int]) -> List[List[int]]: self.length = len(nums) self.re = [] self.sub_permute(nums,[]) return self.re def sub_permute(self,nums,temp): if len(temp) == self.length: self.re.append(temp) else: for i in nums: self.sub_permute(nums[:nums.index(i)]+nums[nums.index(i)+1:],temp+[i]) Time and space complexity: Source: […]