感觉说的挺好的,值得学习
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; }