-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathroot.db.js
9206 lines (9203 loc) · 515 KB
/
root.db.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable */
export const db_collection = {
"two-sum":{
"id": "1",
"name": "two-sum",
"pre": [
{
"text": "哈希表",
"link": null,
"color": "gold"
}
],
"keyPoints": [
{
"text": "求和转换为求差",
"link": null,
"color": "blue"
},
{
"text": "借助Map结构将数组中每个元素及其索引相互对应",
"link": null,
"color": "blue"
},
{
"text": "以空间换时间,将查找时间从O(N)降低到O(1)",
"link": null,
"color": "blue"
}
],
"companies": [
{
"name": "字节跳动"
},
{
"name": "百度"
},
{
"name": "腾讯"
},
{
"name": "adobe"
},
{
"name": "airbnb"
},
{
"name": "amazon"
},
{
"name": "apple"
},
{
"name": "bloomberg"
},
{
"name": "dropbox"
},
{
"name": "facebook"
},
{
"name": "linkedin"
},
{
"name": "microsoft"
},
{
"name": "uber"
},
{
"name": "yahoo"
},
{
"name": "yelp"
}
],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/1.two-sum.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/1.two-sum.md",
"code": [
{
"language": "java",
"text": "\nfor(int i = 0; i < n; i++) {\n for(int j = 0; j < i;j ++){\n if (nums[i] + nums[j] == target) return [j, i]\n }\n}\n"
},
{
"language": "java",
"text": "\nclass Solution {\n public int[] twoSum(int[] nums, int target) {\n Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();\n for (int i = 0; i < nums.length; ++i) {\n if (hashtable.containsKey(target - nums[i])) {\n return new int[]{hashtable.get(target - nums[i]), i};\n }\n hashtable.put(nums[i], i);\n }\n return new int[0];\n }\n}\n"
},
{
"language": "js",
"text": "\n/**\n * @param {number[]} nums\n * @param {number} target\n * @return {number[]}\n */\nconst twoSum = function (nums, target) {\n const map = new Map();\n for (let i = 0; i < nums.length; i++) {\n const diff = target - nums[i];\n if (map.has(diff)) {\n return [map.get(diff), i];\n }\n map.set(nums[i], i);\n }\n};\n"
},
{
"language": "cpp",
"text": "\nclass Solution {\npublic:\n vector<int> twoSum(vector<int>& A, int target) {\n unordered_map<int, int> m;\n for (int i = 0; i < A.size(); ++i) {\n int t = target - A[i];\n if (m.count(t)) return { m[t], i };\n m[A[i]] = i;\n }\n return {};\n }\n};\n"
},
{
"language": "py",
"text": "\nclass Solution:\n def twoSum(self, nums: List[int], target: int) -> List[int]:\n hashtable = dict()\n for i, num in enumerate(nums):\n if target - num in hashtable:\n return [hashtable[target - num], i]\n hashtable[nums[i]] = i\n return []\n"
}
]
},
"add-two-numbers":{
"id": "2",
"name": "add-two-numbers",
"pre": [
{
"text": "链表",
"link": null,
"color": "magenta"
}
],
"keyPoints": [
{
"text": "解析1.链表这种数据结构的特点和使用2.用一个carried变量来实现进位的功能,每次相加之后计算carried,并用于下一位的计算",
"link": null,
"color": "blue"
}
],
"companies": [
{
"name": "阿里巴巴"
},
{
"name": "百度"
},
{
"name": "腾讯"
}
],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/2.add-two-numbers.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/2.add-two-numbers.md",
"code": [
{
"language": "java",
"text": "\nclass Solution {\n public ListNode addTwoNumbers(ListNode l1, ListNode l2) {\n ListNode dummyHead = new ListNode(0);\n ListNode cur = dummyHead;\n int carry = 0;\n\n while(l1 != null || l2 != null)\n {\n int sum = carry;\n if(l1 != null)\n {\n sum += l1.val;\n l1 = l1.next;\n }\n if(l2 != null)\n {\n sum += l2.val;\n l2 = l2.next;\n }\n // 创建新节点\n carry = sum / 10;\n cur.next = new ListNode(sum % 10);\n cur = cur.next;\n\n }\n if (carry > 0) {\n cur.next = new ListNode(carry);\n }\n return dummyHead.next;\n }\n}\n\n"
},
{
"language": "js",
"text": "\n/**\n * Definition for singly-linked list.\n * function ListNode(val) {\n * this.val = val;\n * this.next = null;\n * }\n */\n/**\n * @param {ListNode} l1\n * @param {ListNode} l2\n * @return {ListNode}\n */\nvar addTwoNumbers = function (l1, l2) {\n if (l1 === null || l2 === null) return null;\n\n // 使用dummyHead可以简化对链表的处理,dummyHead.next指向新链表\n let dummyHead = new ListNode(0);\n let cur1 = l1;\n let cur2 = l2;\n let cur = dummyHead; // cur用于计算新链表\n let carry = 0; // 进位标志\n\n while (cur1 !== null || cur2 !== null) {\n let val1 = cur1 !== null ? cur1.val : 0;\n let val2 = cur2 !== null ? cur2.val : 0;\n let sum = val1 + val2 + carry;\n let newNode = new ListNode(sum % 10); // sum%10取模结果范围为0~9,即为当前节点的值\n carry = sum >= 10 ? 1 : 0; // sum>=10,carry=1,表示有进位\n cur.next = newNode;\n cur = cur.next;\n\n if (cur1 !== null) {\n cur1 = cur1.next;\n }\n\n if (cur2 !== null) {\n cur2 = cur2.next;\n }\n }\n\n if (carry > 0) {\n // 如果最后还有进位,新加一个节点\n cur.next = new ListNode(carry);\n }\n\n return dummyHead.next;\n};\n"
},
{
"language": "cpp",
"text": "\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {\n ListNode* ret = nullptr;\n ListNode* cur = nullptr;\n int carry = 0;\n while (l1 != nullptr || l2 != nullptr || carry != 0) {\n carry += (l1 == nullptr ? 0 : l1->val) + (l2 == nullptr ? 0 : l2->val);\n auto temp = new ListNode(carry % 10);\n carry /= 10;\n if (ret == nullptr) {\n ret = temp;\n cur = ret;\n }\n else {\n cur->next = temp;\n cur = cur->next;\n }\n l1 = l1 == nullptr ? nullptr : l1->next;\n l2 = l2 == nullptr ? nullptr : l2->next;\n }\n return ret;\n }\n};\n"
},
{
"language": "py",
"text": "\nclass Solution:\n def addTwoNumbers(self, l1, l2):\n \"\"\"\n :type l1: ListNode\n :type l2: ListNode\n :rtype: ListNode\n \"\"\"\n res=ListNode(0)\n head=res\n carry=0\n while l1 or l2 or carry!=0:\n sum=carry\n if l1:\n sum+=l1.val\n l1=l1.next\n if l2:\n sum+=l2.val\n l2=l2.next\n # set value\n if sum<=9:\n res.val=sum\n carry=0\n else:\n res.val=sum%10\n carry=sum//10\n # creat new node\n if l1 or l2 or carry!=0:\n res.next=ListNode(0)\n res=res.next\n return head\n\n"
}
]
},
"longest-substring-without-repeating-characters":{
"id": "3",
"name": "longest-substring-without-repeating-characters",
"pre": [],
"keyPoints": [],
"companies": [],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/3.longest-substring-without-repeating-characters.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/3.longest-substring-without-repeating-characters.md",
"code": []
},
"median-of-two-sorted-arrays":{
"id": "4",
"name": "median-of-two-sorted-arrays",
"pre": [],
"keyPoints": [],
"companies": [],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/4.median-of-two-sorted-arrays.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/4.median-of-two-sorted-arrays.md",
"code": []
},
"longest-palindromic-substring":{
"id": "5",
"name": "longest-palindromic-substring",
"pre": [],
"keyPoints": [],
"companies": [],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/5.longest-palindromic-substring.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/5.longest-palindromic-substring.md",
"code": []
},
"container-with-most-water":{
"id": "11",
"name": "container-with-most-water",
"pre": [
{
"text": "双指针",
"link": null,
"color": "green"
}
],
"keyPoints": [
{
"text": "双指针优化时间复杂度",
"link": null,
"color": "blue"
}
],
"companies": [
{
"name": "字节跳动"
},
{
"name": "腾讯"
},
{
"name": "百度"
},
{
"name": "阿里巴巴"
}
],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/11.container-with-most-water.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/11.container-with-most-water.md",
"code": [
{
"language": "js",
"text": "\nlet max = 0;\nfor (let i = 0; i < height.length; i++) {\n for (let j = i + 1; j < height.length; j++) {\n const currentArea = Math.abs(i - j) * Math.min(height[i], height[j]);\n if (currentArea > max) {\n max = currentArea;\n }\n }\n}\nreturn max;\n"
},
{
"language": "js",
"text": "\n/**\n * @param {number[]} height\n * @return {number}\n */\nvar maxArea = function (height) {\n if (!height || height.length <= 1) return 0;\n\n let leftPos = 0;\n let rightPos = height.length - 1;\n let max = 0;\n while (leftPos < rightPos) {\n const currentArea =\n Math.abs(leftPos - rightPos) *\n Math.min(height[leftPos], height[rightPos]);\n if (currentArea > max) {\n max = currentArea;\n }\n // 更新小的\n if (height[leftPos] < height[rightPos]) {\n leftPos++;\n } else {\n // 如果相等就随便了\n rightPos--;\n }\n }\n\n return max;\n};\n"
},
{
"language": "py",
"text": "\nclass Solution:\n def maxArea(self, heights):\n l, r = 0, len(heights) - 1\n ans = 0\n while l < r:\n ans = max(ans, (r - l) * min(heights[l], heights[r]))\n if heights[r] > heights[l]:\n l += 1\n else:\n r -= 1\n return ans\n"
}
]
},
"3sum":{
"id": "15",
"name": "3sum",
"pre": [
{
"text": "排序",
"link": null,
"color": "purple"
},
{
"text": "双指针",
"link": null,
"color": "green"
},
{
"text": "分治",
"link": null,
"color": "gold"
}
],
"keyPoints": [
{
"text": "排序之后,用双指针",
"link": null,
"color": "blue"
},
{
"text": "分治",
"link": null,
"color": "blue"
}
],
"companies": [
{
"name": "阿里巴巴"
},
{
"name": "字节跳动"
}
],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/15.3sum.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/15.3sum.md",
"code": [
{
"language": "js",
"text": "\n/**\n * @param {number[]} nums\n * @return {number[][]}\n */\nvar threeSum = function (nums) {\n if (nums.length < 3) return [];\n const list = [];\n nums.sort((a, b) => a - b);\n for (let i = 0; i < nums.length; i++) {\n //nums is sorted,so it's impossible to have a sum = 0\n if (nums[i] > 0) break;\n // skip duplicated result without set\n if (i > 0 && nums[i] === nums[i - 1]) continue;\n let left = i + 1;\n let right = nums.length - 1;\n\n // for each index i\n // we want to find the triplet [i, left, right] which sum to 0\n while (left < right) {\n // since left < right, and left > i, no need to compare i === left and i === right.\n if (nums[left] + nums[right] + nums[i] === 0) {\n list.push([nums[left], nums[right], nums[i]]);\n // skip duplicated result without set\n while (nums[left] === nums[left + 1]) {\n left++;\n }\n left++;\n // skip duplicated result without set\n while (nums[right] === nums[right - 1]) {\n right--;\n }\n right--;\n continue;\n } else if (nums[left] + nums[right] + nums[i] > 0) {\n right--;\n } else {\n left++;\n }\n }\n }\n return list;\n};\n"
},
{
"language": "cpp",
"text": "\nclass Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& A) {\n sort(begin(A), end(A));\n vector<vector<int>> ans;\n int N = A.size();\n for (int i = 0; i < N - 2; ++i) {\n if (i && A[i] == A[i - 1]) continue;\n int L = i + 1, R = N - 1;\n while (L < R) {\n int sum = A[i] + A[L] + A[R];\n if (sum == 0) ans.push_back({ A[i], A[L], A[R] });\n if (sum >= 0) {\n --R;\n while (L < R && A[R] == A[R + 1]) --R;\n }\n if (sum <= 0) {\n ++L;\n while (L < R && A[L] == A[L - 1]) ++L;\n }\n }\n }\n return ans;\n }\n}\n"
}
]
},
"Letter-Combinations-of-a-Phone-Number":{
"id": "17",
"name": "Letter-Combinations-of-a-Phone-Number",
"pre": [
{
"text": "回溯",
"link": null,
"color": "green"
},
{
"text": "笛卡尔积",
"link": null,
"color": "red"
}
],
"keyPoints": [
{
"text": "回溯",
"link": null,
"color": "blue"
},
{
"text": "回溯模板",
"link": null,
"color": "blue"
}
],
"companies": [
{
"name": "阿里巴巴"
},
{
"name": "百度"
},
{
"name": "字节跳动"
},
{
"name": "腾讯"
}
],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/17.Letter-Combinations-of-a-Phone-Number.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/17.Letter-Combinations-of-a-Phone-Number.md",
"code": [
{
"language": "java",
"text": "\nclass Solution {\n\n private String letterMap[] = {\n \" \", //0\n \"\", //1\n \"abc\", //2\n \"def\", //3\n \"ghi\", //4\n \"jkl\", //5\n \"mno\", //6\n \"pqrs\", //7\n \"tuv\", //8\n \"wxyz\" //9\n };\n private ArrayList<String> res;\n public List<String> letterCombinations(String digits) {\n res = new ArrayList<String>();\n if(digits.equals(\"\"))\n {\n return res;\n }\n dfs(digits, 0, \"\");\n return res;\n }\n\n public void dfs(String digits, int index, String s)\n {\n if(index == digits.length())\n {\n res.add(s);\n return;\n }\n // 获取当前数字\n Character c = digits.charAt(index);\n // 获取数字对应字母\n String letters = letterMap[c-'0'];\n for(int i = 0 ; i < letters.length() ; i ++)\n {\n dfs(digits, index+1, s+letters.charAt(i));\n }\n }\n}\n"
},
{
"language": "js",
"text": "\n/**\n * @param {string} digits\n * @return {string[]}\n */\nconst letterCombinations = function (digits) {\n if (!digits) {\n return [];\n }\n const len = digits.length;\n const map = new Map();\n map.set(\"2\", \"abc\");\n map.set(\"3\", \"def\");\n map.set(\"4\", \"ghi\");\n map.set(\"5\", \"jkl\");\n map.set(\"6\", \"mno\");\n map.set(\"7\", \"pqrs\");\n map.set(\"8\", \"tuv\");\n map.set(\"9\", \"wxyz\");\n const result = [];\n\n function generate(i, str) {\n if (i == len) {\n result.push(str);\n return;\n }\n const tmp = map.get(digits[i]);\n for (let r = 0; r < tmp.length; r++) {\n generate(i + 1, str + tmp[r]);\n }\n }\n generate(0, \"\");\n return result;\n};\n"
},
{
"language": "cpp",
"text": "\nclass Solution {\npublic:\n string letterMap[10] = {\" \",\" \",\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqrs\",\"tuv\",\"wxyz\"};\n vector<string> res;\n vector<string> letterCombinations(string digits) {\n if(digits == \"\")\n {\n return res;\n }\n dfs(digits, 0, \"\");\n return res;\n }\n\n void dfs(string digits, int index, string s)\n {\n if(index == digits.length())\n {\n res.push_back(s);\n return;\n }\n // 获取当前数字\n char c = digits[index];\n // 获取数字对应字母\n string letters = letterMap[c-'0'];\n for(int i = 0 ; i < letters.length() ; i ++)\n {\n dfs(digits, index+1, s+letters[i]);\n }\n }\n}\n"
},
{
"language": "py",
"text": "\nclass Solution(object):\n def letterCombinations(self, digits):\n \"\"\"\n :type digits: str\n :rtype: List[str]\n \"\"\"\n if not digits:\n return []\n # 0-9\n self.d = [\" \",\" \",\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqrs\",\"tuv\",\"wxyz\"]\n self.res = []\n self.dfs(digits, 0, \"\")\n return self.res\n\n def dfs(self, digits, index, s):\n # 递归的终止条件,用index记录每次遍历到字符串的位置\n if index == len(digits):\n self.res.append(s)\n return\n # 获取当前数字\n c = digits[index]\n # print(c, int(c))\n # 获取数字对应字母\n letters = self.d[int(c)]\n # 遍历字符串\n for l in letters:\n # 调用下一层\n self.dfs(digits, index+1, s+l)\n"
},
{
"language": "py",
"text": "\n\n# 输入:\"23\"\n# 输出:[\"ad\", \"ae\", \"af\", \"bd\", \"be\", \"bf\", \"cd\", \"ce\", \"cf\"].\nclass Solution:\n def letterCombinations(self, digits: str) -> List[str]:\n mapper = [\" \", \" \", \"abc\", \"def\", \"ghi\",\n \"jkl\", \"mno\", \"pqrs\", \"tuv\", \"wxyz\"]\n @lru_cache(None)\n def backtrack(digits, start):\n if start >= len(digits):\n return ['']\n ans = []\n for i in range(start, len(digits)):\n for c in mapper[int(digits[i])]:\n # 笛卡尔积\n for p in backtrack(digits, i + 1):\n # 需要过滤诸如 \"d\", \"e\", \"f\" 等长度不符合的数据\n if start == 0:\n if len(c + p) == len(digits):\n ans.append(c + p)\n else:\n ans.append(c + p)\n return ans\n if not digits:\n return []\n return backtrack(digits, 0)\n\n"
}
]
},
"removeNthNodeFromEndofList":{
"id": "19",
"name": "removeNthNodeFromEndofList",
"pre": [
{
"text": "链表",
"link": null,
"color": "magenta"
},
{
"text": "双指针",
"link": null,
"color": "green"
}
],
"keyPoints": [
{
"text": "解析1.链表这种数据结构的特点和使用2.使用双指针3.使用一个dummyHead简化操作",
"link": null,
"color": "blue"
}
],
"companies": [
{
"name": "阿里巴巴"
},
{
"name": "百度"
},
{
"name": "腾讯"
},
{
"name": "字节跳动"
}
],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/19.removeNthNodeFromEndofList.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/19.removeNthNodeFromEndofList.md",
"code": [
{
"language": "java",
"text": "\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n * int val;\n * ListNode next;\n * ListNode(int x) { val = x; }\n * }\n */\nclass Solution {\n public ListNode removeNthFromEnd(ListNode head, int n) {\n TreeNode dummy = new TreeNode(0);\n dummy.next = head;\n TreeNode first = dummy;\n TreeNode second = dummy;\n\n if (int i=0; i<=n; i++) {\n first = first.next;\n }\n\n while (first != null) {\n first = first.next;\n second = second.next;\n }\n\n second.next = second.next.next;\n\n return dummy.next;\n }\n}\n"
},
{
"language": "js",
"text": "\n/**\n * @param {ListNode} head\n * @param {number} n\n * @return {ListNode}\n */\nvar removeNthFromEnd = function (head, n) {\n let i = -1;\n const noop = {\n next: null,\n };\n\n const dummyHead = new ListNode(); // 增加一个dummyHead 简化操作\n dummyHead.next = head;\n\n let currentP1 = dummyHead;\n let currentP2 = dummyHead;\n\n while (currentP1) {\n if (i === n) {\n currentP2 = currentP2.next;\n }\n\n if (i !== n) {\n i++;\n }\n\n currentP1 = currentP1.next;\n }\n\n currentP2.next = ((currentP2 || noop).next || noop).next;\n\n return dummyHead.next;\n};\n"
},
{
"language": "cpp",
"text": "\nclass Solution {\npublic:\n ListNode* removeNthFromEnd(ListNode* head, int n) {\n ListNode *p = head, *q = head;\n while (n--) q = q->next;\n if (!q) {\n head = head->next;\n delete p;\n return head;\n }\n while (q->next) p = p->next, q = q->next;\n q = p->next;\n p->next = q->next;\n delete q;\n return head;\n }\n};\n"
}
]
},
"valid-parentheses":{
"id": "20",
"name": "valid-parentheses",
"pre": [
{
"text": "栈",
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/basic-data-structure.md",
"color": "red"
}
],
"keyPoints": [
{
"text": "解析1.栈的基本特点和操作2.可以用数组来模拟栈比如入:push出:pop就是栈。入:push出shift就是队列。但是这种算法实现的队列在头部删除元素的时候时间复杂度比较高,具体大家可以参考一下[双端队列deque](https://zh.wikipedia.org/wiki/%E5%8F%8C%E7%AB%AF%E9%98%9F%E5%88%97)。",
"link": null,
"color": "blue"
}
],
"companies": [
{
"name": "阿里巴巴"
},
{
"name": "百度"
},
{
"name": "腾讯"
},
{
"name": "字节跳动"
},
{
"name": "airbnb"
},
{
"name": "amazon"
},
{
"name": "bloomberg"
},
{
"name": "facebook"
},
{
"name": "google"
},
{
"name": "microsoft"
},
{
"name": "twitter"
},
{
"name": "zenefits"
}
],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/20.valid-parentheses.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/20.valid-parentheses.md",
"code": [
{
"language": "java",
"text": "\nclass Solution {\n public boolean isValid(String s) {\n //1.判断空字符串\n if(s.isEmpty()) return true;\n //2.创建辅助栈\n Stack<Character> stack = new Stack<>();\n //3.仅遍历一次\n for(char c : s.toCharArray()){\n if(c == '('){\n stack.push(')');\n }else if(c == '['){\n stack.push(']');\n }else if(c == '{'){\n stack.push('}');\n }else if(stack.isEmpty() || c != stack.pop()){\n return false;\n }\n }\n //4.返回\n return stack.isEmpty();\n }\n}\n"
},
{
"language": "js",
"text": "\n/**\n * @param {string} s\n * @return {boolean}\n */\nvar isValid = function (s) {\n let valid = true;\n const stack = [];\n const mapper = {\n \"{\": \"}\",\n \"[\": \"]\",\n \"(\": \")\",\n };\n\n for (let i in s) {\n const v = s[i];\n if ([\"(\", \"[\", \"{\"].indexOf(v) > -1) {\n stack.push(v);\n } else {\n const peak = stack.pop();\n if (v !== mapper[peak]) {\n return false;\n }\n }\n }\n\n if (stack.length > 0) return false;\n\n return valid;\n};\n"
},
{
"language": "js",
"text": "\nvar isValid = function (s) {\n while (s.includes(\"[]\") || s.includes(\"()\") || s.includes(\"{}\")) {\n s = s.replace(\"[]\", \"\").replace(\"()\", \"\").replace(\"{}\", \"\");\n }\n s = s.replace(\"[]\", \"\").replace(\"()\", \"\").replace(\"{}\", \"\");\n return s.length === 0;\n};\n"
},
{
"language": "cpp",
"text": "\nclass Solution {\npublic:\n bool isValid(string s) {\n int n = s.size();\n if (n % 2 == 1) {\n return false;\n }\n\n unordered_map<char, char> pairs = {\n {')', '('},\n {']', '['},\n {'}', '{'}\n };\n stack<char> stk;\n for (char ch: s) {\n if (pairs.count(ch)) {\n if (stk.empty() || stk.top() != pairs[ch]) {\n return false;\n }\n stk.pop();\n }\n else {\n stk.push(ch);\n }\n }\n return stk.empty();\n }\n};\n"
},
{
"language": "cpp",
"text": "\nclass Solution {\npublic:\n bool isValid(string s) {\n int top = -1;\n for(int i =0;i<s.length();++i){\n if(top<0 || !isMatch(s[top], s[i])){\n ++top;\n s[top] = s[i];\n }else{\n --top;\n }\n }\n return top == -1;\n }\n bool isMatch(char c1, char c2){\n if(c1 == '(' && c2 == ')') return true;\n if(c1 == '[' && c2 == ']') return true;\n if(c1 == '{' && c2 == '}') return true;\n return false;\n }\n};\n"
},
{
"language": "py",
"text": "\n class Solution:\n def isValid(self,s):\n stack = []\n map = {\n \"{\":\"}\",\n \"[\":\"]\",\n \"(\":\")\"\n }\n for x in s:\n if x in map:\n stack.append(map[x])\n else:\n if len(stack)!=0:\n top_element = stack.pop()\n if x != top_element:\n return False\n else:\n continue\n else:\n return False\n return len(stack) == 0\n"
},
{
"language": "py",
"text": "\nclass Solution:\n def isValid(self, s):\n\n while '[]' in s or '()' in s or '{}' in s:\n s = s.replace('[]','').replace('()','').replace('{}','')\n return not len(s)\n"
}
]
},
"merge-two-sorted-lists":{
"id": "21",
"name": "merge-two-sorted-lists",
"pre": [
{
"text": "递归",
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/dynamic-programming.md",
"color": "orange"
},
{
"text": "链表",
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/basic-data-structure.md",
"color": "magenta"
}
],
"keyPoints": [
{
"text": "掌握链表数据结构",
"link": null,
"color": "blue"
},
{
"text": "考虑边界情况",
"link": null,
"color": "blue"
}
],
"companies": [
{
"name": "阿里巴巴"
},
{
"name": "字节跳动"
},
{
"name": "腾讯"
},
{
"name": "amazon"
},
{
"name": "apple"
},
{
"name": "linkedin"
},
{
"name": "microsoft"
}
],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/21.merge-two-sorted-lists.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/21.merge-two-sorted-lists.md",
"code": [
{
"language": "java",
"text": "\nclass Solution {\n public ListNode mergeTwoLists(ListNode l1, ListNode l2) {\n if (l1 == null) {\n return l2;\n }\n else if (l2 == null) {\n return l1;\n }\n else if (l1.val < l2.val) {\n l1.next = mergeTwoLists(l1.next, l2);\n return l1;\n }\n else {\n l2.next = mergeTwoLists(l1, l2.next);\n return l2;\n }\n\n }\n}\n"
},
{
"language": "java",
"text": "\nclass Solution {\n public ListNode mergeTwoLists(ListNode l1, ListNode l2) {\n ListNode prehead = new ListNode(-1);\n\n ListNode prev = prehead;\n while (l1 != null && l2 != null) {\n if (l1.val <= l2.val) {\n prev.next = l1;\n l1 = l1.next;\n } else {\n prev.next = l2;\n l2 = l2.next;\n }\n prev = prev.next;\n }\n\n // 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可\n prev.next = l1 == null ? l2 : l1;\n\n return prehead.next;\n }\n}\n"
},
{
"language": "js",
"text": "\n/**\n * Definition for singly-linked list.\n * function ListNode(val) {\n * this.val = val;\n * this.next = null;\n * }\n */\n/**\n * @param {ListNode} l1\n * @param {ListNode} l2\n * @return {ListNode}\n */\nconst mergeTwoLists = function (l1, l2) {\n if (l1 === null) {\n return l2;\n }\n if (l2 === null) {\n return l1;\n }\n if (l1.val < l2.val) {\n l1.next = mergeTwoLists(l1.next, l2);\n return l1;\n } else {\n l2.next = mergeTwoLists(l1, l2.next);\n return l2;\n }\n};\n"
},
{
"language": "js",
"text": "\nvar mergeTwoLists = function (l1, l2) {\n const prehead = new ListNode(-1);\n\n let prev = prehead;\n while (l1 != null && l2 != null) {\n if (l1.val <= l2.val) {\n prev.next = l1;\n l1 = l1.next;\n } else {\n prev.next = l2;\n l2 = l2.next;\n }\n prev = prev.next;\n }\n prev.next = l1 === null ? l2 : l1;\n\n return prehead.next;\n};\n"
},
{
"language": "cpp",
"text": "\nclass Solution {\npublic:\n ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {\n if (l1 == nullptr) {\n return l2;\n } else if (l2 == nullptr) {\n return l1;\n } else if (l1->val < l2->val) {\n l1->next = mergeTwoLists(l1->next, l2);\n return l1;\n } else {\n l2->next = mergeTwoLists(l1, l2->next);\n return l2;\n }\n }\n};\n"
},
{
"language": "cpp",
"text": "\nclass Solution {\npublic:\n ListNode* mergeTwoLists(ListNode* a, ListNode* b) {\n ListNode head, *tail = &head;\n while (a && b) {\n if (a->val <= b->val) {\n tail->next = a;\n a = a->next;\n } else {\n tail->next = b;\n b = b->next;\n }\n tail = tail->next;\n }\n tail->next = a ? a : b;\n return head.next;\n }\n};\n"
},
{
"language": "py",
"text": "\nclass Solution:\n def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:\n if not l1: return l2 # 终止条件,直到两个链表都空\n if not l2: return l1\n if l1.val <= l2.val: # 递归调用\n l1.next = self.mergeTwoLists(l1.next,l2)\n return l1\n else:\n l2.next = self.mergeTwoLists(l1,l2.next)\n return l2\n"
},
{
"language": "py",
"text": "\nclass Solution:\n def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:\n prehead = ListNode(-1)\n\n prev = prehead\n while l1 and l2:\n if l1.val <= l2.val:\n prev.next = l1\n l1 = l1.next\n else:\n prev.next = l2\n l2 = l2.next \n prev = prev.next\n\n # 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可\n prev.next = l1 if l1 is not None else l2\n\n return prehead.next\n"
}
]
},
"generate-parentheses":{
"id": "22",
"name": "generate-parentheses",
"pre": [
{
"text": "DFS",
"link": null,
"color": "red"
},
{
"text": "回溯法",
"link": null,
"color": "gold"
}
],
"keyPoints": [
{
"text": "当l<r时记得剪枝",
"link": null,
"color": "blue"
}
],
"companies": [
{
"name": "阿里巴巴"
},
{
"name": "百度"
},
{
"name": "腾讯"
},
{
"name": "字节跳动"
}
],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/22.generate-parentheses.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/22.generate-parentheses.md",
"code": [
{
"language": "js",
"text": "\n/**\n * @param {number} n\n * @return {string[]}\n * @param l 左括号已经用了几个\n * @param r 右括号已经用了几个\n * @param str 当前递归得到的拼接字符串结果\n * @param res 结果集\n */\nconst generateParenthesis = function (n) {\n const res = [];\n\n function dfs(l, r, str) {\n if (l == n && r == n) {\n return res.push(str);\n }\n // l 小于 r 时不满足条件 剪枝\n if (l < r) {\n return;\n }\n // l 小于 n 时可以插入左括号,最多可以插入 n 个\n if (l < n) {\n dfs(l + 1, r, str + \"(\");\n }\n // r < l 时 可以插入右括号\n if (r < l) {\n dfs(l, r + 1, str + \")\");\n }\n }\n dfs(0, 0, \"\");\n return res;\n};\n"
},
{
"language": "cpp",
"text": "\ns.push_back(')');\ndfs(l, r + 1, s);\ns.pop_back();\n"
},
{
"language": "cpp",
"text": "\nclass Solution {\nprivate:\n vector<string> ans;\n void generate(int leftCnt, int rightCnt, string &s) {\n if (!leftCnt && !rightCnt) {\n ans.push_back(s);\n return;\n }\n if (leftCnt) {\n s.push_back('(');\n generate(leftCnt - 1, rightCnt, s);\n s.pop_back();\n }\n if (rightCnt > leftCnt) {\n s.push_back(')');\n generate(leftCnt, rightCnt - 1, s);\n s.pop_back();\n }\n }\npublic:\n vector<string> generateParenthesis(int n) {\n string s;\n generate(n, n, s);\n return ans;\n }\n};\n"
},
{
"language": "py",
"text": "\nres = []\ndef dfs(l, r, s):\n if l > n or r > n: return\n if (l == r == n): res.append(s)\n # 剪枝,提高算法效率\n if l < r: return\n # 加一个左括号\n dfs(l + 1, r, s + '(')\n # 加一个右括号\n dfs(l, r + 1, s + ')')\ndfs(0, 0, '')\nreturn res\n"
},
{
"language": "py",
"text": "\nclass Solution:\n def generateParenthesis(self, n: int) -> List[str]:\n res = []\n def dfs(l, r, s):\n if l > n or r > n: return\n if (l == r == n): res.append(s)\n if l < r: return\n # 加一个左括号\n dfs(l + 1, r, s + '(')\n # 加一个右括号\n dfs(l, r + 1, s + ')')\n dfs(0, 0, '')\n return res\n"
}
]
},
"merge-k-sorted-lists":{
"id": "23",
"name": "merge-k-sorted-lists",
"pre": [
{
"text": "链表",
"link": null,
"color": "magenta"
},
{
"text": "归并排序",
"link": null,
"color": "cyan"
}
],
"keyPoints": [
{
"text": "分治",
"link": null,
"color": "blue"
},
{
"text": "归并排序(mergesort)",
"link": null,
"color": "blue"
}
],
"companies": [
{
"name": "阿里巴巴"
},
{
"name": "百度"
},
{
"name": "腾讯"
},
{
"name": "字节跳动"
}
],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/23.merge-k-sorted-lists.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/23.merge-k-sorted-lists.md",
"code": [
{
"language": "js",
"text": "\n/*\n * @lc app=leetcode id=23 lang=javascript\n *\n * [23] Merge k Sorted Lists\n *\n * https://leetcode.com/problems/merge-k-sorted-lists/description/\n *\n */\nfunction mergeTwoLists(l1, l2) {\n const dummyHead = {};\n let current = dummyHead;\n // l1: 1 -> 3 -> 5\n // l2: 2 -> 4 -> 6\n while (l1 !== null && l2 !== null) {\n if (l1.val < l2.val) {\n current.next = l1; // 把小的添加到结果链表\n current = current.next; // 移动结果链表的指针\n l1 = l1.next; // 移动小的那个链表的指针\n } else {\n current.next = l2;\n current = current.next;\n l2 = l2.next;\n }\n }\n\n if (l1 === null) {\n current.next = l2;\n } else {\n current.next = l1;\n }\n return dummyHead.next;\n}\n/**\n * Definition for singly-linked list.\n * function ListNode(val) {\n * this.val = val;\n * this.next = null;\n * }\n */\n/**\n * @param {ListNode[]} lists\n * @return {ListNode}\n */\nvar mergeKLists = function (lists) {\n // 图参考: https://zhuanlan.zhihu.com/p/61796021\n if (lists.length === 0) return null;\n if (lists.length === 1) return lists[0];\n if (lists.length === 2) {\n return mergeTwoLists(lists[0], lists[1]);\n }\n\n const mid = lists.length >> 1;\n const l1 = [];\n for (let i = 0; i < mid; i++) {\n l1[i] = lists[i];\n }\n\n const l2 = [];\n for (let i = mid, j = 0; i < lists.length; i++, j++) {\n l2[j] = lists[i];\n }\n\n return mergeTwoLists(mergeKLists(l1), mergeKLists(l2));\n};\n"
},
{
"language": "cpp",
"text": "\nclass Solution {\nprivate:\n ListNode* mergeTwoLists(ListNode* a, ListNode* b) {\n ListNode head(0), *tail = &head;\n while (a && b) {\n if (a->val < b->val) { tail->next = a; a = a->next; }\n else { tail->next = b; b = b->next; }\n tail = tail->next;\n }\n tail->next = a ? a : b;\n return head.next;\n }\npublic:\n ListNode* mergeKLists(vector<ListNode*>& lists) {\n if (lists.empty()) return NULL;\n for (int N = lists.size(); N > 1; N = (N + 1) / 2) {\n for (int i = 0; i < N / 2; ++i) {\n lists[i] = mergeTwoLists(lists[i], lists[N - 1 - i]);\n }\n }\n return lists[0];\n }\n};\n"
},
{
"language": "py",
"text": "\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, x):\n# self.val = x\n# self.next = None\n\nclass Solution:\n def mergeKLists(self, lists: List[ListNode]) -> ListNode:\n n = len(lists)\n\n # basic cases\n if n == 0: return None\n if n == 1: return lists[0]\n if n == 2: return self.mergeTwoLists(lists[0], lists[1])\n\n # divide and conqure if not basic cases\n mid = n // 2\n return self.mergeTwoLists(self.mergeKLists(lists[:mid]), self.mergeKLists(lists[mid:n]))\n\n\n def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:\n res = ListNode(0)\n c1, c2, c3 = l1, l2, res\n while c1 or c2:\n if c1 and c2:\n if c1.val < c2.val:\n c3.next = ListNode(c1.val)\n c1 = c1.next\n else:\n c3.next = ListNode(c2.val)\n c2 = c2.next\n c3 = c3.next\n elif c1:\n c3.next = c1\n break\n else:\n c3.next = c2\n break\n\n return res.next\n"
}
]
},
"swapNodesInPairs":{
"id": "24",
"name": "swapNodesInPairs",
"pre": [
{
"text": "链表",
"link": null,
"color": "magenta"
}
],
"keyPoints": [
{
"text": "解析1.链表这种数据结构的特点和使用2.dummyHead简化操作",
"link": null,
"color": "blue"
}
],
"companies": [
{
"name": "阿里巴巴"
},
{
"name": "腾讯"
},
{
"name": "百度"
},
{
"name": "字节跳动"
}
],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/24.swapNodesInPairs.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/24.swapNodesInPairs.md",
"code": [
{
"language": "js",
"text": "\n/**\n * Definition for singly-linked list.\n * function ListNode(val) {\n * this.val = val;\n * this.next = null;\n * }\n */\n/**\n * @param {ListNode} head\n * @return {ListNode}\n */\nvar swapPairs = function (head) {\n const dummy = new ListNode(0);\n dummy.next = head;\n let current = dummy;\n while (current.next != null && current.next.next != null) {\n // 初始化双指针\n const first = current.next;\n const second = current.next.next;\n\n // 更新双指针和 current 指针\n first.next = second.next;\n second.next = first;\n current.next = second;\n\n // 更新指针\n current = current.next.next;\n }\n return dummy.next;\n};\n"
},
{
"language": "cpp",
"text": "\nclass Solution {\npublic:\n ListNode* swapPairs(ListNode* head) {\n ListNode h, *tail = &h;\n while (head && head->next) {\n auto p = head, q = head->next;\n head = q->next;\n q->next = p;\n tail->next = q;\n tail = p;\n }\n tail->next = head;\n return h.next;\n }\n};\n"
},
{
"language": "py",
"text": "\nclass Solution:\n def swapPairs(self, head: ListNode) -> ListNode:\n \"\"\"\n 用递归实现链表相邻互换:\n 第一个节点的 next 是第三、第四个节点交换的结果,第二个节点的 next 是第一个节点;\n 第三个节点的 next 是第五、第六个节点交换的结果,第四个节点的 next 是第三个节点;\n 以此类推\n :param ListNode head\n :return ListNode\n \"\"\"\n # 如果为 None 或 next 为 None,则直接返回\n if not head or not head.next:\n return head\n\n _next = head.next\n head.next = self.swapPairs(_next.next)\n _next.next = head\n return _next\n"
}
]
},
"reverse-nodes-in-k-groups-cn":{
"id": "25",
"name": "reverse-nodes-in-k-groups-cn",
"pre": [
{
"text": "链表",
"link": null,
"color": "magenta"
}
],
"keyPoints": [
{
"text": "分析1.创建一个dummynode2.对链表以k为单位进行分组,记录每一组的起始和最后节点位置3.对每一组进行翻转,更换起始和最后的位置4.返回`dummy.next`.",
"link": null,
"color": "blue"
}
],
"companies": [
{
"name": "阿里巴巴"
},
{
"name": "腾讯"
},
{
"name": "百度"
},
{
"name": "字节跳动"
}
],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/25.reverse-nodes-in-k-groups-cn.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/25.reverse-nodes-in-k-groups-cn.md",
"code": [
{
"language": "java",
"text": "\nclass ReverseKGroupsLinkedList {\n public ListNode reverseKGroup(ListNode head, int k) {\n if (head == null || k == 1) {\n return head;\n }\n ListNode dummy = new ListNode(0);\n dummy.next = head;\n\n ListNode start = dummy;\n ListNode end = head;\n int count = 0;\n while (end != null) {\n count++;\n // group\n if (count % k == 0) {\n // reverse linked list (start, end]\n start = reverse(start, end.next);\n end = start.next;\n } else {\n end = end.next;\n }\n }\n return dummy.next;\n }\n\n /**\n * reverse linked list from range (start, end), return last node.\n * for example:\n * 0->1->2->3->4->5->6->7->8\n * | |\n * start end\n *\n * After call start = reverse(start, end)\n *\n * 0->3->2->1->4->5->6->7->8\n * | |\n * start end\n * first\n *\n */\n private ListNode reverse(ListNode start, ListNode end) {\n ListNode curr = start.next;\n ListNode prev = start;\n ListNode first = curr;\n while (curr != end){\n ListNode temp = curr.next;\n curr.next = prev;\n prev = curr;\n curr = temp;\n }\n start.next = prev;\n first.next = curr;\n return first;\n }\n}\n"
},
{
"language": "js",
"text": "\n/**\n * @param {ListNode} head\n * @param {number} k\n * @return {ListNode}\n */\nvar reverseKGroup = function (head, k) {\n // 标兵\n let dummy = new ListNode();\n dummy.next = head;\n let [start, end] = [dummy, dummy.next];\n let count = 0;\n while (end) {\n count++;\n if (count % k === 0) {\n start = reverseList(start, end.next);\n end = start.next;\n } else {\n end = end.next;\n }\n }\n return dummy.next;\n\n // 翻转stat -> end的链表\n function reverseList(start, end) {\n let [pre, cur] = [start, start.next];\n const first = cur;\n while (cur !== end) {\n let next = cur.next;\n cur.next = pre;\n pre = cur;\n cur = next;\n }\n start.next = pre;\n first.next = cur;\n return first;\n }\n};\n"
},
{
"language": "py",
"text": "\nclass Solution:\n def reverseKGroup(self, head: ListNode, k: int) -> ListNode:\n if head is None or k < 2:\n return head\n dummy = ListNode(0)\n dummy.next = head\n start = dummy\n end = head\n count = 0\n while end:\n count += 1\n if count % k == 0:\n start = self.reverse(start, end.next)\n # end 调到下一个\n end = start.next\n else:\n end = end.next\n return dummy.next\n # (start, end) 左右都开放\n\n def reverse(self, start, end):\n prev, curr = start, start.next\n first = curr\n # 反转\n while curr != end:\n next = curr.next\n curr.next = prev\n prev = curr\n curr = next\n # 将反转后的链表添加到原链表中\n start.next = prev\n first.next = end\n # 返回反转前的头, ��就是反转后的尾部\n return first\n\n"
},
{
"language": "py",
"text": "\n\nclass Solution:\n def reverseKGroup(self, head: ListNode, k: int) -> ListNode:\n if head is None or k < 2:\n return head\n dummy = ListNode(0)\n dummy.next = head\n pre = dummy\n cur = head\n count = 0\n while cur:\n count += 1\n if count % k == 0:\n pre = self.reverse(pre, cur.next)\n # end 调到下一个位置\n cur = pre.next\n else:\n cur = cur.next\n return dummy.next\n # (p1, p4) 左右都开放\n\n def reverse(self, p1, p4):\n prev, curr = p1, p1.next\n p2 = curr\n # 反转\n while curr != p4:\n next = curr.next\n curr.next = prev\n prev = curr\n curr = next\n # 将反转后的链表添加到原链表中\n # prev 相当于 p3\n p1.next = prev\n p2.next = p4\n # 返回反转前的头, 也就是反转后的尾部\n return p2\n\n# @lc code=end\n\n"
}
]
},
"reverse-nodes-in-k-groups":{
"id": "25",
"name": "reverse-nodes-in-k-groups",
"pre": [
{
"text": "链表",
"link": null,
"color": "magenta"
}
],
"keyPoints": [
{
"text": "分析1.创建一个dummynode2.对链表以k为单位进行分组,记录每一组的起始和最后节点位置3.对每一组进行翻转,更换起始和最后的位置4.返回`dummy.next`.",
"link": null,
"color": "blue"
}
],
"companies": [
{
"name": "阿里巴巴"
},
{
"name": "腾讯"
},
{
"name": "百度"
},
{
"name": "字节跳动"
}
],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/25.reverse-nodes-in-k-groups.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/25.reverse-nodes-in-k-groups.md",
"code": [
{
"language": "java",
"text": "\nclass ReverseKGroupsLinkedList {\n public ListNode reverseKGroup(ListNode head, int k) {\n if (head == null || k == 1) {\n return head;\n }\n ListNode dummy = new ListNode(0);\n dummy.next = head;\n\n ListNode start = dummy;\n ListNode end = head;\n int count = 0;\n while (end != null) {\n count++;\n // group\n if (count % k == 0) {\n // reverse linked list (start, end]\n start = reverse(start, end.next);\n end = start.next;\n } else {\n end = end.next;\n }\n }\n return dummy.next;\n }\n\n /**\n * reverse linked list from range (start, end), return last node.\n * for example:\n * 0->1->2->3->4->5->6->7->8\n * | |\n * start end\n *\n * After call start = reverse(start, end)\n *\n * 0->3->2->1->4->5->6->7->8\n * | |\n * start end\n * first\n *\n */\n private ListNode reverse(ListNode start, ListNode end) {\n ListNode curr = start.next;\n ListNode prev = start;\n ListNode first = curr;\n while (curr != end){\n ListNode temp = curr.next;\n curr.next = prev;\n prev = curr;\n curr = temp;\n }\n start.next = prev;\n first.next = curr;\n return first;\n }\n}\n"
},
{
"language": "js",
"text": "\n/**\n * @param {ListNode} head\n * @param {number} k\n * @return {ListNode}\n */\nvar reverseKGroup = function (head, k) {\n // 标兵\n let dummy = new ListNode();\n dummy.next = head;\n let [start, end] = [dummy, dummy.next];\n let count = 0;\n while (end) {\n count++;\n if (count % k === 0) {\n start = reverseList(start, end.next);\n end = start.next;\n } else {\n end = end.next;\n }\n }\n return dummy.next;\n\n // 翻转stat -> end的链表\n function reverseList(start, end) {\n let [pre, cur] = [start, start.next];\n const first = cur;\n while (cur !== end) {\n let next = cur.next;\n cur.next = pre;\n pre = cur;\n cur = next;\n }\n start.next = pre;\n first.next = cur;\n return first;\n }\n};\n"
},
{
"language": "py",
"text": "\nclass Solution:\n # 翻转一个子链表,并且返回新的头与尾\n def reverse(self, head: ListNode, tail: ListNode, terminal):\n cur = head\n pre = None\n while cur != terminal:\n next = cur.next\n cur.next = pre\n\n pre = cur\n cur = next\n return tail, head\n\n def reverseKGroup(self, head: ListNode, k: int) -> ListNode:\n ans = ListNode()\n ans.next = head\n pre = ans\n\n while head:\n tail = pre\n # 查看剩余部分长度是否大于等于 k\n for i in range(k):\n tail = tail.next\n if not tail:\n return ans.next\n next = tail.next\n head, tail = self.reverse(head, tail, tail.next)\n # 把子链表重新接回原链表\n pre.next = head\n tail.next = next\n pre = tail\n head = next\n\n return ans.next\n\n"
},
{
"language": "py",
"text": "\n\nclass Solution:\n def reverseKGroup(self, head: ListNode, k: int) -> ListNode:\n if head is None or k < 2:\n return head\n dummy = ListNode(0)\n dummy.next = head\n pre = dummy\n cur = head\n count = 0\n while cur:\n count += 1\n if count % k == 0:\n pre = self.reverse(pre, cur.next)\n # end 调到下一个位置\n cur = pre.next\n else:\n cur = cur.next\n return dummy.next\n # (p1, p4) 左右都开放\n\n def reverse(self, p1, p4):\n prev, curr = p1, p1.next\n p2 = curr\n # 反转\n while curr != p4:\n next = curr.next\n curr.next = prev\n prev = curr\n curr = next\n # 将反转后的链表添加到原链表中\n # prev 相当于 p3\n p1.next = prev\n p2.next = p4\n # 返回反转前的头, 也就是反转后的尾部\n return p2\n\n# @lc code=end\n\n"
}
]
},
"remove-duplicates-from-sorted-array":{
"id": "26",
"name": "remove-duplicates-from-sorted-array",
"pre": [
{
"text": "数组",
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/basic-data-structure.md",
"color": "purple"
},
{
"text": "双指针",
"link": null,
"color": "green"
}
],
"keyPoints": [
{
"text": "双指针这道题如果不要求,O(n)的时间复杂度,O(1)的空间复杂度的话,会很简单。但是这道题是要求的,这种题的思路一般都是采用双指针",
"link": null,
"color": "blue"
},
{
"text": "如果是数据是无序的,就不可以用这种方式了,从这里也可以看出排序在算法中的基础性和重要性。",
"link": null,
"color": "blue"
},
{
"text": "注意nums为空时的边界条件。",
"link": null,
"color": "blue"
}
],
"companies": [
{
"name": "阿里巴巴"
},
{
"name": "腾讯"
},
{
"name": "百度"
},
{
"name": "字节跳动"
},
{
"name": "bloomberg"
},
{
"name": "facebook"
},
{
"name": "microsoft"
}
],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/26.remove-duplicates-from-sorted-array.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/26.remove-duplicates-from-sorted-array.md",
"code": [
{
"language": "java",
"text": "\n public int removeDuplicates(int[] nums) {\n if(nums == null || nums.length == 0) return 0;\n int p = 0;\n int q = 1;\n while(q < nums.length){\n if(nums[p] != nums[q]){\n nums[p + 1] = nums[q];\n p++;\n }\n q++;\n }\n return p + 1;\n}\n"
},
{
"language": "js",
"text": "\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar removeDuplicates = function (nums) {\n const size = nums.length;\n if (size == 0) return 0;\n let slowP = 0;\n for (let fastP = 0; fastP < size; fastP++) {\n if (nums[fastP] !== nums[slowP]) {\n slowP++;\n nums[slowP] = nums[fastP];\n }\n }\n return slowP + 1;\n};\n"
},
{
"language": "cpp",
"text": "\nclass Solution {\npublic:\n int removeDuplicates(vector<int>& nums) {\n if(nums.empty()) return 0;\n int fast,slow;\n fast=slow=0;\n while(fast!=nums.size()){\n if(nums[fast]==nums[slow]) fast++;\n else {\n slow++;\n nums[slow]=nums[fast];\n fast++;\n }\n }\n return slow+1;\n }\n};\n"
},
{
"language": "py",
"text": "\nclass Solution:\n def removeDuplicates(self, nums: List[int]) -> int:\n if nums:\n slow = 0\n for fast in range(1, len(nums)):\n if nums[fast] != nums[slow]:\n slow += 1\n nums[slow] = nums[fast]\n return slow + 1\n else:\n return 0\n"
}
]
},
"divide-two-integers":{
"id": "29",
"name": "divide-two-integers",
"pre": [],
"keyPoints": [],
"companies": [],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/29.divide-two-integers.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/29.divide-two-integers.md",
"code": []
},
"substring-with-concatenation-of-all-words":{
"id": "30",
"name": "substring-with-concatenation-of-all-words",
"pre": [],
"keyPoints": [],
"companies": [],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/30.substring-with-concatenation-of-all-words.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/30.substring-with-concatenation-of-all-words.md",
"code": []
},
"next-permutation":{
"id": "31",
"name": "next-permutation",
"pre": [],
"keyPoints": [],
"companies": [],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/31.next-permutation.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/31.next-permutation.md",
"code": []
},
"longest-valid-parentheses":{
"id": "32",
"name": "longest-valid-parentheses",
"pre": [],
"keyPoints": [],
"companies": [],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/32.longest-valid-parentheses.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/32.longest-valid-parentheses.md",
"code": []
},
"search-in-rotated-sorted-array":{
"id": "33",
"name": "search-in-rotated-sorted-array",
"pre": [],
"keyPoints": [],
"companies": [],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/33.search-in-rotated-sorted-array.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/33.search-in-rotated-sorted-array.md",
"code": []
},
"combination-sum":{
"id": "39",
"name": "combination-sum",
"pre": [],
"keyPoints": [],
"companies": [],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/39.combination-sum.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/39.combination-sum.md",
"code": []
},
"combination-sum-ii":{
"id": "40",
"name": "combination-sum-ii",
"pre": [],
"keyPoints": [],
"companies": [],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/40.combination-sum-ii.md",
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/40.combination-sum-ii.md",
"code": []
},
"trapping-rain-water":{
"id": "42",
"name": "trapping-rain-water",
"pre": [],
"keyPoints": [],
"companies": [],
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/42.trapping-rain-water.md",