leetcode day 28 93.复原IP地址 78.子集 90.子集II

文章目录

  • 一、leetcode 93.复原IP地址 §§§§§§
    • 1.题目链接:
    • 2.独立做题问题总结
    • 3.解法总结:
  • 二、leetcode 78.子集
    • 1.题目链接:
    • 2.独立做题问题总结
    • 3.解法总结:
  • 三、leetcode 90.子集II
    • 1.题目链接:
    • 2.独立做题问题总结
    • 3.解法总结:


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

一、leetcode 93.复原IP地址 §§§§§§

1.题目链接:

link

2.独立做题问题总结

3.解法总结:

  1. 回溯
    § check函数 判断分支
    § 回溯:将字符串加三个分隔符,当分隔符数目为3时在判断条件里判断第四个子串,若满足,则存储加.后的字符串。
    § 当前子串为 l = s[startindex: i + 1]
    § 每获取一个子串在加入cur之前先判断是否满足check条件(剪枝
		if len(s) > 12 or len(s) < 4:
            return []
        
        def check(l):
            
            if len(l) > 3 or len(l) < 1:
                return False
            if len(l) > 1 and l[0] == "0":
                return False
            if len(l) == 3:
                n = int(l)
                if n > 255:
                    return False
            return True

        def backtracking(s, startindex, cnt):
            nonlocal num
            if cnt == 3:
                if not check(s[startindex:]):
                    return 
                ss = ""
                for i in range(3):
                    ss = ss + cur[i] + "."
                ss += s[startindex:]
                res.append(ss)
                return
            for i in range(startindex, len(s)):
                l = s[startindex: i + 1]
                # print(f"cur l before = {l}, cnt = {cnt}, cur = {cur}")
                if check(l):
                    cur.append(l)
                    cnt += 1
                    # print(f"cur = {cur}, cnt = {cnt}")
                    backtracking(s, i + 1, cnt)
                    cur.pop()
                    cnt -= 1
                else:
                    continue
        res = []
        num = 0
        cur = []
        backtracking(s, 0, 0)
        return res

		

二、leetcode 78.子集

1.题目链接:

link

2.独立做题问题总结

3.解法总结:

  1. 回溯
    对每个节点的store进行存储
		res = []
        store = []
        cnt = 0
        def backtracking(nums, startindex):
            nonlocal cnt
            #if cnt == pow(2, len(nums)):
            #    return
            res.append(store[:])
            for i in range(startindex, len(nums)):
                used[i] = 1
                store.append(nums[i])
                cnt += 1
                backtracking(nums, i + 1)
                used[i] = 0
                store.pop()
        used = [0] * len(nums)
        backtracking(nums, 0 )
        return res
		

三、leetcode 90.子集II

1.题目链接:

link

2.独立做题问题总结

3.解法总结:

  1. 回溯+去重
    i > startindex and nums[i] == nums[i - 1]
		res = []
        store = []
        used = [0] * len(nums)
        nums.sort()
        def backtracking(nums, startindex):
            # 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
                store.append(nums[i])
                backtracking(nums, i + 1)
                store.pop()
        backtracking(nums, 0)
        return res
		

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