算法练习Day24 (Leetcode/Python-回溯算法)

93. Restore IP Addresses

valid IP address consists of exactly four integers separated by single dots. Each integer is between 0 and 255 (inclusive) and cannot have leading zeros.

  • For example, "0.1.2.201" and "192.168.1.1" are valid IP addresses, but "0.011.255.245""192.168.1.312" and "192.168@1.1" are invalid IP addresses.

Given a string s containing only digits, return all possible valid IP addresses that can be formed by inserting dots into s. You are not allowed to reorder or remove any digits in s. You may return the valid IP addresses in any order.

Example 1:

Input: s = "25525511135"
Output: ["255.255.11.135","255.255.111.35"]

思路:分割一个字符串为四段,输出所有满足有效IP地址的分割。分为四段就是切三刀,判断每一段是不是有效,切完三刀后,看第四段是否符合要求,如果还是符合,就输出该结果,否则直接return。

class Solution(object):
    def backtrack(self, s, start_index, point_num, current, result):
        if point_num == 3:
            if self.is_valid(s, start_index, len(s) - 1):
                current += s[start_index:]
                result.append(current)
            return
        for i in range(start_index, len(s)):
            if self.is_valid(s, start_index, i):
                sub = s[start_index:i+1]
                self.backtrack(s,i+1,point_num+1, current+sub+'.', result)

    def is_valid(self, s, start, end):
        if start > end:
            return False
        if s[start] == '0' and start != end:  # 0开头的数字不合法
            return False
        num = 0
        for i in range(start, end + 1):
            if not s[i].isdigit():  # 遇到非数字字符不合法
                return False
            num = num * 10 + int(s[i])
            if num > 255:  # 如果大于255了不合法
                return False
        return True

    def restoreIpAddresses(self, s):
        """
        :type s: str
        :rtype: List[str]
        """
        result = []
        self.backtrack(s, 0, 0, "", result)
        return result

78. Subsets

Given an integer array nums of unique elements, return all possible 

subsets

 (the power set).

The solution set must not contain duplicate subsets. Return the solution in any order.

Example 1:

Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

思路:列举所有可能的子集。那么所有的子集都是可以在backtrack中被放入result的,而不需要特别的判断了。因为是原数组内无元素重复,所以也不用担心子集重复。

class Solution(object):
    def backtrack(self, nums, start_index, path, result):
        result.append(path[:])
        for i in range(start_index, len(nums)):
            path.append(nums[i])
            self.backtrack(nums, i+1, path, result)
            path.pop()

    def subsets(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        result = []
        self.backtrack(nums, 0, [], result)
        return result

相关推荐

  1. 算法练习Day24 (Leetcode/Python-回溯算法

    2023-12-29 10:46:29       40 阅读
  2. 算法练习Day21 (Leetcode/Python-回溯算法

    2023-12-29 10:46:29       31 阅读
  3. 算法练习Day22 (Leetcode/Python-回溯算法

    2023-12-29 10:46:29       35 阅读
  4. Day 24 回溯算法 1

    2023-12-29 10:46:29       38 阅读
  5. Day 24 回溯算法01

    2023-12-29 10:46:29       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-29 10:46:29       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-29 10:46:29       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-29 10:46:29       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-29 10:46:29       20 阅读

热门阅读

  1. 二、C#基础语法( 控制结构)

    2023-12-29 10:46:29       41 阅读
  2. 小程序webView初始化销毁页面

    2023-12-29 10:46:29       46 阅读
  3. 举例说明自然语言处理(NLP)技术。

    2023-12-29 10:46:29       29 阅读
  4. Decode函数:解码编程中的密码

    2023-12-29 10:46:29       37 阅读
  5. go 语言程序设计第4章--复合数据类型

    2023-12-29 10:46:29       37 阅读
  6. JVM系列-方法区、堆区、栈区

    2023-12-29 10:46:29       36 阅读
  7. 【刷图】最短路径算法

    2023-12-29 10:46:29       39 阅读
  8. JWT使用HS512算法生成全局服务token原理

    2023-12-29 10:46:29       47 阅读
  9. oj 1.8编程基础之多维数组 24:蛇形填充数组

    2023-12-29 10:46:29       37 阅读
  10. 位运算:消失的两个数字

    2023-12-29 10:46:29       38 阅读
  11. 【洛谷】高考组题

    2023-12-29 10:46:29       42 阅读
  12. Ubuntu零基础教程

    2023-12-29 10:46:29       29 阅读
  13. C语言-破解密码

    2023-12-29 10:46:29       31 阅读