leetcode 刷题记录(java)-持续更新

 

 

感觉说的挺好的,值得学习

1
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 2 本文链接:https://blog.csdn.net/liujiaqi12345/article/details/88357041 3 Leetcode JAVA 题解: https://github.com/mJackie/leetcode 4 自己日常刷题经过是这样的: 5 6 拿到题目,看一眼Difficulty,然后自己思考一下解题思路。如果解不出来,就记下在哪里卡住了,难点在哪。 7 如果对应的题目有Solution,就看Solution,没有的话就点Discuss,按Most Votes排序,看排名最高的解法。 8 对比一下自己的解法与最优的解法的差别,总结一下为什么没想起来,记录下来这个思考的过程。 9 关掉别人的代码,开始Coding,Debug,Submit。 10 附上自己总结的几条经验: 11 12 先刷两个Top专题。Leetcode 上有个List选项,里边有两个专题,分别是Top 100 Liked Questions和Top Interview Questions。这两个List中有很多重复的题,加起来一共150道左右。都是经典的题目,将这150道刷完基本上所有的题型都见过了,而且多数经典题目都会涉及,是提升最快的一个方法。 13 14 注意记录、总结与复习。自己写过的代码一定要保存下来,刷题的时候也要记下主要思路和注意点,这样在复习的时候也能对比发现自己哪里还能改进,之前犯得错误有没有重犯。可以将相互关联的题目对比着一起看,方便总结与记忆。一定要时常复习刷过的题,复习比一味的追求数量更重要。 15 16 做好Easy,没必要死扣Hard。LeetCode上很多Easy的题目看似简单,实则想要写出Perfect的代码并非易事。多思考如何优化Easy,Medium的解法实际上比花精力解Hard题更能提高自己。况且面试的时候Hard被问的概率太小了。 17 18 切忌眼高手低。不要想着自己知道思路解法了就是会了,一定要亲自Coding,手撸出来。我在刷的过程中就经常在Debug的时候才发现自己忘记考虑了某些条件。不把代码写出来,只看别人的答案对自己是没有多大的提高的,只有亲自AC了题目,才能算做过一道题。 19 ———————————————— 20 版权声明:本文为CSDN博主「Jackie.Liu」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 21 原文链接:https://blog.csdn.net/liujiaqi12345/article/details/88357041

还有这注释方式也不错,学习

  • 语言: Java

  • 说明: 每道题在代码头部都添加了我的解题思路和批注,Eg:

      /*****
       * 287. Find the Duplicate Number
       * 题意:n+1个数属于[1~n],找出重复的那个数
       * 难度:Medium
       * 分类:Array, Two Pointers, Binary Search
       * 思路:如果nums[i]不在对应位置,则和对应位置交换。如果对应位置上也为该数,说明这个数就是重复的数字。这个方法改变了数组。是错误的。
       *      另一种方法,把问题转换成有环链表,找环的起始节点。O(n) O(1) lc142
       *      二分查找,每次看一边数字的个数, O(nlog(n)) O(1)
       * Tips:剑指offer原题
       */
    
  •  



 

8. String to Integer (atoi)


 

 1 public static int myAtoi(String str) {
 2 
 3             // 1字符串非空判断 ""||" "
 4             if (str.isEmpty() || str.trim().isEmpty()) {
 5                 return 0;
 6             }
 7 
 8             int index = 0;
 9             int sign = 1;
10             int total = 0;
11             //1检测第一个非空字符串是什么
12             while (str.charAt(index) == ' ' && index < str.length()) {
13                 index++;
14             }
15 
16             //1判断这个数是正数还是负数
17             if (str.charAt(index) == '+' || str.charAt(index) == '-') {
18                 sign = str.charAt(index) == '+' ? 1 : -1;
19                 index++;
20             }
21             
22             //1判断是否是数字,是否越界,如果越界就取越界的边界值
23             while (index < str.length()) {
24                 int digit = str.charAt(index) - '0';
25                 if (digit < 0 || digit > 9) {
26                     break;
27                 }
28 
29                 if (Integer.MAX_VALUE / 10 > total
30                         || (Integer.MAX_VALUE / 10 == total && Integer.MAX_VALUE % 10 >= digit)) {
31                     total = total * 10 + digit;
32                     index++;
33                 } else {
34                     return sign > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
35                 }
36             }
37             return total * sign;
38 
39         }

 

 

 

1. 两数之和

 
 1         public int[] twoSum(int[] nums, int target) {
 2             int[] result = new int[2];
 3             Map<Integer, Integer> map = new HashMap<Integer, Integer>();
 4             for (int i = 0; i < nums.length; i++) {
 5                 if (map.containsKey(target - nums[i])) {
 6                     result[1] = i;
 7                     result[0] = map.get(target - nums[i]);
 8                     return result;
 9                 }
10                 map.put(nums[i], i);
11             }
12             return result;
13 
14         }
15     

 

 

7. 整数反转

 1         public int reverse(int x) {
 2             int ans = 0;
 3             while (x != 0) {
 4                 int pop = x % 10;
 5                 if (ans > Integer.MAX_VALUE / 10 || (ans == Integer.MAX_VALUE / 10 && pop > 7))
 6                     return 0;
 7                 if (ans < Integer.MIN_VALUE / 10 || (ans == Integer.MIN_VALUE / 10 && pop < -8))
 8                     return 0;
 9                 ans = ans * 10 + pop;
10                 x /= 10;
11             }
12             return ans;
13         }
14     

 

 

20. 有效的括号

 1     class Solution {
 2         public boolean isValid(String s) {
 3             int n = s.length();
 4             for (int i = 0; i < n / 2; i++) {
 5                 if (s.contains("{}"))
 6                     s = s.replace("{}", "");
 7                 if (s.contains("()"))
 8                     s = s.replace("()", "");
 9                 if (s.contains("[]"))
10                     s = s.replace("[]", "");
11             }
12             if ("".equals(s)) {
13                 return true;
14             }
15             return false;
16         }
17     }

 

 

 

 

26. 删除排序数组中的重复项

 

 1     class Solution {
 2         public int removeDuplicates(int[] nums) {
 3             int number = 0;
 4             for (int i = 0; i < nums.length; i++) {
 5                 if (nums[i] != nums[number]) {
 6                     number++;
 7                     nums[number] = nums[i];
 8                 }
 9             }
10             for (int i = (++number); i < nums.length; i++) {
11                 nums[i] = 0;
12             }
13             return number;
14         }
15     }

 

204. 计数质数

 

 

 1     class Solution {
 2         public int countPrimes(int n) {
 3             int[] num = new int[n];
 4 
 5             for (int i = 0; i < n; i++) {
 6                 num[i] = 1;
 7             }
 8 
 9             for (int j = 2; j < n; j++) {
10                 if (num[j] == 1) {
11                     for (int k = 2; k * j < n; k++) {
12                         num[j * k] = 0;
13                     }
14                 }
15             }
16             int sum = 0;
17             for (int i = 2; i < n; i++) {
18                 if (num[i] == 1) {
19                     sum++;
20                 }
21             }
22             return sum;
23         }
24 
25     }

 

412. Fizz Buzz

 1     class Solution {
 2         public List<String> fizzBuzz(int n) {
 3             List<String> result = new ArrayList<>();
 4             for (int i = 1; i <= n; i++) {
 5                 if (i % 3 == 0 && i % 5 == 0) {
 6                     result.add("FizzBuzz");
 7                 } else if (i % 3 == 0) {
 8                     result.add("Fizz");
 9                 } else if (i % 5 == 0) {
10                     result.add("Buzz");
11                 } else {
12                     result.add("" + i);
13                 }
14 
15             }
16             return result;
17         }
18     }

 

 

 136. Single Number

 1   public int singleNumber(int[] nums) {
 2         
 3             
 4             Map<Integer, Integer> map = new HashMap<Integer, Integer>();
 5 
 6             for (int i = 0; i < nums.length; i++) {
 7                 if (map.containsKey(nums[i])) {
 8                     map.remove(nums[i]);
 9                 } else {
10                     map.put(nums[i], 1);
11                 }
12 
13             }
14 
15             Integer result = 0;
16             for (Integer key : map.keySet()) {
17                 result = key;
18                 break;
19             }
20 
21             return result;
22         
23         
24     }

 

 137. Single Number II

 1  public int singleNumber(int[] nums) {
 2         
 3         
 4             Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();
 5 
 6             for (int i = 0; i < nums.length; i++) {
 7                 if (map.containsKey(nums[i])) {
 8                     map.put(nums[i], false);
 9                 } else {
10                     map.put(nums[i], true);
11                 }
12 
13             }
14 
15             List<Integer> result = new ArrayList<Integer>();
16             map.forEach((k, v) -> {
17                 if (v) {
18                     result.add(k);
19                 }
20             });
21             return result.get(0);
22         
23         
24     }

 

 167. Two Sum II - Input array is sorted

 1 public int[] twoSum(int[] numbers, int target) {
 2 
 3             int[] position = new int[2];
 4             for (int i = 0; i < numbers.length; i++) {
 5                 for (int j = i + 1; j < numbers.length; j++) {
 6                     if (numbers[i] + numbers[j] == target) {
 7                         position[0] = ++i;
 8                         position[1] = ++j;
 9                         break;
10                     }
11                 }
12             }
13 
14             return position;
15 
16         }

 

 

 260. Single Number III

 1  public int[] singleNumber(int[] nums) {
 2         
 3             Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();
 4 
 5             for (int i = 0; i < nums.length; i++) {
 6                 if (map.containsKey(nums[i])) {
 7                     map.put(nums[i], false);
 8                 } else {
 9                     map.put(nums[i], true);
10                 }
11 
12             }
13 
14             List<Integer> resultTemp = new ArrayList<Integer>();
15 
16             map.forEach((k, v) -> {
17                 if (v) {
18                     resultTemp.add(k);
19                 }
20             });
21 
22             int[] result = new int[resultTemp.size()];
23 
24             for (int i = 0; i < resultTemp.size(); i++) {
25                 result[i] = resultTemp.get(i);
26             }
27             return result;
28         
29     }

 

 

448. Find All Numbers Disappeared in an Array

 

 1   public List<Integer> findDisappearedNumbers(int[] nums) {
 2         
 3             
 4             Map<Integer, Integer> numsMap = new HashMap<Integer, Integer>();
 5             for (int i = 0; i < nums.length; i++) {
 6                 numsMap.put(nums[i], i);
 7             }
 8 
 9             // 1给定数组应该有的大小
10             int size = nums.length;
11 
12             List<Integer> disappearedNumbers = new ArrayList<Integer>();
13             for (int i = 1; i <= size; i++) {
14                 if (!numsMap.containsKey(i)) {
15                     disappearedNumbers.add(i);
16                 }
17             }
18 
19             return disappearedNumbers;
20         
21     }

 

 

 

 442. Find All Duplicates in an Array

 1   public List<Integer> findDuplicates(int[] nums) {
 2         
 3             // 1对于给定数组进行排序
 4             Map<Integer, Integer> numsMap = new HashMap<Integer, Integer>();
 5             for (int i = 0; i < nums.length; i++) {
 6                 if(numsMap.containsKey(nums[i])) {
 7                     numsMap.put(nums[i], 2);
 8                 }else {
 9                     numsMap.put(nums[i], 1);
10                 }
11             }
12 
13             List<Integer> disappearedNumbers = new ArrayList<Integer>();
14             numsMap.forEach((k,v)->{
15                 if(v==2) {
16                     disappearedNumbers.add(k);
17                 }
18             });
19             
20             return disappearedNumbers;
21         
22         
23     }

 41. First Missing Positive

 

 1 public static int firstMissingPositive(int[] nums) {
 2             if (nums.length == 0) {
 3                 return 1;
 4             }
 5 
 6             Set<Integer> numsSet = new HashSet<Integer>();
 7             for (int i = 0; i < nums.length; i++) {
 8                 if (nums[i] < 1) {
 9                     continue;
10                 } else {
11                     numsSet.add(nums[i]);
12                 }
13             }
14             List<Integer> numsList = new ArrayList<Integer>();
15             numsSet.forEach(n -> numsList.add(n));
16 
17             // 1筛选过后的数组为空
18             if (numsList.size() == 0) {
19                 return 1;
20             }
21 
22             numsList.sort((a, b) -> a.compareTo(b.intValue()));
23 
24             int index = 0;// 1当前数组下标
25             for (int i = 1;; i++) {
26                 // 1预防数组越界
27                 if (index < numsList.size() && numsList.get(index) == i) {
28                     index++;
29                 } else {
30                     return i;
31                 }
32             }
33 
34         }

 

 283. Move Zeroes

 1 public static void moveZeroes(int[] nums) {
 2             for (int i = 0; i < nums.length; i++) {
 3                 if (nums[i] != 0) {
 4                     continue;
 5                 } else {
 6                     int count = 0;
 7                     do {
 8                         swap(nums, i);//如果当前位置为0,则置换到最后一位
 9                         count++;
10                     } while (nums[i] == 0 && count < nums.length - i);//如果当前位置是0,并且之前也没有0啦,则终止循环,终止条件有待优化
11                 }
12             }
13             for (int i = 0; i < nums.length; i++) {
14                 System.out.print(nums[i]);
15             }
16         }
17 
18         public static void swap(int[] nums, int position) {//普通的置换算法,冒泡排序里的一段
19             for (int i = position; i < nums.length - 1; i++) {
20                 int temp = nums[i];
21                 nums[i] = nums[i + 1];
22                 nums[i + 1] = temp;
23             }
24         }

 27. Remove Element

 

 1         public static int removeElement(int[] nums, int val) {
 2             if (nums == null) {
 3                 return -1;
 4             } else if (nums.length == 0) {
 5                 return 0;
 6             } else {
 7                 int count = 0;//统计几个不相同,同时作为新数组的下标
 8                 for (int i = 0; i < nums.length; i++) {
 9                     if (nums[i] != val) {
10                         nums[count++] = nums[i];//注意count++的执行顺序
11                     }
12                 }
13                 return count;
14             }
15 
16         }

 

 26. Remove Duplicates from Sorted Array

 1 public static int removeDuplicates(int[] nums) {
 2             int count = 1;
 3             for (int i = 1; i < nums.length; i++) {//用当前的数字和上一个被比较的数字进行比较,如果大于他就替换,本题默认一排序
 4                 if (nums[i] > nums[count - 1]) {
 5                     nums[count++] = nums[i];
 6                 }
 7             }
 8             return count;
 9 
10         }

 

 

 80. Remove Duplicates from Sorted Array II

1 public static int removeDuplicates(int[] nums) {
2             int count = 2;
3             for (int i = 2; i < nums.length; i++) {
4                 if (nums[i] > nums[count - 2]) {
5                     nums[count++] = nums[i];
6                 }
7             }
8             return count;
9         }

 

 

 

 617. Merge Two Binary Trees

 

 1 /**
 2      * 题意:将俩个二叉树及进行合并,如果同一位置节点都存在,则合并,否则旧直接放上去
 3      * 思路:相同位置进行合并,有则相加,无责这届放上去,注意用递归
 4      * @param t1
 5      * @param t2
 6      * @return
 7      */
 8     public static TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
 9         if (t1 == null && t2 == null) {
10             return null;
11         }
12         if (t1 == null) {
13             return t2;
14         }
15         if (t2 == null) {
16             return t1;
17         }
18 
19         TreeNode t = new TreeNode(t1.val + t2.val);
20         t.left = mergeTrees(t1.left, t2.left);
21         t.right = mergeTrees(t1.right, t2.right);
22         return t;
23     }

 

 

 2. Add Two Numbers

/**
     * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 +
     * 465 = 807
     * 
     * 题意:对于俩个链表。对应节点相加,满十进一
     * 思路:先判断对应节点是否至少存在一个有值,有则相加,然后移动节点向下,循环如此,如果说最后一次相加,进位(carry)不为0,则要显示,其次,返回值要从返回链表的第二个几点开始
     * 
     * @param l1
     * @param l2
     * @return
     */
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode resultNode = new ListNode(0);
        ListNode p = l1, q = l2, curr = resultNode;
        int carry = 0;
        while (p != null || q != null) {
            int x = p != null ? p.val : 0;
            int y = q != null ? q.val : 0;
            int sum = x + y + carry;
            carry = sum / 10;
            curr.next = new ListNode(sum % 10);
            curr = curr.next;
            if (p != null) {
                p = p.next;
            }
            if (q != null) {
                q = q.next;
            }
        }
        if (carry > 0) {
            curr.next = new ListNode(carry);
        }
        return resultNode.next;
    }

 

 

 3. Longest Substring Without Repeating Characters

 1 public static void main(String[] args) {
 2         String s = "abbabc";
 3         System.out.println(lengthOfLongestSubstring(s));
 4     }
 5 
 6     public static int lengthOfLongestSubstring(String s) {
 7         int max = 0;
 8         // ”记录当前重复字母的最新位置“
 9         int j = 0;
10         HashMap<Character, Integer> resultMap = new HashMap<Character, Integer>();
11         for (int i = 0; i < s.length(); i++) {
12             if (resultMap.containsKey(s.charAt(i))) {
13                 j = Math.max(resultMap.get(s.charAt(i)) + 1, j);
14             }
15             //”当前位置-上次重复的最大位置+1“
16             max = Math.max(i - j + 1, max);
17             resultMap.put(s.charAt(i), i);
18         }
19 
20         return max;
21     }

 

 

 11. Container With Most Water

 1 /**
 2      * 解法1:俩边的边为起点,进行计算,如果左边的边比右边的小,左边第二条边和当前右边的边进行计算,如果右边的边小于左边的边,则右边的第二条便进行计算,依此类推
 3      * 
 4      * @param height
 5      * @return
 6      */
 7     public static int maxArea(int[] height) {
 8         int i = 0, j = height.length - 1, res = 0;
 9         while (i < j) {
10             // ‘取最大值’
11             res = Math.max(res, Math.min(height[i], height[j]) * (j - i));
12             if (height[i] < height[j]) {
13                 i++;
14             } else {
15                 j--;
16             }
17         }
18         return res;
19     }
20 
21     /**
22      * 解法2 遍历所有的可能结果n(n-1)/2中情况
23      * 
24      * @param height
25      * @return
26      */
27     public int maxArea1(int[] height) {
28         int max = 0;
29         for (int i = 0; i < height.length; i++) {
30             for (int j = i + 1; j < height.length; j++) {
31                 max = Math.max(max, Math.min(height[i], height[j]) * (j - i));
32             }
33         }
34 
35         return max;
36     }

 

 17. Letter Combinations of a Phone Number

 1     public static List<String> letterCombinations(String digits) {
 2         List<String> ret = new ArrayList<String>();
 3         Map<Character, String> map = new HashMap<Character, String>() {
 4             {
 5                 put('2', "abc");
 6                 put('3', "def");
 7                 put('4', "ghi");
 8                 put('5', "jkl");
 9                 put('6', "mno");
10                 put('7', "pqrs");
11                 put('8', "tuv");
12                 put('9', "wxyz");
13             }
14         };
15 
16         //‘非空校验’
17         if (digits == null || "".equals(digits)) {
18             return ret;
19         }
20         dfs(digits, 0, "", map, ret);
21 
22         return ret;
23     }
24 
25     public static void dfs(String digits, int idx, String path, Map<Character, String> map, List<String> ret) {
26         if (digits.length() == path.length()) {
27             ret.add(path);
28             return;
29         }
30         //‘循环配合递归’
31         for (int i = idx; i < digits.length(); i++) {
32             for (char c : map.get(digits.charAt(i)).toCharArray()) {//这里是第个数字的对应的字母
33                 dfs(digits, i + 1, path + c, map, ret);//这里进行递归,对应的第二个数字的循环,和第一个字母进行拼接
34             }
35         }
36     }

 

19. Remove Nth Node From End of List

 1 public class ListNode {
 2     int val;
 3     ListNode next;
 4 
 5     ListNode(int x) {
 6         val = x;
 7     }
 8 
 9     /**
10      *‘本题思路:建立俩个链表,一个是dummy,复制原链表,另一个链表(first)为了计算链表长度;然后在用first链表指向dummy,删掉指定位置的元素’
11      *‘注意,应为是dummy指向head,所以多了一个节点,在指定删除位置时不用减一;另外返回时应该返回dummy.next,第一个节点是我们自己定义的’ 
12      * @param head
13      * @param n
14      * @return
15      */
16     public static ListNode removeNthFromEnd(ListNode head, int n) {
17         ListNode dummy = new ListNode(0);
18         dummy.next = head;
19         ListNode first = head;
20         int length = 0;
21         while (first != null) {
22             length++;
23             first = first.next;
24         }
25         int position = length - n;
26         first = dummy;
27         while (position > 0) {
28             position--;
29             first = first.next;
30         }
31         first.next = first.next.next;
32         return dummy.next;
33 
34     }
35 
36     public static void main(String[] args) {
37         ListNode a1 = new ListNode(1);
38         ListNode a2 = new ListNode(2);
39         ListNode a3 = new ListNode(3);
40         ListNode a4 = new ListNode(4);
41         ListNode a5 = new ListNode(5);
42         a1.next = a2;
43         a2.next = a3;
44         a3.next = a4;
45         a4.next = a5;
46         ListNode a6 = removeNthFromEnd(a1, 2);
47         while (a6 != null) {
48             System.out.println(a6.val);
49             a6 = a6.next;
50         }
51     }
52 }

 

 20. Valid Parentheses

 

 1 /**
 2      * 题意:括号匹配,俩个匹配的括号之间是不允许有为匹配(也就是单个的)括号
 3      * 解题思路:通过入栈的形式,如果未匹配就入栈,匹配就出栈,最后如果栈不为空或者栈顶元素不当前元素不匹配就返回false
 4      * 
 5      * @param s
 6      * @return
 7      */
 8     public static boolean isValid(String s) {
 9         Stack<Character> stack = new Stack<Character>();
10         for (char c : s.toCharArray()) {
11             if (c == '(') {
12                 stack.push(')');
13             } else if (c == '[') {
14                 stack.push(']');
15             } else if (c == '{') {
16                 stack.push('}');
17             } else if (stack.isEmpty() || stack.pop() != c) {
18                 return false;
19             }
20         }
21         return stack.isEmpty();
22     }
23 
24     public static void main(String[] args) {
25         System.out.println(isValid("["));
26     }

 21. Merge Two Sorted Lists

 

 1     int val;
 2     ListNode next;
 3 
 4     ListNode(int x) {
 5         val = x;
 6     }
 7 
 8     /**
 9      * 本题思路:‘将当前节点l1.next和L2的当前节点(第一个节点)进行比较,如果小于等于(注意:等于也是可以的),继续往下走,反之则进行节点替换(l1.next和l2进行替换),当l2为null时(也就是l1.next=null)结束循环’
10      * @param l1
11      * @param l2
12      * @return
13      */
14     public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
15         if (l1 == null) {
16             return l2;
17         }
18         if (l2 == null) {
19             return l1;
20         }
21         //此处是为了保证第一个节点时最小值
22         ListNode tmp;
23         if (l1.val > l2.val) {
24             tmp = l2;
25             l2 = l1;
26             l1 = tmp;
27         }
28         ListNode newListNode = l1;
29         while (l2 != null) {
30             //遍历节点进行组合
31             while (newListNode.next != null && newListNode.next.val <= l2.val) {
32                 newListNode = newListNode.next;
33             }
34             //比较排序
35             tmp = newListNode.next;
36             newListNode.next = l2;
37             l2 = tmp;
38         }
39         return l1;
40 
41     }
42 
43     //展示当前链表的值
44     public static void sysoListNode(ListNode l1) {
45         while (l1 != null) {
46             System.out.format("%d->", l1.val);
47             l1 = l1.next;
48         }
49         System.out.println("===================");
50     }
51     
52     /**
53      * 大神的解法:原理和上面一样,只是利用递归的原理
54      * @param l1
55      * @param l2
56      * @return
57      */
58     public static ListNode mergeTwoLists2(ListNode l1, ListNode l2) {
59         if (l1 == null)
60             return l2;
61         if (l2 == null)
62             return l1;
63         if (l1.val < l2.val) {
64             l1.next = mergeTwoLists(l1.next, l2);
65             //此处时为了更直观看当前链表的状态
66             sysoListNode(l1);
67             sysoListNode(l2);
68             return l1;
69         } else {
70             l2.next = mergeTwoLists(l1, l2.next);
71             //此处时为了更直观看当前链表的状态
72             sysoListNode(l1);
73             sysoListNode(l2);
74             return l2;
75         }
76     }
77 
78     //测试数据
79     public static void main(String[] args) {
80         ListNode l1 = new ListNode(1);
81         ListNode l2 = new ListNode(2);
82         ListNode l3 = new ListNode(4);
83         l1.next = l2;
84         l2.next = l3;
85 
86         ListNode r1 = new ListNode(1);
87         ListNode r2 = new ListNode(3);
88         ListNode r3 = new ListNode(4);
89         r1.next = r2;
90         r2.next = r3;
91 
92         mergeTwoLists(l1, r1);
93 
94     }

 

 46. Permutations

 1 /**
 2      * 本题目标:对于给定数组列出所有的可能排列组合
 3      * 实现方法:利用递归的思路
 4      * 举个例子,当数组为【1,2,3】;先考虑第一个数为1时,后面的可能性,以此类推
 5      * 注意:后面的可能性要以递归的思路去考虑,或者入栈出栈的思路。
 6      * @param nums
 7      * @return
 8      */
 9     public static List<List<Integer>> permute(int[] nums) {
10         List<List<Integer>> list = new ArrayList<>();
11         // Arrays.sort(nums); // not necessary
12         backtrack(list, new ArrayList<>(), nums);
13         return list;
14     }
15 
16     private static void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums) {
17         if (tempList.size() == nums.length) {
18             //注意这里的细节,是新声明一个集合去保存这个值,如果用tempList会导致最后list为空,原因就是堆被清空啦
19             list.add(new ArrayList<Integer>(tempList));
20         } else {
21             for (int i = 0; i < nums.length; i++) {
22                 if (tempList.contains(nums[i])) {
23                     continue;
24                 } else {
25                     tempList.add(nums[i]);
26                     //注意是循环中调用递归
27                     backtrack(list, tempList, nums);
28                     //小算法,清空当前递归中的最后一个值
29                     tempList.remove(tempList.size() - 1);
30                 }
31             }
32 
33         }
34     }

 48. Rotate Image

 1 /**
 2      * 本体题意:顺时针反转90度
 3      * 解题方法:找出通项公式
 4      * @param matrix
 5      */
 6     public static void rotate(int[][] matrix) {
 7         int n = matrix.length;
 8         int[][] rotate = new int[n][n];
 9         for (int i = 0; i < n; i++) {
10             for (int j = 0; j < n; j++) {
11                 //通项公式
12                 rotate[i][j] = matrix[n - 1 - j][i];
13             }
14         }
15         for (int i = 0; i < n; i++) {
16             for (int j = 0; j < n; j++) {
17                 //重新赋值
18                 matrix[i][j] = rotate[i][j];
19             }
20         }
21     }

 

 

 

 

 49. Group Anagrams

 

 1     /**
 2      * 本题题意:将含有相同字母的字符串归类
 3      * 
 4      * 解法:将字符串拆分成字符,然后排序作为key,最后map转化成list
 5      */
 6     public static List<List<String>> groupAnagrams(String[] strs) {
 7         if (strs.length == 0) {
 8             return new ArrayList<List<String>>();
 9         }
10         Map<String, List<String>> map = new HashMap<String, List<String>>();
11         for (String s : strs) {
12             char[] chars = s.toCharArray();
13             Arrays.sort(chars);
14             String key = String.valueOf(chars);
15             if (!map.containsKey(key)) {
16                 map.put(key, new ArrayList<String>());
17             }
18             map.get(key).add(s);
19         }
20         return new ArrayList<List<String>>(map.values());
21 
22     }

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/xiaoshahai/p/11454298.html

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值