当前位置:   article > 正文

代码随想录day23 Java版

代码随想录day23 Java版

131.分割回文串

开幕雷击,感觉挺难的,需要把切割问题抽象为组合问题,还得设置切割过的地方不能重复切割,所以递归函数需要传入i + 1。

模拟切割线,其实就是index是上一层已经确定了的分割线,i是这一层试图寻找的新分割线

  1. class Solution {
  2. List<List<String>> res = new ArrayList<>();
  3. List<String> path = new LinkedList<>();
  4. public List<List<String>> partition(String s) {
  5. backtrack(s,0);
  6. return res;
  7. }
  8. void backtrack(String s, int start) {
  9. if (start >= s.length()) {
  10. res.add(new ArrayList(path));
  11. return;
  12. }
  13. for (int i = start; i < s.length(); i++) {
  14. if (isPalindrome(s, start, i)) {
  15. String str = s.substring(start, i + 1);
  16. path.addLast(str);
  17. } else {
  18. continue;
  19. }
  20. backtrack(s, i + 1);
  21. path.removeLast();
  22. }
  23. }
  24. boolean isPalindrome(String s, int start, int end) {
  25. for (int i = start, j = end; i < j; i++, j--) {
  26. if (s.charAt(i) != s.charAt(j)) return false;
  27. }
  28. return true;
  29. }
  30. }

93.复原IP地址

比较考字符串操作,回溯比较容易

  1. class Solution {
  2. List<String> res = new ArrayList<>();
  3. public List<String> restoreIpAddresses(String s) {
  4. StringBuilder sb = new StringBuilder(s);
  5. backTracking(sb, 0, 0);
  6. return res;
  7. }
  8. void backTracking(StringBuilder s, int start, int dotCount){
  9. if(dotCount == 3){
  10. if(isValid(s, start, s.length() - 1)){
  11. res.add(s.toString());
  12. }
  13. return;
  14. }
  15. for(int i = start; i < s.length(); i++){
  16. if(isValid(s, start, i)){
  17. s.insert(i + 1, '.');
  18. backTracking(s, i + 2, dotCount + 1);
  19. s.deleteCharAt(i + 1);
  20. }else{
  21. break;
  22. }
  23. }
  24. }
  25. boolean isValid(StringBuilder s, int start, int end){
  26. if(start > end) return false;
  27. if(s.charAt(start) == '0' && start != end) return false;
  28. int num = 0;
  29. for(int i = start; i <= end; i++){
  30. int digit = s.charAt(i) - '0';
  31. num = num * 10 + digit;
  32. if(num > 255) return false;
  33. }
  34. return true;
  35. }
  36. }

78.子集

可以注释掉 if (start >= nums.length) return; 这行代码,因为、在每一次递归调用中,都会先将当前的 path 加入到结果集中,然后再进行下一层的递归搜索。当 start >= nums.length 时,for 循环条件不满足,递归就自然会结束。

  1. class Solution {
  2. List<List<Integer>> res = new ArrayList<>();
  3. LinkedList<Integer> path = new LinkedList<>();
  4. public List<List<Integer>> subsets(int[] nums) {
  5. backtrack(nums,0);
  6. return res;
  7. }
  8. void backtrack(int[] nums, int start) {
  9. res.add(new ArrayList<>(path));
  10. //if (start >= nums.length) return;
  11. for(int i = start; i < nums.length; i++) {
  12. path.add(nums[i]);
  13. backtrack(nums, i+1);
  14. path.removeLast();
  15. }
  16. }
  17. }

90.子集II

和前一天的组合数去重一个套路,理解了树层去重和树枝去重

  1. class Solution {
  2. List<List<Integer>> res = new ArrayList<>();
  3. List<Integer> path = new LinkedList<>();
  4. boolean[] used;
  5. public List<List<Integer>> subsetsWithDup(int[] nums) {
  6. if (nums.length == 0){
  7. res.add(path);
  8. return res;
  9. }
  10. Arrays.sort(nums);
  11. used = new boolean[nums.length];
  12. backtrack(nums, 0);
  13. return res;
  14. }
  15. void backtrack(int[] nums, int start) {
  16. res.add(new ArrayList<>(path));
  17. //if (start >= nums.length) return;
  18. for(int i = start; i < nums.length; i++) {
  19. if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) continue;
  20. path.add(nums[i]);
  21. used[i] = true;
  22. backtrack(nums, i+1);
  23. path.removeLast();
  24. used[i] = false;
  25. }
  26. }
  27. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号