C#代码随想录算法训练营day28|回溯算法、复原IP地址、子集

LeetCode93 复原 IP 地址

题目:

有效 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 ‘.’ 分隔
例如:“0.1.2.201” 和 “192.168.1.1” 是 有效 IP 地址,但是 “0.011.255.245”、“192.168.1.312” 和 “[email protected]” 是 无效 IP 地址。
给定一个只包含数字的字符串 s ,用以表示一个 IP 地址,返回所有可能的有效 IP 地址,这些地址可以通过在 s 中插入 ‘.’ 来形成。你 不能 重新排序或删除 s 中的任何数字。你可以按 任何 顺序返回答案。

示例 1:
输入:s = “25525511135”
输出:[“255.255.11.135”,“255.255.111.35”]

示例 2:
输入:s = “0000”
输出:[“0.0.0.0”]

示例 3:
输入:s = “101023”
输出:[“1.0.10.23”,“1.0.102.3”,“10.1.0.23”,“10.10.2.3”,“101.0.2.3”]

代码:
public class Solution {
    
    
    IList<string> res = new List<string>();
    public IList<string> RestoreIpAddresses(string s)
    {
    
    
        if (s.Length > 12) return res;
        BackTracking(s, 0, 0);
        return res;
    }

    public void BackTracking(string s, int startIndex, int pointNum)
    {
    
    
        if (pointNum == 3)
        {
    
    
            if (IsValied(s, startIndex, s.Length - 1))
            {
    
    
                res.Add(s);
            }
            return;
        }
        for (int i = startIndex; i < s.Length; i++)
        {
    
    
            if (IsValied(s, startIndex, i))
            {
    
    
                s = s.Substring(0, i + 1) + "." + s.Substring(i + 1);
                pointNum++;
                BackTracking(s, i + 2, pointNum);
                pointNum--;//回溯
                s = s.Substring(0, i + 1) + s.Substring(i + 2);
            }
            else
            {
    
    
                break;
            }
        }
    }

    public bool IsValied(string s, int start, int end)
    {
    
    
        if (start > end) return false;
        if (s[start] == '0' && start != end)
        {
    
    
            return false;
        }
        int num = 0;
        for (int i = start; i <= end; i++)
        {
    
    
            if (s[i] > '9' || s[i] < '0')
            {
    
    
                return false;
            }
            num = num * 10 + (s[i] - '0');
            if (num > 255)
            {
    
    
                return false;
            }
        }
        return true;
    }
}

LeetCode78 子集

题目:

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

示例 1:
输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

示例 2:
输入:nums = [0]
输出:[[],[0]]

代码:
public class Solution {
    
    
    IList<IList<int>> result = new List<IList<int>>();
    IList<int> list = new List<int>();
    public IList<IList<int>> Subsets(int[] nums)
    {
    
    
        BackTrack(nums, 0);
        return result;
    }

    public void BackTrack(int[] nums, int startIndex)
    {
    
    
        result.Add(new List<int>(list));
        if (startIndex >= nums.Length)
        {
    
    
            return;
        }
        for (int i = startIndex; i < nums.Length; i++)
        {
    
    
            list.Add(nums[i]);
            BackTrack(nums, i + 1);
            list.RemoveAt(list.Count - 1);
        }
    }
}

LeetCode90 子集 II

题目:

给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。

示例 1:
输入:nums = [1,2,2]
输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]

示例 2:
输入:nums = [0]
输出:[[],[0]]

代码:
public class Solution {
    
    
    IList<IList<int>> res1 = new List<IList<int>>();
    IList<int> list1 = new List<int>();
    public IList<IList<int>> SubsetsWithDup(int[] nums)
    {
    
    
        Array.Sort(nums);
        BackTracking1(nums, 0);
        return res1;
    }

    public void BackTracking1(int[] nums, int startIndex)
    {
    
    
        res1.Add(new List<int>(list1));
        for (int i = startIndex; i < nums.Length; i++)
        {
    
    
            if (i > startIndex && nums[i - 1] == nums[i])
            {
    
    
                continue;
            }
            list1.Add(nums[i]);
            BackTracking1(nums, i + 1);
            list1.RemoveAt(list1.Count - 1);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44740741/article/details/130072285
今日推荐