Skip to content

Commit 2cdde70

Browse files
committed
Mar-8
1 parent 106a86a commit 2cdde70

10 files changed

+237
-248
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
4444
| [94%] | [neetcode150.md](./list/neetcode150.md) | 141/150 | 1 vip |
4545
| [] | [9c-advanced.md](./list/9c-advanced.md) | 75/93 | 18 vips |
4646
| [] | [9c-dp.md](./list/9c-dp.md) | 42/45 | 3 vips |
47+
| [] | [9c-top.md](./list/9c-top.md) | 55/57 | 2 vips |
4748
| [] | [blind75.md](./list/blind75.md) | 76/77 | 1 vip |
4849
| [] | [geekbang.md](./list/geekbang.md) | 55/55 | - |
4950
| [] | [grind102.md](./list/grind102.md) | 101/102 | 1 vip |
@@ -52,7 +53,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5253
| [] | [leetcode-top-100-liked.md](./list/leetcode-top-100-liked.md) | 100/100 | - |
5354
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
5455
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/129 | 3 vips |
55-
| [🔲] | [9c-top.md](./list/9c-top.md) | 50/56 | 2 vips |
5656
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 47/2208 | 3 vips |
5757

5858
**Solved**: 576 problems
@@ -100,9 +100,10 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
100100

101101
## Math
102102

103-
| Link | Problem(35) | Solution | Tag | Time | Space | Ref |
103+
| Link | Problem(37) | Solution | Tag | Time | Space | Ref |
104104
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
105105
| [Leetcode-415](https://leetcode.com/problems/add-strings/) | Add Strings | [c++](./leetcode/415.add-strings.cpp), [python3](./leetcode/415.add-strings.py) | Math | O\(N\) | O\(1\) | - |
106+
| [Leetcode-2](https://leetcode.com/problems/add-two-numbers/) | Add Two Numbers | [c++](./leetcode/2.add-two-numbers.cpp), [python3](./leetcode/2.add-two-numbers.py) | Math | O\(N \+ M\) | O\(1\) | - |
106107
| [Leetcode-504](https://leetcode.com/problems/base-7/) | Base 7 | [c++](./leetcode/504.base-7.cpp), [python3](./leetcode/504.base-7.py) | Math | O\(N\) | O\(1\) | - |
107108
| [Leetcode-224](https://leetcode.com/problems/basic-calculator/) | Basic Calculator | [c++](./leetcode/224.basic-calculator.cpp), [python3](./leetcode/224.basic-calculator.py) | Math | O\(N\) | O\(N\) | - |
108109
| [Leetcode-227](https://leetcode.com/problems/basic-calculator-ii/) | Basic Calculator II | [c++](./leetcode/227.basic-calculator-ii.cpp), [python3](./leetcode/227.basic-calculator-ii.py) | Math | O\(N\) | O\(N\) | - |
@@ -126,6 +127,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
126127
| [Leetcode-973](https://leetcode.com/problems/k-closest-points-to-origin/) | K Closest Points To Origin | [c++](./leetcode/973.k-closest-points-to-origin.cpp), [python3](./leetcode/973.k-closest-points-to-origin.py) | Math | O\(NlogN\) | O\(K\) | - |
127128
| [Leetcode-149](https://leetcode.com/problems/max-points-on-a-line/) | Max Points On A Line | [c++](./leetcode/149.max-points-on-a-line.cpp), [python3](./leetcode/149.max-points-on-a-line.py) | Math | O\(N^2\) | O\(N\) | - |
128129
| [Leetcode-462](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | Minimum Moves To Equal Array Elements II | [c++](./leetcode/462.minimum-moves-to-equal-array-elements-ii.cpp), [python3](./leetcode/462.minimum-moves-to-equal-array-elements-ii.py) | Math | O\(NlogN\) | O\(1\) | - |
130+
| [Leetcode-43](https://leetcode.com/problems/multiply-strings/) | Multiply Strings | [c++](./leetcode/43.multiply-strings.cpp), [python3](./leetcode/43.multiply-strings.py) | Math | O\(NM\) | O\(N\+M\) | - |
129131
| [Leetcode-326](https://leetcode.com/problems/power-of-three/) | Power Of Three | [c++](./leetcode/326.power-of-three.cpp), [python3](./leetcode/326.power-of-three.py) | Math | O\(1\) | O\(1\) | - |
130132
| [Leetcode-50](https://leetcode.com/problems/powx-n/) | Powx N | [c++](./leetcode/50.powx-n.cpp), [python3](./leetcode/50.powx-n.py) | Math | O\(logN\) | O\(1\) | - |
131133
| [Leetcode-1352](https://leetcode.com/problems/product-of-the-last-k-numbers/) | Product Of The Last K Numbers | [c++](./leetcode/1352.product-of-the-last-k-numbers.cpp), [python3](./leetcode/1352.product-of-the-last-k-numbers.py) | Math | O\(1\) | O\(N\) | - |
@@ -189,7 +191,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
189191

190192
## Simulation
191193

192-
| Link | Problem(24) | Solution | Tag | Time | Space | Ref |
194+
| Link | Problem(25) | Solution | Tag | Time | Space | Ref |
193195
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
194196
| [Leetcode-67](https://leetcode.com/problems/add-binary/) | Add Binary | [c++](./leetcode/67.add-binary.cpp), [python3](./leetcode/67.add-binary.py) | Simulation | O\(M\+N\) | O\(1\) | - |
195197
| [Leetcode-415](https://leetcode.com/problems/add-strings/) | Add Strings | [c++](./leetcode/415.add-strings.cpp), [python3](./leetcode/415.add-strings.py) | Simulation | O\(N\) | O\(1\) | - |
@@ -198,6 +200,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
198200
| [Leetcode-3174](https://leetcode.com/problems/clear-digits/) | Clear Digits | [c++](./leetcode/3174.clear-digits.cpp), [python3](./leetcode/3174.clear-digits.py) | Simulation | O\(N\) | O\(N\) | - |
199201
| [Leetcode-3160](https://leetcode.com/problems/find-the-number-of-distinct-colors-among-the-balls/) | Find The Number Of Distinct Colors Among The Balls | [c++](./leetcode/3160.find-the-number-of-distinct-colors-among-the-balls.cpp), [python3](./leetcode/3160.find-the-number-of-distinct-colors-among-the-balls.py) | Simulation | O\(N\) | O\(N\) | - |
200202
| [Leetcode-3066](https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-ii/) | Minimum Operations To Exceed Threshold Value II | [c++](./leetcode/3066.minimum-operations-to-exceed-threshold-value-ii.cpp), [python3](./leetcode/3066.minimum-operations-to-exceed-threshold-value-ii.py) | Simulation | O\(NlogN\) | O\(N\) | - |
203+
| [Leetcode-43](https://leetcode.com/problems/multiply-strings/) | Multiply Strings | [c++](./leetcode/43.multiply-strings.cpp), [python3](./leetcode/43.multiply-strings.py) | Simulation | O\(NM\) | O\(N\+M\) | - |
201204
| [Leetcode-2161](https://leetcode.com/problems/partition-array-according-to-given-pivot/) | Partition Array According To Given Pivot | [c++](./leetcode/2161.partition-array-according-to-given-pivot.cpp), [python3](./leetcode/2161.partition-array-according-to-given-pivot.py) | Simulation | O\(N\) | O\(N\) | - |
202205
| [Leetcode-1910](https://leetcode.com/problems/remove-all-occurrences-of-a-substring/) | Remove All Occurrences Of A Substring | [c++](./leetcode/1910.remove-all-occurrences-of-a-substring.cpp), [python3](./leetcode/1910.remove-all-occurrences-of-a-substring.py) | Simulation | O\(NM\) | O\(1\) | - |
203206
| [Leetcode-566](https://leetcode.com/problems/reshape-the-matrix/) | Reshape The Matrix | [c++](./leetcode/566.reshape-the-matrix.cpp), [python3](./leetcode/566.reshape-the-matrix.py) | Simulation | O\(MN\) | O\(1\) | - |
@@ -441,8 +444,9 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
441444

442445
## Linked List
443446

444-
| Link | Problem(20) | Solution | Tag | Time | Space | Ref |
447+
| Link | Problem(21) | Solution | Tag | Time | Space | Ref |
445448
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
449+
| [Leetcode-2](https://leetcode.com/problems/add-two-numbers/) | Add Two Numbers | [c++](./leetcode/2.add-two-numbers.cpp), [python3](./leetcode/2.add-two-numbers.py) | Linked List | O\(N \+ M\) | O\(1\) | - |
446450
| [Leetcode-432](https://leetcode.com/problems/all-oone-data-structure/) | All Oone Data Structure | [c++](./leetcode/432.all-oone-data-structure.cpp), [python3](./leetcode/432.all-oone-data-structure.py) | Linked List | O\(1\) | O\(N\) | - |
447451
| [Leetcode-109](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | Convert Sorted List To Binary Search Tree | [c++](./leetcode/109.convert-sorted-list-to-binary-search-tree.cpp), [python3](./leetcode/109.convert-sorted-list-to-binary-search-tree.py) | Linked List | O\(NlogN\) | O\(logN\) | - |
448452
| [Leetcode-114](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) | Flatten Binary Tree To Linked List | [c++](./leetcode/114.flatten-binary-tree-to-linked-list.cpp), [python3](./leetcode/114.flatten-binary-tree-to-linked-list.py) | Linked List | O\(N\) | O\(H\) | - |
@@ -1184,11 +1188,10 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
11841188

11851189
## Other
11861190

1187-
| Link | Problem(110) | Solution | Tag | Time | Space | Ref |
1191+
| Link | Problem(108) | Solution | Tag | Time | Space | Ref |
11881192
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
11891193
| [Leetcode-16](https://leetcode.com/problems/3sum-closest/) | 3Sum Closest | [c++](./leetcode/16.3sum-closest.cpp), [python3](./leetcode/16.3sum-closest.py) | Other | \- | \- | - |
11901194
| [Leetcode-454](https://leetcode.com/problems/4sum-ii/) | 4Sum II | [c++](./leetcode/454.4sum-ii.cpp) | Other | \- | \- | - |
1191-
| [Leetcode-2](https://leetcode.com/problems/add-two-numbers/) | Add Two Numbers | [c++](./leetcode/2.add-two-numbers.cpp), [python3](./leetcode/2.add-two-numbers.py) | Other | \- | \- | - |
11921195
| [Leetcode-269](https://leetcode.com/problems/alien-dictionary/) | Alien Dictionary | [python3](./leetcode/269.alien-dictionary.py) | Other | \- | \- | - |
11931196
| [Leetcode-561](https://leetcode.com/problems/array-partition-i/) | Array Partition I | [c++](./leetcode/561.array-partition-i.cpp) | Other | \- | \- | - |
11941197
| [Leetcode-844](https://leetcode.com/problems/backspace-string-compare/) | Backspace String Compare | [c++](./leetcode/844.backspace-string-compare.cpp), [python3](./leetcode/844.backspace-string-compare.py) | Other | \- | \- | - |
@@ -1243,7 +1246,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
12431246
| [Leetcode-599](https://leetcode.com/problems/minimum-index-sum-of-two-lists/) | Minimum Index Sum Of Two Lists | [c++](./leetcode/599.minimum-index-sum-of-two-lists.cpp) | Other | \- | \- | - |
12441247
| [Leetcode-283](https://leetcode.com/problems/move-zeroes/) | Move Zeroes | [c++](./leetcode/283.move-zeroes.cpp), [python3](./leetcode/283.move-zeroes.py) | Other | \- | \- | - |
12451248
| [Leetcode-346](https://leetcode.com/problems/moving-average-from-data-stream/) | Moving Average From Data Stream | [c++](./leetcode/346.moving-average-from-data-stream.cpp), [python3](./leetcode/346.moving-average-from-data-stream.py) | Other | \- | \- | - |
1246-
| [Leetcode-43](https://leetcode.com/problems/multiply-strings/) | Multiply Strings | [c++](./leetcode/43.multiply-strings.cpp), [python3](./leetcode/43.multiply-strings.py) | Other | \- | \- | - |
12471249
| [Leetcode-31](https://leetcode.com/problems/next-permutation/) | Next Permutation | [c++](./leetcode/31.next-permutation.cpp), [python3](./leetcode/31.next-permutation.py) | Other | \- | \- | - |
12481250
| [Leetcode-447](https://leetcode.com/problems/number-of-boomerangs/) | Number Of Boomerangs | [c++](./leetcode/447.number-of-boomerangs.cpp), [python3](./leetcode/447.number-of-boomerangs.py) | Other | \- | \- | - |
12491251
| [Leetcode-323](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/) | Number Of Connected Components In An Undirected Graph | [c++](./leetcode/323.number-of-connected-components-in-an-undirected-graph.cpp), [python3](./leetcode/323.number-of-connected-components-in-an-undirected-graph.py) | Other | \- | \- | - |

leetcode/2.add-two-numbers.cpp

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
1-
/*
2-
* @lc app=leetcode id=2 lang=cpp
3-
*
4-
* [2] Add Two Numbers
5-
*
6-
* https://leetcode.com/problems/add-two-numbers/description/
7-
*
8-
* algorithms
9-
* Medium (33.44%)
10-
* Total Accepted: 1.4M
11-
* Total Submissions: 4.2M
12-
* Testcase Example: '[2,4,3]\n[5,6,4]'
13-
*
14-
* You are given two non-empty linked lists representing two non-negative
15-
* integers. The digits are stored in reverse order and each of their nodes
16-
* contain a single digit. Add the two numbers and return it as a linked list.
17-
*
18-
* You may assume the two numbers do not contain any leading zero, except the
19-
* number 0 itself.
20-
*
21-
* Example:
22-
*
23-
*
24-
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
25-
* Output: 7 -> 0 -> 8
26-
* Explanation: 342 + 465 = 807.
27-
*
28-
*
29-
*/
1+
// Tag: Linked List, Math, Recursion
2+
// Time: O(N + M)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
8+
// You may assume the two numbers do not contain any leading zero, except the number 0 itself.
9+
//  
10+
// Example 1:
11+
//
12+
//
13+
// Input: l1 = [2,4,3], l2 = [5,6,4]
14+
// Output: [7,0,8]
15+
// Explanation: 342 + 465 = 807.
16+
//
17+
// Example 2:
18+
//
19+
// Input: l1 = [0], l2 = [0]
20+
// Output: [0]
21+
//
22+
// Example 3:
23+
//
24+
// Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
25+
// Output: [8,9,9,9,0,0,0,1]
26+
//
27+
//  
28+
// Constraints:
29+
//
30+
// The number of nodes in each linked list is in the range [1, 100].
31+
// 0 <= Node.val <= 9
32+
// It is guaranteed that the list represents a number that does not have leading zeros.
33+
//
34+
//
35+
3036
/**
31-
* Definition for singly-linked list.
3237
* Definition for singly-linked list.
3338
* struct ListNode {
3439
* int val;
@@ -41,25 +46,21 @@
4146
class Solution {
4247
public:
4348
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
44-
49+
ListNode *cur1 = l1;
50+
ListNode *cur2 = l2;
51+
ListNode dummy = ListNode(0);
52+
ListNode *cur = &dummy;
4553
int extra = 0;
46-
ListNode *root = new ListNode(0);
47-
ListNode *pre = root;
48-
49-
while (l1 || l2 || extra)
50-
{
51-
int digit1 = l1 ? l1->val: 0;
52-
int digit2 = l2 ? l2->val: 0;
53-
int number = digit1 + digit2 + extra;
54-
55-
extra = number / 10;
56-
pre->next = new ListNode(number % 10);
57-
58-
l1 = l1 ? l1->next : l1;
59-
l2 = l2 ? l2->next : l2;
60-
pre = pre->next;
54+
while (cur1 || cur2 || extra) {
55+
int v1 = cur1 ? cur1->val: 0;
56+
int v2 = cur2 ? cur2->val: 0;
57+
int val = v1 + v2 + extra;
58+
extra = val / 10;
59+
cur->next = new ListNode(val % 10);
60+
cur = cur->next;
61+
cur1 = cur1 ? cur1->next: nullptr;
62+
cur2 = cur2 ? cur2->next: nullptr;
6163
}
62-
63-
return root->next;
64+
return dummy.next;
6465
}
65-
};
66+
};

leetcode/2.add-two-numbers.py

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,58 @@
1-
#
2-
# @lc app=leetcode id=2 lang=python3
3-
#
4-
# [2] Add Two Numbers
5-
#
6-
# https://leetcode.com/problems/add-two-numbers/description/
7-
#
8-
# algorithms
9-
# Medium (33.44%)
10-
# Total Accepted: 1.4M
11-
# Total Submissions: 4.2M
12-
# Testcase Example: '[2,4,3]\n[5,6,4]'
13-
#
14-
# You are given two non-empty linked lists representing two non-negative
15-
# integers. The digits are stored in reverse order and each of their nodes
16-
# contain a single digit. Add the two numbers and return it as a linked list.
17-
#
18-
# You may assume the two numbers do not contain any leading zero, except the
19-
# number 0 itself.
20-
#
21-
# Example:
22-
#
23-
#
24-
# Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
25-
# Output: 7 -> 0 -> 8
26-
# Explanation: 342 + 465 = 807.
27-
#
28-
#
29-
#
1+
# Tag: Linked List, Math, Recursion
2+
# Time: O(N + M)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
8+
# You may assume the two numbers do not contain any leading zero, except the number 0 itself.
9+
#  
10+
# Example 1:
11+
#
12+
#
13+
# Input: l1 = [2,4,3], l2 = [5,6,4]
14+
# Output: [7,0,8]
15+
# Explanation: 342 + 465 = 807.
16+
#
17+
# Example 2:
18+
#
19+
# Input: l1 = [0], l2 = [0]
20+
# Output: [0]
21+
#
22+
# Example 3:
23+
#
24+
# Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
25+
# Output: [8,9,9,9,0,0,0,1]
26+
#
27+
#  
28+
# Constraints:
29+
#
30+
# The number of nodes in each linked list is in the range [1, 100].
31+
# 0 <= Node.val <= 9
32+
# It is guaranteed that the list represents a number that does not have leading zeros.
33+
#
34+
#
35+
3036
# Definition for singly-linked list.
3137
# class ListNode:
3238
# def __init__(self, val=0, next=None):
3339
# self.val = val
3440
# self.next = next
3541
class Solution:
36-
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
37-
pre = head = ListNode()
38-
addition = 0
39-
40-
while (l1 is not None or l2 is not None or addition > 0):
41-
num1 = 0 if l1 is None else l1.val
42-
num2 = 0 if l2 is None else l2.val
43-
44-
total = num1 + num2 + addition
45-
addition = total // 10
46-
pre.next = ListNode(val = total % 10)
47-
48-
pre = pre.next
49-
l1 = None if l1 is None else l1.next
50-
l2 = None if l2 is None else l2.next
51-
52-
return head.next
42+
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
43+
cur1 = l1
44+
cur2 = l2
45+
extra = 0
46+
dummy = ListNode()
47+
cur = dummy
48+
while cur1 or cur2 or extra > 0:
49+
v1 = cur1.val if cur1 else 0
50+
v2 = cur2.val if cur2 else 0
51+
val = v1 + v2 + extra
52+
extra = val // 10
53+
cur.next = ListNode(val % 10)
54+
cur = cur.next
55+
cur1 = cur1.next if cur1 else None
56+
cur2 = cur2.next if cur2 else None
57+
58+
return dummy.next

leetcode/2379.minimum-recolors-to-get-k-consecutive-black-blocks.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Space: O(1)
44
// Ref: -
55
// Note: -
6+
// Video: https://youtu.be/2beK-k1hnmA
67

78
// You are given a 0-indexed string blocks of length n, where blocks[i] is either 'W' or 'B', representing the color of the ith block. The characters 'W' and 'B' denote the colors white and black, respectively.
89
// You are also given an integer k, which is the desired number of consecutive black blocks.
@@ -44,15 +45,11 @@ class Solution {
4445
int black = 0;
4546
int count = 0;
4647
for (int i = 0; i < n; i++) {
47-
if (blocks[i] == 'B') {
48-
count += 1;
49-
}
48+
count += blocks[i] == 'B';
5049

5150
if (i >= k - 1) {
5251
black = max(black, count);
53-
if (blocks[i - k + 1] == 'B') {
54-
count -= 1;
55-
}
52+
count -= blocks[i - k + 1] == 'B';
5653
}
5754
}
5855

leetcode/2379.minimum-recolors-to-get-k-consecutive-black-blocks.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Space: O(1)
44
# Ref: -
55
# Note: -
6+
# Video: https://youtu.be/2beK-k1hnmA
67

78
# You are given a 0-indexed string blocks of length n, where blocks[i] is either 'W' or 'B', representing the color of the ith block. The characters 'W' and 'B' denote the colors white and black, respectively.
89
# You are also given an integer k, which is the desired number of consecutive black blocks.
@@ -40,15 +41,12 @@
4041
class Solution:
4142
def minimumRecolors(self, blocks: str, k: int) -> int:
4243
n = len(blocks)
43-
black = 0
4444
count = 0
45+
res = 0
4546
for i in range(n):
46-
if blocks[i] == 'B':
47-
count += 1
48-
47+
count += blocks[i] == 'B'
4948
if i >= k - 1:
50-
black = max(black, count)
51-
if blocks[i - k + 1] == 'B':
52-
count -= 1
49+
res = max(res, count)
50+
count -= blocks[i - k + 1] == 'B'
5351

54-
return k - black
52+
return k - res

0 commit comments

Comments
 (0)