Skip to content

Commit a3f88e3

Browse files
committed
feat: update solutions to lcci problems
* lcci No.01.02.Check Permutation * lcci No.02.05.Sum List
1 parent 9b8b726 commit a3f88e3

File tree

17 files changed

+237
-87
lines changed

17 files changed

+237
-87
lines changed

lcci/01.01.Is Unique/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<p><strong>示例 1:</strong></p>
1111

12-
<pre><strong>输入:</strong> s= &quot;leetcode&quot;
12+
<pre><strong>输入:</strong> s = &quot;leetcode&quot;
1313
<strong>输出:</strong> false
1414
</pre>
1515

lcci/01.02.Check Permutation/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,28 @@ func CheckPermutation(s1 string, s2 string) bool {
119119
}
120120
```
121121

122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
bool CheckPermutation(string s1, string s2) {
128+
int n1 = s1.size();
129+
int n2 = s2.size();
130+
if (n1 != n2) return 0;
131+
vector<int> counter(128);
132+
for (int i = 0; i < n1; ++i)
133+
{
134+
++counter[s1[i]];
135+
--counter[s2[i]];
136+
}
137+
for (int v : counter)
138+
if (v) return 0;
139+
return 1;
140+
}
141+
};
142+
```
143+
122144
### **...**
123145
124146
```

lcci/01.02.Check Permutation/README_EN.md

+22
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,28 @@ func CheckPermutation(s1 string, s2 string) bool {
117117
}
118118
```
119119

120+
### **C++**
121+
122+
```cpp
123+
class Solution {
124+
public:
125+
bool CheckPermutation(string s1, string s2) {
126+
int n1 = s1.size();
127+
int n2 = s2.size();
128+
if (n1 != n2) return 0;
129+
vector<int> counter(128);
130+
for (int i = 0; i < n1; ++i)
131+
{
132+
++counter[s1[i]];
133+
--counter[s2[i]];
134+
}
135+
for (int v : counter)
136+
if (v) return 0;
137+
return 1;
138+
}
139+
};
140+
```
141+
120142
### **...**
121143
122144
```
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
bool CheckPermutation(string s1, string s2) {
4+
int n1 = s1.size();
5+
int n2 = s2.size();
6+
if (n1 != n2) return 0;
7+
vector<int> counter(128);
8+
for (int i = 0; i < n1; ++i)
9+
{
10+
++counter[s1[i]];
11+
--counter[s2[i]];
12+
}
13+
for (int v : counter)
14+
if (v) return 0;
15+
return 1;
16+
}
17+
};

lcci/02.05.Sum Lists/README.md

+52-20
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,13 @@
4848

4949
class Solution:
5050
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
51+
dummy = cur = ListNode(0)
5152
carry = 0
52-
dummy = ListNode(-1)
53-
cur = dummy
5453
while l1 or l2 or carry:
55-
s = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry
56-
carry, val = divmod(s, 10)
57-
cur.next = ListNode(val)
54+
carry += (0 if not l1 else l1.val) + (0 if not l2 else l2.val)
55+
cur.next = ListNode(carry % 10)
5856
cur = cur.next
57+
carry //= 10
5958
l1 = None if not l1 else l1.next
6059
l2 = None if not l2 else l2.next
6160
return dummy.next
@@ -80,7 +79,8 @@ class Solution {
8079
ListNode dummy = new ListNode(-1);
8180
ListNode cur = dummy;
8281
while (l1 != null || l2 != null || carry != 0) {
83-
int s = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
82+
int s =
83+
(l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
8484
carry = s / 10;
8585
cur.next = new ListNode(s % 10);
8686
cur = cur.next;
@@ -106,16 +106,17 @@ class Solution {
106106
class Solution {
107107
public:
108108
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
109-
int carry = 0;
110-
ListNode* dummy = new ListNode(-1);
109+
ListNode* dummy = new ListNode(0);
111110
ListNode* cur = dummy;
112-
while (l1 != NULL || l2 != NULL || carry != 0) {
113-
int s = (l1 == NULL ? 0 : l1-> val) + (l2 == NULL ? 0 : l2->val) + carry;
114-
carry = s / 10;
115-
cur->next = new ListNode(s % 10);
111+
int carry = 0;
112+
while (l1 || l2 || carry)
113+
{
114+
carry += (!l1 ? 0 : l1-> val) + (!l2 ? 0 : l2->val);
115+
cur->next = new ListNode(carry % 10);
116116
cur = cur->next;
117-
l1 = l1 == NULL ? NULL : l1->next;
118-
l2 = l2 == NULL ? NULL : l2->next;
117+
carry /= 10;
118+
l1 = l1 ? l1->next : l1;
119+
l2 = l2 ? l2->next : l2;
119120
}
120121
return dummy->next;
121122
}
@@ -139,20 +140,51 @@ public:
139140
*/
140141
var addTwoNumbers = function (l1, l2) {
141142
let carry = 0;
142-
const dummy = new ListNode(-1);
143+
const dummy = new ListNode(0);
143144
let cur = dummy;
144145
while (l1 || l2 || carry) {
145-
const s = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + carry;
146-
carry = Math.floor(s / 10);
147-
cur.next = new ListNode(s % 10);
146+
carry += (l1?.val || 0) + (l2?.val || 0);
147+
cur.next = new ListNode(carry % 10);
148+
carry = Math.floor(carry / 10);
148149
cur = cur.next;
149-
l1 = l1 ? l1.next : l1;
150-
l2 = l2 ? l2.next : l2;
150+
l1 = l1?.next;
151+
l2 = l2?.next;
151152
}
152153
return dummy.next;
153154
};
154155
```
155156

157+
### **Go**
158+
159+
```go
160+
/**
161+
* Definition for singly-linked list.
162+
* type ListNode struct {
163+
* Val int
164+
* Next *ListNode
165+
* }
166+
*/
167+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
168+
dummy := &ListNode{}
169+
cur := dummy
170+
carry := 0
171+
for l1 != nil || l2 != nil || carry > 0 {
172+
if l1 != nil {
173+
carry += l1.Val
174+
l1 = l1.Next
175+
}
176+
if l2 != nil {
177+
carry += l2.Val
178+
l2 = l2.Next
179+
}
180+
cur.Next = &ListNode{Val: carry % 10}
181+
cur = cur.Next
182+
carry /= 10
183+
}
184+
return dummy.Next
185+
}
186+
```
187+
156188
### **...**
157189

158190
```

lcci/02.05.Sum Lists/README_EN.md

+52-20
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,13 @@
4545

4646
class Solution:
4747
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
48+
dummy = cur = ListNode(0)
4849
carry = 0
49-
dummy = ListNode(-1)
50-
cur = dummy
5150
while l1 or l2 or carry:
52-
s = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry
53-
carry, val = divmod(s, 10)
54-
cur.next = ListNode(val)
51+
carry += (0 if not l1 else l1.val) + (0 if not l2 else l2.val)
52+
cur.next = ListNode(carry % 10)
5553
cur = cur.next
54+
carry //= 10
5655
l1 = None if not l1 else l1.next
5756
l2 = None if not l2 else l2.next
5857
return dummy.next
@@ -75,7 +74,8 @@ class Solution {
7574
ListNode dummy = new ListNode(-1);
7675
ListNode cur = dummy;
7776
while (l1 != null || l2 != null || carry != 0) {
78-
int s = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
77+
int s =
78+
(l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
7979
carry = s / 10;
8080
cur.next = new ListNode(s % 10);
8181
cur = cur.next;
@@ -101,16 +101,17 @@ class Solution {
101101
class Solution {
102102
public:
103103
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
104-
int carry = 0;
105-
ListNode* dummy = new ListNode(-1);
104+
ListNode* dummy = new ListNode(0);
106105
ListNode* cur = dummy;
107-
while (l1 != NULL || l2 != NULL || carry != 0) {
108-
int s = (l1 == NULL ? 0 : l1-> val) + (l2 == NULL ? 0 : l2->val) + carry;
109-
carry = s / 10;
110-
cur->next = new ListNode(s % 10);
106+
int carry = 0;
107+
while (l1 || l2 || carry)
108+
{
109+
carry += (!l1 ? 0 : l1-> val) + (!l2 ? 0 : l2->val);
110+
cur->next = new ListNode(carry % 10);
111111
cur = cur->next;
112-
l1 = l1 == NULL ? NULL : l1->next;
113-
l2 = l2 == NULL ? NULL : l2->next;
112+
carry /= 10;
113+
l1 = l1 ? l1->next : l1;
114+
l2 = l2 ? l2->next : l2;
114115
}
115116
return dummy->next;
116117
}
@@ -134,20 +135,51 @@ public:
134135
*/
135136
var addTwoNumbers = function (l1, l2) {
136137
let carry = 0;
137-
const dummy = new ListNode(-1);
138+
const dummy = new ListNode(0);
138139
let cur = dummy;
139140
while (l1 || l2 || carry) {
140-
const s = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + carry;
141-
carry = Math.floor(s / 10);
142-
cur.next = new ListNode(s % 10);
141+
carry += (l1?.val || 0) + (l2?.val || 0);
142+
cur.next = new ListNode(carry % 10);
143+
carry = Math.floor(carry / 10);
143144
cur = cur.next;
144-
l1 = l1 ? l1.next : l1;
145-
l2 = l2 ? l2.next : l2;
145+
l1 = l1?.next;
146+
l2 = l2?.next;
146147
}
147148
return dummy.next;
148149
};
149150
```
150151

152+
### **Go**
153+
154+
```go
155+
/**
156+
* Definition for singly-linked list.
157+
* type ListNode struct {
158+
* Val int
159+
* Next *ListNode
160+
* }
161+
*/
162+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
163+
dummy := &ListNode{}
164+
cur := dummy
165+
carry := 0
166+
for l1 != nil || l2 != nil || carry > 0 {
167+
if l1 != nil {
168+
carry += l1.Val
169+
l1 = l1.Next
170+
}
171+
if l2 != nil {
172+
carry += l2.Val
173+
l2 = l2.Next
174+
}
175+
cur.Next = &ListNode{Val: carry % 10}
176+
cur = cur.Next
177+
carry /= 10
178+
}
179+
return dummy.Next
180+
}
181+
```
182+
151183
### **...**
152184

153185
```

lcci/02.05.Sum Lists/Solution.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
class Solution {
1010
public:
1111
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
12-
int carry = 0;
13-
ListNode* dummy = new ListNode(-1);
12+
ListNode* dummy = new ListNode(0);
1413
ListNode* cur = dummy;
15-
while (l1 != NULL || l2 != NULL || carry != 0) {
16-
int s = (l1 == NULL ? 0 : l1-> val) + (l2 == NULL ? 0 : l2->val) + carry;
17-
carry = s / 10;
18-
cur->next = new ListNode(s % 10);
14+
int carry = 0;
15+
while (l1 || l2 || carry)
16+
{
17+
carry += (!l1 ? 0 : l1-> val) + (!l2 ? 0 : l2->val);
18+
cur->next = new ListNode(carry % 10);
1919
cur = cur->next;
20-
l1 = l1 == NULL ? NULL : l1->next;
21-
l2 = l2 == NULL ? NULL : l2->next;
20+
carry /= 10;
21+
l1 = l1 ? l1->next : l1;
22+
l2 = l2 ? l2->next : l2;
2223
}
2324
return dummy->next;
2425
}

lcci/02.05.Sum Lists/Solution.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
9+
dummy := &ListNode{}
10+
cur := dummy
11+
carry := 0
12+
for l1 != nil || l2 != nil || carry > 0 {
13+
if l1 != nil {
14+
carry += l1.Val
15+
l1 = l1.Next
16+
}
17+
if l2 != nil {
18+
carry += l2.Val
19+
l2 = l2.Next
20+
}
21+
cur.Next = &ListNode{Val: carry % 10}
22+
cur = cur.Next
23+
carry /= 10
24+
}
25+
return dummy.Next
26+
}

lcci/02.05.Sum Lists/Solution.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
*/
99
class Solution {
1010
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
11-
int carry = 0;
12-
ListNode dummy = new ListNode(-1);
11+
ListNode dummy = new ListNode(0);
1312
ListNode cur = dummy;
14-
while (l1 != null || l2 != null || carry != 0) {
15-
int s = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
16-
carry = s / 10;
17-
cur.next = new ListNode(s % 10);
13+
int carry = 0;
14+
while (l1 != null || l2 != null || carry > 0) {
15+
carry += (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val);
16+
cur.next = new ListNode(carry % 10);
1817
cur = cur.next;
18+
carry /= 10;
1919
l1 = l1 == null ? null : l1.next;
2020
l2 = l2 == null ? null : l2.next;
2121
}

0 commit comments

Comments
 (0)