代码随想录算法训练营第二十四天-93复原IP地址、78子集、90子集II
-
前言
状态:93 可以 AC。78、90AC。
-
更新
初步题解
93 复原 IP 地址
题目链接:(https://leetcode.cn/problems/restore-ip-addresses)
解的时候不知道单字符串怎么操作了,加了一个数组。慢了很多。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public static List<String> restoreIpAddresses(String s) { List<String> result = new ArrayList<>(); List<String> temp = new ArrayList<>(); if (s.length() < 4 || s.length() > 12){ return result; } restoreIpAddressesDFS(0, temp, s, result); return result; }
private static void restoreIpAddressesDFS(int startIndex, List<String> temp, String s, List<String> result) { if (startIndex >= s.length() && temp.size() == 4) { String resultOne = String.join(".", temp); result.add(resultOne); } for (int i = startIndex; i < s.length() && Integer.parseInt(s.substring(startIndex, i + 1)) < 256; i++) { String substring = s.substring(startIndex, i + 1); if (substring.length() < 1 || substring.length() > 3) { continue; } if (substring.length() > 1 && substring.startsWith("0")) { continue; } temp.add(substring); restoreIpAddressesDFS(i + 1, temp, s, result); temp.remove(temp.size() - 1); } }
|
78 子集
题目链接:(https://leetcode.cn/problems/subsets)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public static List<List<Integer>> subsets(int[] nums) { List<List<Integer>> result = new ArrayList<>(); List<Integer> resultOne = new ArrayList<>(); Arrays.sort(nums); subsetsDFS(0, nums, resultOne, result); return result; }
private static void subsetsDFS(int startIndex, int[] nums, List<Integer> resultOne, List<List<Integer>> result) { result.add(new ArrayList<>(resultOne)); for (int i = startIndex; i < nums.length; i++) { resultOne.add(nums[i]); subsetsDFS(i + 1, nums, resultOne, result); resultOne.remove(resultOne.size() - 1); } }
|
90 子集 II
题目链接:(https://leetcode.cn/problems/subsets-ii)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public static List<List<Integer>> subsetsWithDup(int[] nums) { List<List<Integer>> result = new ArrayList<>(); List<Integer> resultOne = new ArrayList<>(); Arrays.sort(nums); subsetsDFS(0, nums, resultOne, result); return result; }
private static void subsetsDFS(int startIndex, int[] nums, List<Integer> resultOne, List<List<Integer>> result) { result.add(new ArrayList<>(resultOne)); for (int i = startIndex; i < nums.length; i++) { if (i - 1 >= startIndex && nums[i] == nums[i - 1]) { continue; } resultOne.add(nums[i]); subsetsDFS(i + 1, nums, resultOne, result); resultOne.remove(resultOne.size() - 1); } }
|
看解析
93 复原 IP 地址
题目链接/文章讲解:(https://programmercarl.com/0093.复原IP地址.html)
视频讲解:(https://www.bilibili.com/video/BV1XP4y1U73i/)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| class Solution { List<String> result = new ArrayList<String>(); StringBuilder stringBuilder = new StringBuilder();
public List<String> restoreIpAddresses(String s) { restoreIpAddressesHandler(s, 0, 0); return result; }
public void restoreIpAddressesHandler(String s, int start, int number) { if (start == s.length() && number == 4) { result.add(stringBuilder.toString()); return; } if (start == s.length() || number == 4) { return; } for (int i = start; i < s.length() && i - start < 3 && Integer.parseInt(s.substring(start, i + 1)) >= 0 && Integer.parseInt(s.substring(start, i + 1)) <= 255; i++) { if (i + 1 - start > 1 && s.charAt(start) - '0' == 0) { continue; } stringBuilder.append(s.substring(start, i + 1)); if (number < 3) { stringBuilder.append("."); } number++; restoreIpAddressesHandler(s, i + 1, number); number--; stringBuilder.delete(start + number, i + number + 2); } } }
|
78 子集
题目链接/文章讲解:(https://programmercarl.com/0078.子集.html)
视频讲解:(https://www.bilibili.com/video/BV1U84y1q7Ci)
90 子集 II
题目链接/文章讲解:(https://programmercarl.com/0090.子集II.html)
视频讲解:(https://www.bilibili.com/video/BV1vm4y1F71J)