leetcode day 29 * 491.递增子序列 * 46.全排列 * 47.全排列 II

文章目录

  • 一、leetcode 491.递增子序列 §§§§§
    • 1.题目链接:
    • 2.独立做题问题总结
    • 3.解法总结:
  • 二、leetcode 46.全排列 §§§§
    • 1.题目链接:
    • 2.独立做题问题总结
    • 3.解法总结:
  • 三、leetcode 47.全排列 II §§§
    • 1.题目链接:
    • 2.独立做题问题总结
    • 3.解法总结:


提示:以下是本篇文章正文内容,下面案例可供参考

一、leetcode 491.递增子序列 §§§§§

1.题目链接:

link

2.独立做题问题总结

运行时间过长

3.解法总结:

  1. 求子集后进行判断
		def check(store):                
            if len(store) < 2:
                return False
            # for i in range(len(store)):
            #     if i > 0 and store[i - 1] > store[i]:
            #         return False
            return True

        def backtracking(nums, startindex):

            if check(store):
                if store not in res:
                    res.append(store[:])
            for i in range(startindex, len(nums)):

                if i > startindex and nums[i] == nums[i - 1]:
                    continue
                if len(store) != 0 and nums[i] < store[-1]:
                    continue
                store.append(nums[i])
                backtracking(nums, i + 1)
                store.pop()
        res = []
        store = []
        backtracking(nums, 0)
        return res
		
  1. 回溯+剪枝
    @@因为不是有序数组,不能直接通过前一位和后一位相等去重
    §. 砍掉树层上重复的:因为原数组无序:
    用set记录已存放数组:
    used = set()
    if nums[i] in used
    used.add(nums[i])
    §. 砍掉元素小于store最后一位的
		 def backtracking(nums, startindex):

            if len(store) >= 2:                    
                res.append(store[:])
            used = set()#所放位置保证了当进行到下一层时进行清零
            for i in range(startindex, len(nums)):

                # if i > startindex and nums[i] == nums[i - 1] :
                #     continue
                if store and nums[i] < store[-1] or nums[i] in used:
                    continue
                used.add(nums[i])
                store.append(nums[i])
                backtracking(nums, i + 1)
                store.pop()
        res = []
        store = []
        backtracking(nums, 0)
        return res
		

二、leetcode 46.全排列 §§§§

1.题目链接:

link

2.独立做题问题总结

3.解法总结:

  1. 回溯+去重
    用used数组进行记录,若用过则continue
		def backtracking(nums):
            if len(store) == len(nums):
                res.append(store[:])
                return            
            for i in range(len(nums)):
                if used[i] == True:
                    continue
                store.append(nums[i])
                used[i] = True
                backtracking(nums)
                store.pop()
                used[i] = False
        res = []
        store = []
        used = [0] * len(nums)
        backtracking(nums)
        return res
		
				if used[i] == True:
                    continue

§§§用continue跳过,因为还可以取同一层的其他元素
或者:

		# 若遇到self.path里已收录的元素,跳过
            if nums[i] in self.path:
                continue

三、leetcode 47.全排列 II §§§

1.题目链接:

link

2.独立做题问题总结

3.解法总结:

leetcode day 29 * 491.递增子序列 * 46.全排列 * 47.全排列 II_第1张图片
nums[i] == nums[i - 1] and used[i - 1] == False -------前半部分判断重复元素,后半部分判断在同一层

		used = [0] * len(nums)
        res = []
        store = []
        def backtracking(nums):

            if len(nums) == len(store):
                res.append(store[:])
                return
            
            for i in range(len(nums)):
                if i > 0 and nums[i] == nums[i - 1] and used[i - 1] == False:
                    continue
                if used[i] == False:
                    store.append(nums[i])
                    used[i] = True
                    backtracking(nums)
                    used[i] = False
                    store.pop()
        nums.sort()
        backtracking(nums)
        return res
		

(used[i - 1] == true)
leetcode day 29 * 491.递增子序列 * 46.全排列 * 47.全排列 II_第2张图片

你可能感兴趣的:(leetcode,leetcode,算法,职场和发展)