文章目录
- 从零开始刷leetcode的经验总结
- 1-100
- 1. Two Sum - Day 2-2 220914
- 2. Add Two Numbers - Day 3-1 220916
- 3. Longest Substring Without Repeating Characters - Day 4-1 220917
- 4. Median of Two Sorted Arrays - Day 4-2 220917
- 5. Longest Palindromic Substring - Day 5-2 220919
- 7. Reverse Integer - Day 7-1 220921
- 8. String to Integer (atoi) - Day 8-1 220922
- 10. Regular Expression Matching - Day 9-1 220923
- 11. Container With Most Water - Day 10-1 20220924
- 13. Roman to Integer - Day1-1 220913
- 14. Longest Common Prefix - Day 5-1 220919
- 15. 3Sum Day 10-2 20220924
- 17. Letter Combinations of a Phone Number - Day 12-1 220926
- 19. Remove Nth Node From End of List - Day 13-1 220927
- 20. Valid Parentheses -Day 11-1 220925
- 21. Merge Two Sorted Lists - Day 14-1 220928
- 22. Generate Parentheses - Day 15-1 220929
- 23. Merge k Sorted Lists - Day 15-2 220929
- 26. Remove Duplicates from Sorted Array - Day 6-1 220920
- 28. Find the Index of the First Occurrence in a String - Day 15-3 220929
- 29. Divide Two Integers- Day 16-1 220930
- 33. Search in Rotated Sorted Array - Day 18-1 221002
- 34. Find First and Last Position of Element in Sorted Array - Day 19-1 221003
- 36. Valid Sudoku - Day 20-1 221004
- 38. Count and Say - Day 21-1 221005
- 41. First Missing Positive - Day 22-1 221006
- 42. Trapping Rain Water - Day 23-1 221008
- 44. Wildcard Matching - Day 24-1 221009
- 46.Permutations - Day 25-1 221010
- 48. Rotate Image- Day 25-2 221010
- 49. Group Anagrams - Day 26-1 221011
- 50. Pow(x, n) - Day 27-1 221012
- 53. Maximum Subarray - Day 27-2 221012
- 54. Spiral Matrix - Day 28-1 221013
- 55. Jump Game - Day 28-2 221013
- 56. Merge Intervals - Day 32-1 221021
- 62. Unique Paths - Day 29-1 221014
- 66. Plus One - Day 29-2 221014
- 69. Sqrt(x) - Day 30-1 221015
- 70. Climbing Stairs - Day 30-2 221015
- 73. Set Matrix Zeroes - Day 31-1 221016
- 75. Sort Colors - Day 31-2 221016
- 76. Minimum Window Substring - Day 33-1 221022
- 78. Subsets - Day 33-2 221022
- 79. Word Search - Day 33-3 221022
- 84. Largest Rectangle in Histogram - Day 33-4 221022
- 88. Merge Sorted Array - Day 33-5 221022
- 91. Decode Ways - Day 33-6 221022
- 94. Binary Tree Inorder Traversal - Day 33-7 221022
- 98. Validate Binary Search Tree - Day 34-1 221023
- 101-200
- 101. Symmetric Tree - Day 34-2 221023
- 102. Binary Tree Level Order Traversal - Day 35-1 221028
- 103. Binary Tree Zigzag Level Order Traversal - Day 35-2 221028
- 104. Maximum Depth of Binary Tree - Day 36-1 221206
- 105. Construct Binary Tree from Preorder and Inorder Traversal - Day 36-2 221206
- 108. Convert Sorted Array to Binary Search Tree - Day 36-3 221206
- 116. Populating Next Right Pointers in Each Node - Day 36-4 221206
- 118. Pascal's Triangle - Day 37-1 221207
- 121. Best Time to Buy and Sell Stock - Day 37-2 221207
- 122. Best Time to Buy and Sell Stock II - Day 37-3 221207
- 124. Binary Tree Maximum Path Sum - Day 37-4 221207
- 124. Binary Tree Maximum Path Sum - Day 37-5 221207
- 125. Valid Palindrome - Day 37-6 221207
- 127. Word Ladder - Day 37-7 221207
- 128. Longest Consecutive Sequence - Day 37-8 221207
- 130. Surrounded Regions - Day 37-9 221207
- 131. Palindrome Partitioning - Day 37-10 221207
- 134. Gas Station - Day 37-11 221207
- 136. Single Number - Day 37-12 221207
- 138. Copy List with Random Pointer - Day 38-1 221208
- 139. Word Break - Day 38-2 221208
- 140. Word Break II - Day 38-3 221208
- 141. Linked List Cycle - Day 38-4 221208
- 146. LRU Cache - Day 38-5 221208
- 149. Max Points on a Line - Day 38-6 221208
- 150. Evaluate Reverse Polish Notation - Day 38-7 221208
- 152. Maximum Product Subarray - Day 38-8 221208
- 155. Min Stack - Day 38-9 221208
- 160. Intersection of Two Linked Lists - Day 38-10 221208
- 162. Find Peak Element - Day 39-01 230608
- 166. Fraction to Recurring Decimal - Day 39-02 230608
- 201-300
- 301-400
- 401-500
- 501+
- 刷题进度
从零开始刷leetcode的经验总结
1-100
真的是从零开始,以前只有点java基础,希望可以做到一天一题
1. Two Sum - Day 2-2 220914
看错题了,没有看到是两个数相加,解了大半天;
2. Add Two Numbers - Day 3-1 220916
昨天落了一天
还是没解出来哈哈哈哈
小垃圾真呀真垃圾
ListNode fake = new ListNode(0);
ListNode p = fake;
p相当于一个游标,fake为了找到头节点
3. Longest Substring Without Repeating Characters - Day 4-1 220917
1、判断字符串是否包含重复字符
public static boolean hasDuplicateChar2(String str) {
/* 判断一个字符最后一次出现的位置和当前位置是否相同,如果字母重复 则不相同,反之相同 */
for (int i = 0; i < str.length(); ++i) {
char ch = str.charAt(i);
if (str.lastIndexOf(ch) != i) {
return true;
}
}
return false;
}
2、ascii码字符只有128种取值的特性
3、学会用游标和哈希表哝
4. Median of Two Sorted Arrays - Day 4-2 220917
依然很暴力
double z = (double) y/x;
5. Longest Palindromic Substring - Day 5-2 220919
依然很暴力,不过也过了,感觉还是能粘贴出来炫耀一下的
public String longestPalindrome(String s) {
int n = s.length();
char[] str = s.toCharArray();
if (n == 0 || n == 1) return s;
int max = 1; int left = 0; int right = 0;
for (int i = 0; i < n; i++) {
int l = i; int r = i;
while (0<=l && r<n){
if (str[l] == str[r]){
l--;r++;
}else break;;
}
if (max < r-l+1){
max = r-l+1; left=l+1; right=r-1;
}
l = i; r = i+1;
while (0<=l && r<n){
if (str[l] == str[r]){
l--;r++;
}else break;;
}
if (max < r-l+1){
max = r-l+1; left=l+1; right=r-1;
}
}
return s.substring(left,right+1);
}
7. Reverse Integer - Day 7-1 220921
rev >= Integer.MAX_VALUE || rev <= Integer.MIN_VALUE
8. String to Integer (atoi) - Day 8-1 220922
没有考虑一些情况,不过最后做出来了
网上看看别人的方法似乎更简单
10. Regular Expression Matching - Day 9-1 220923
没做出来看得答案,复制粘贴一个最有意思的
public boolean isMatch(String s, String p) {
return s.matches(p);
}
做题的时候还是要多考虑考虑递归或者动态规划
11. Container With Most Water - Day 10-1 20220924
左右指针一滑动就可以做出来了
13. Roman to Integer - Day1-1 220913
用纯暴力的方法做出来的
1、字符串的比较不能用==,要用equals()
2、取字符串子串,.substring(i,j),只能取到i到j-1的字符串
3、有多种选择的时候可以用switch啊
14. Longest Common Prefix - Day 5-1 220919
纯暴力,没有考虑很多种情况:
1、当字符串数组只有一个元素的情况没有考虑
2、当后面元素比前面元素长度小的方法没有考虑
15. 3Sum Day 10-2 20220924
没做出来 一直都是 timeout
应该是list判断是否contain浪费了很长时间
17. Letter Combinations of a Phone Number - Day 12-1 220926
高兴 一次通过了
19. Remove Nth Node From End of List - Day 13-1 220927
自己的解法,跑过了100%的其他解法
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode p = head;
int i = 0;
while (p != null){
p = p.next; i++;
}
if (i == 1 && n == 1) return head.next;
if (i == n) return head.next;
p = head;
int j = 0;
while (p != null && j < i-n-1){
p = p.next; j++;
}
p.next = p.next.next;
return head;
}
}
20. Valid Parentheses -Day 11-1 220925
用栈解决了
只不过单数情况未考虑
看了别人的答案,自己的做法还是太复杂了
21. Merge Two Sorted Lists - Day 14-1 220928
一次做出来一次通过!
22. Generate Parentheses - Day 15-1 220929
我没做出来
将set转化为list的方法
Set<String> resultSet = new HashSet<>();
List<String> result = new ArrayList<>();
result.addAll(resultSet);
23. Merge k Sorted Lists - Day 15-2 220929
借用21题解了出来
26. Remove Duplicates from Sorted Array - Day 6-1 220920
解出来了
28. Find the Index of the First Occurrence in a String - Day 15-3 220929
return haystack.indexOf(needle);
一句话解出来了
不清楚题目用意
29. Divide Two Integers- Day 16-1 220930
^ java中的异或
要用位运算不然会超时
还要考虑到int溢出的问题
总之我没做出来
33. Search in Rotated Sorted Array - Day 18-1 221002
感觉自己做的很麻烦不过还是做出来了
34. Find First and Last Position of Element in Sorted Array - Day 19-1 221003
做是做出来了
Arrays.binarySearch(nums, target);(不一定返回的是第一次出现的位置)
数组循环、判断,第一步判断index是否溢出
36. Valid Sudoku - Day 20-1 221004
一遍做出来了,但是做法有点麻烦
38. Count and Say - Day 21-1 221005
41. First Missing Positive - Day 22-1 221006
明显不是O(n), 也给我通过了,很奇怪
42. Trapping Rain Water - Day 23-1 221008
没做出来看网上答案补的
44. Wildcard Matching - Day 24-1 221009
没做出来看网上答案补的
还是应该抽空学习一下动态规划
46.Permutations - Day 25-1 221010
dfs 全排列 全文背诵
48. Rotate Image- Day 25-2 221010
哇 先沿对角线翻转 在沿中垂线折叠 哪个天才想出来的
49. Group Anagrams - Day 26-1 221011
气死,坐半天还是个超时
50. Pow(x, n) - Day 27-1 221012
真让人无语,这题就纯纯搁那里卡bug呗
善用二分、善用递归、善用 System.out.println(Integer.MAX_VALUE); System.out.println(Integer.MIN_VALUE);
53. Maximum Subarray - Day 27-2 221012
54. Spiral Matrix - Day 28-1 221013
做是做出来了,真困难啊
55. Jump Game - Day 28-2 221013
思考的太复杂,用的递归,timeout
56. Merge Intervals - Day 32-1 221021
//按数组头元素升序排序(lambda表达式排序是真的慢)
//Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
Arrays.sort(intervals, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
});
// 列表转数组
itvs.toArray(new int[0][]);
62. Unique Paths - Day 29-1 221014
66. Plus One - Day 29-2 221014
69. Sqrt(x) - Day 30-1 221015
70. Climbing Stairs - Day 30-2 221015
73. Set Matrix Zeroes - Day 31-1 221016
75. Sort Colors - Day 31-2 221016
76. Minimum Window Substring - Day 33-1 221022
78. Subsets - Day 33-2 221022
& 是按位运算
79. Word Search - Day 33-3 221022
84. Largest Rectangle in Histogram - Day 33-4 221022
System.arraycopy的函数原型是:
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
其中:src表示源数组,srcPos表示源数组要复制的起始位置,desc表示目标数组,destPos在目标数组开始赋值的位置,length表示要复制的长度。
88. Merge Sorted Array - Day 33-5 221022
91. Decode Ways - Day 33-6 221022
94. Binary Tree Inorder Traversal - Day 33-7 221022
98. Validate Binary Search Tree - Day 34-1 221023
101-200
101. Symmetric Tree - Day 34-2 221023
102. Binary Tree Level Order Traversal - Day 35-1 221028
103. Binary Tree Zigzag Level Order Traversal - Day 35-2 221028
104. Maximum Depth of Binary Tree - Day 36-1 221206
105. Construct Binary Tree from Preorder and Inorder Traversal - Day 36-2 221206
108. Convert Sorted Array to Binary Search Tree - Day 36-3 221206
116. Populating Next Right Pointers in Each Node - Day 36-4 221206
118. Pascal’s Triangle - Day 37-1 221207
杨辉三角
121. Best Time to Buy and Sell Stock - Day 37-2 221207
122. Best Time to Buy and Sell Stock II - Day 37-3 221207
124. Binary Tree Maximum Path Sum - Day 37-4 221207
124. Binary Tree Maximum Path Sum - Day 37-5 221207
125. Valid Palindrome - Day 37-6 221207
如何逆转一个字符串
String rev = new StringBuffer(actual).reverse().toString();
判断一个char是字母和数字吗?
Character.isLetterOrDigit(cHead)
127. Word Ladder - Day 37-7 221207
能看懂答案,但是自己绝对做不出来!!!
128. Longest Consecutive Sequence - Day 37-8 221207
130. Surrounded Regions - Day 37-9 221207
131. Palindrome Partitioning - Day 37-10 221207
134. Gas Station - Day 37-11 221207
136. Single Number - Day 37-12 221207
用异或来消除两个相同的数字,太妙了
138. Copy List with Random Pointer - Day 38-1 221208
139. Word Break - Day 38-2 221208
140. Word Break II - Day 38-3 221208
141. Linked List Cycle - Day 38-4 221208
快慢指针,一个一次走一步,一个一次走两步,如果有环的话,二者总会相遇,太奇妙了
146. LRU Cache - Day 38-5 221208
基于 LinkedHashMap 实现
149. Max Points on a Line - Day 38-6 221208
150. Evaluate Reverse Polish Notation - Day 38-7 221208
152. Maximum Product Subarray - Day 38-8 221208
155. Min Stack - Day 38-9 221208
160. Intersection of Two Linked Lists - Day 38-10 221208
162. Find Peak Element - Day 39-01 230608
md 太长时间没有编程了,基本语法都忘完了,罪过罪过~
166. Fraction to Recurring Decimal - Day 39-02 230608
没做出来,看答案的
201-300
206. Reverse Linked List - Day 26-2 221011
234. Palindrome Linked List - Day1-2 220913
用暴力没有做出来,查的网络上的答案
1、java中的栈
Stack<ListNode> stack = new Stack<ListNode>();
301-400
383. Ransom Note - Day 1-3 220913
暴力,一下就通过了,只不过runtime就很可怜的
1、获得字符串中一个数组的位置indexOf(),没有返回0
2、获得字符串的第几个位置的字符charAt()
3、删除字符串temp位置的字符
magazine = magazine.substring(0,temp) + magazine.substring(temp+1);
4、flag[ch1[i] - 97]-- == 0 先比较再–
5、‘a’-97=0
387. First Unique Character in a String Day 221001
一遍过
401-500
412. Fizz Buzz - Day 1-4 220913
依然暴力
1、将字串 String 转换成整数 int
1-1、 int i = Integer.parseInt([String]); 或 i = Integer.parseInt([String],[int radix]);
1-2、 int i = Integer.valueOf(my_str).intValue();
注: 字串转成 Double, Float, Long 的方法大同小异.
2、 如何将整数 int 转换成字串 String ?
2-1、String s = String.valueOf(i);
2-2、String s = Integer.toString(i);
2-3、String s = “” + i;
501+
876. Middle of the Linked List - Day 1-5 220913
快慢指针做出来了
1337. The K Weakest Rows in a Matrix - Day 2-1 220914
昨天看了大半天没做出来,主要是不会带序号的比较
1、定义比较方法,r1[1], r2[1]返回负数,降序排列;r2[1], r1[1]返回正数,升序排列
Arrays.sort(info, (r1, r2) -> r1[1] != r2[1] ? Integer.compare(r1[1], r2[1]) : Integer.compare(r1[0], r2[0]));
2、>>,右移运算符的优先度小于+ -
3、输出一维数组、二维数组,Arrays.toString
1342. Number of Steps to Reduce a Number to Zero - Day 1-6 220913
感觉一点点都不难
刷题进度
周一 | 周二 | 周三 | 周四 | 周五 | 周六 | 周日 | 总计 |
---|---|---|---|---|---|---|---|
Day1 220913 6 | Day2 220914 2 | Day3 220916 1 | Day4 220917 2 | 13 | |||
Day5 220919 2 | Day6 220920 1 | Day7 220921 1 | Day8 220922 1 | Day9 2209023 1 | Day10 220924 2 | Day11 220925 1 | 9 |
Day 12 220926 1 | Day 13 220927 1 | Day 14 220928 1 | Day 15 220929 3 | Day 16 220930 1 | Day 17 221001 1 | Day 18 221002 1 | 9 |
Day 19 221003 1 | Day 20 221004 1 | Day 21 221005 1 | Day 22 221006 1 | Day 23 221008 1 | Day 24 221009 1 | 6 | |
Day 25 221010 2 | Day 26 221011 2 | Day 27 221012 2 | Day 28 221013 2 | Day 29 221014 2 | Day 30 221015 2 | Day 31 221016 2 | 14 |
Day 32 221021 1 | Day 33 221022 7 | Day 34 221023 2 | 10 | ||||
第43周 20221024 | Day 35 221028 2 | 2 | |||||
第44周 20221031 | 0 | ||||||
第45周 20221107 | 0 | ||||||
第46周 20221114 | 0 | ||||||
第47周 20221121 | 0 | ||||||
第48周 20221128 | 0 | ||||||
第49周 20221205 | Day 36 221206 4 | Day 37 221207 12 | Day 38 221208 10 | ||||
…… | |||||||
…… | |||||||
第X1周 | Day39 230608 2 | ||||||
碎碎念
- 从 221010 开始每天最少刷两道题争取年前可以把Top Interview Questions刷完
- 221017-221023 这周有点松懈,没做完的题数瞬移到下周,221024-221030要做18道题
- 哈哈哈,今天是20221206差不多两个月没有刷题,但是我要准备面试了,啊啊啊,我要开始刷题了
- 20230608 哈哈哈,因为找到了实习,一晃半年,就没有再刷题了,今天闲下来想做一道题