Skip to content

Commit e826df0

Browse files
committed
feat: update solutions to lcci problems
* lcci No.01.02.Check Permutation * lcci No.01.04.Palindrome Permutation * lcci No.02.03.Delete Middle Node
1 parent 003c5a9 commit e826df0

File tree

15 files changed

+185
-92
lines changed

15 files changed

+185
-92
lines changed

lcci/01.01.Is Unique/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

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

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

1616
<p><strong>示例 2:</strong></p>
1717

18-
<pre><strong>输入:</strong> <code>s</code> = &quot;abc&quot;
18+
<pre><strong>输入:</strong> s = &quot;abc&quot;
1919
<strong>输出:</strong> true
2020
</pre>
2121

lcci/01.01.Is Unique/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<pre>
1212

13-
<strong>Input: </strong><code>s</code> = &quot;leetcode&quot;
13+
<strong>Input: </strong> = &quot;leetcode&quot;
1414

1515
<strong>Output: </strong>false
1616

@@ -20,7 +20,7 @@
2020

2121
<pre>
2222

23-
<strong>Input: </strong><code>s</code> = &quot;abc&quot;
23+
<strong>Input: </strong>s = &quot;abc&quot;
2424

2525
<strong>Output: </strong>true
2626

lcci/01.02.Check Permutation/README.md

+10-13
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

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

12-
<pre><strong>输入:</strong> <code>s1</code> = &quot;abc&quot;, <code>s2</code> = &quot;bca&quot;
12+
<pre><strong>输入:</strong> s1 = &quot;abc&quot;, s2 = &quot;bca&quot;
1313
<strong>输出:</strong> true
1414
</pre>
1515

1616
<p><strong>示例 2:</strong></p>
1717

18-
<pre><strong>输入:</strong> <code>s1</code> = &quot;abc&quot;, <code>s2</code> = &quot;bad&quot;
18+
<pre><strong>输入:</strong> s1 = &quot;abc&quot;, s2 = &quot;bad&quot;
1919
<strong>输出:</strong> false
2020
</pre>
2121

@@ -48,10 +48,7 @@ class Solution:
4848
for i in range(n1):
4949
counter[s1[i]] += 1
5050
counter[s2[i]] -= 1
51-
for val in counter.values():
52-
if val != 0:
53-
return False
54-
return True
51+
return all(v == 0 for v in counter.values())
5552
```
5653

5754
### **Java**
@@ -61,18 +58,18 @@ class Solution:
6158
```java
6259
class Solution {
6360
public boolean CheckPermutation(String s1, String s2) {
64-
int n1 = s1.length(), n2 = s2.length();
61+
int n1 = s1.length();
62+
int n2 = s2.length();
6563
if (n1 != n2) {
6664
return false;
6765
}
68-
Map<Character, Integer> counter = new HashMap<>();
66+
int[] counter = new int[128];
6967
for (int i = 0; i < n1; ++i) {
70-
char c1 = s1.charAt(i), c2 = s2.charAt(i);
71-
counter.put(c1, counter.getOrDefault(c1, 0) + 1);
72-
counter.put(c2, counter.getOrDefault(c2, 0) - 1);
68+
++counter[s1.charAt(i)];
69+
--counter[s2.charAt(i)];
7370
}
74-
for (int val : counter.values()) {
75-
if (val != 0) {
71+
for (int v : counter) {
72+
if (v != 0) {
7673
return false;
7774
}
7875
}

lcci/01.02.Check Permutation/README_EN.md

+10-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<pre>
1212

13-
<strong>Input: </strong><code>s1</code> = &quot;abc&quot;, <code>s2</code> = &quot;bca&quot;
13+
<strong>Input: </strong>s1 = &quot;abc&quot;, s2 = &quot;bca&quot;
1414

1515
<strong>Output: </strong>true
1616

@@ -20,7 +20,7 @@
2020

2121
<pre>
2222

23-
<strong>Input: </strong><code>s1</code> = &quot;abc&quot;, <code>s2</code> = &quot;bad&quot;
23+
<strong>Input: </strong>s1 = &quot;abc&quot;, s2 = &quot;bad&quot;
2424

2525
<strong>Output: </strong>false
2626

@@ -48,29 +48,26 @@ class Solution:
4848
for i in range(n1):
4949
counter[s1[i]] += 1
5050
counter[s2[i]] -= 1
51-
for val in counter.values():
52-
if val != 0:
53-
return False
54-
return True
51+
return all(v == 0 for v in counter.values())
5552
```
5653

5754
### **Java**
5855

5956
```java
6057
class Solution {
6158
public boolean CheckPermutation(String s1, String s2) {
62-
int n1 = s1.length(), n2 = s2.length();
59+
int n1 = s1.length();
60+
int n2 = s2.length();
6361
if (n1 != n2) {
6462
return false;
6563
}
66-
Map<Character, Integer> counter = new HashMap<>();
64+
int[] counter = new int[128];
6765
for (int i = 0; i < n1; ++i) {
68-
char c1 = s1.charAt(i), c2 = s2.charAt(i);
69-
counter.put(c1, counter.getOrDefault(c1, 0) + 1);
70-
counter.put(c2, counter.getOrDefault(c2, 0) - 1);
66+
++counter[s1.charAt(i)];
67+
--counter[s2.charAt(i)];
7168
}
72-
for (int val : counter.values()) {
73-
if (val != 0) {
69+
for (int v : counter) {
70+
if (v != 0) {
7471
return false;
7572
}
7673
}

lcci/01.02.Check Permutation/Solution.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
class Solution {
22
public boolean CheckPermutation(String s1, String s2) {
3-
int n1 = s1.length(), n2 = s2.length();
3+
int n1 = s1.length();
4+
int n2 = s2.length();
45
if (n1 != n2) {
56
return false;
67
}
7-
Map<Character, Integer> counter = new HashMap<>();
8+
int[] counter = new int[128];
89
for (int i = 0; i < n1; ++i) {
9-
char c1 = s1.charAt(i), c2 = s2.charAt(i);
10-
counter.put(c1, counter.getOrDefault(c1, 0) + 1);
11-
counter.put(c2, counter.getOrDefault(c2, 0) - 1);
10+
++counter[s1.charAt(i)];
11+
--counter[s2.charAt(i)];
1212
}
13-
for (int val : counter.values()) {
14-
if (val != 0) {
13+
for (int v : counter) {
14+
if (v != 0) {
1515
return false;
1616
}
1717
}

lcci/01.02.Check Permutation/Solution.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,4 @@ def CheckPermutation(self, s1: str, s2: str) -> bool:
77
for i in range(n1):
88
counter[s1[i]] += 1
99
counter[s2[i]] -= 1
10-
for val in counter.values():
11-
if val != 0:
12-
return False
13-
return True
10+
return all(v == 0 for v in counter.values())

lcci/01.04.Palindrome Permutation/README.md

+20-17
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,7 @@
3737
class Solution:
3838
def canPermutePalindrome(self, s: str) -> bool:
3939
counter = Counter(s)
40-
cnt = 0
41-
for val in counter.values():
42-
if (val & 1) == 1:
43-
cnt += 1
44-
if cnt > 1:
45-
return False
46-
return True
40+
return sum(1 for v in counter.values() if v % 2 == 1) <= 1
4741
```
4842

4943
### **Java**
@@ -54,20 +48,14 @@ class Solution:
5448
class Solution {
5549
public boolean canPermutePalindrome(String s) {
5650
Map<Character, Integer> counter = new HashMap<>();
57-
for (int i = 0, n = s.length(); i < n; ++i) {
58-
char c = s.charAt(i);
51+
for (char c : s.toCharArray()) {
5952
counter.put(c, counter.getOrDefault(c, 0) + 1);
6053
}
6154
int cnt = 0;
62-
for (int val : counter.values()) {
63-
if ((val & 1) == 1) {
64-
++cnt;
65-
}
66-
if (cnt > 1) {
67-
return false;
68-
}
55+
for (int v : counter.values()) {
56+
cnt += v % 2;
6957
}
70-
return true;
58+
return cnt < 2;
7159
}
7260
}
7361
```
@@ -91,6 +79,21 @@ func canPermutePalindrome(s string) bool {
9179
}
9280
```
9381

82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
bool canPermutePalindrome(string s) {
88+
unordered_map<char, int> counter;
89+
for (char c : s) ++counter[c];
90+
int cnt = 0;
91+
for (auto& [k, v] : counter) cnt += v % 2;
92+
return cnt < 2;
93+
}
94+
};
95+
```
96+
9497
### **...**
9598
9699
```

lcci/01.04.Palindrome Permutation/README_EN.md

+20-17
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,7 @@
2828
class Solution:
2929
def canPermutePalindrome(self, s: str) -> bool:
3030
counter = Counter(s)
31-
cnt = 0
32-
for val in counter.values():
33-
if (val & 1) == 1:
34-
cnt += 1
35-
if cnt > 1:
36-
return False
37-
return True
31+
return sum(1 for v in counter.values() if v % 2 == 1) <= 1
3832
```
3933

4034
### **Java**
@@ -43,20 +37,14 @@ class Solution:
4337
class Solution {
4438
public boolean canPermutePalindrome(String s) {
4539
Map<Character, Integer> counter = new HashMap<>();
46-
for (int i = 0, n = s.length(); i < n; ++i) {
47-
char c = s.charAt(i);
40+
for (char c : s.toCharArray()) {
4841
counter.put(c, counter.getOrDefault(c, 0) + 1);
4942
}
5043
int cnt = 0;
51-
for (int val : counter.values()) {
52-
if ((val & 1) == 1) {
53-
++cnt;
54-
}
55-
if (cnt > 1) {
56-
return false;
57-
}
44+
for (int v : counter.values()) {
45+
cnt += v % 2;
5846
}
59-
return true;
47+
return cnt < 2;
6048
}
6149
}
6250
```
@@ -80,6 +68,21 @@ func canPermutePalindrome(s string) bool {
8068
}
8169
```
8270

71+
### **C++**
72+
73+
```cpp
74+
class Solution {
75+
public:
76+
bool canPermutePalindrome(string s) {
77+
unordered_map<char, int> counter;
78+
for (char c : s) ++counter[c];
79+
int cnt = 0;
80+
for (auto& [k, v] : counter) cnt += v % 2;
81+
return cnt < 2;
82+
}
83+
};
84+
```
85+
8386
### **...**
8487
8588
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
bool canPermutePalindrome(string s) {
4+
unordered_map<char, int> counter;
5+
for (char c : s) ++counter[c];
6+
int cnt = 0;
7+
for (auto& [k, v] : counter) cnt += v % 2;
8+
return cnt < 2;
9+
}
10+
};
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
class Solution {
22
public boolean canPermutePalindrome(String s) {
33
Map<Character, Integer> counter = new HashMap<>();
4-
for (int i = 0, n = s.length(); i < n; ++i) {
5-
char c = s.charAt(i);
4+
for (char c : s.toCharArray()) {
65
counter.put(c, counter.getOrDefault(c, 0) + 1);
76
}
87
int cnt = 0;
9-
for (int val : counter.values()) {
10-
if ((val & 1) == 1) {
11-
++cnt;
12-
}
13-
if (cnt > 1) {
14-
return false;
15-
}
8+
for (int v : counter.values()) {
9+
cnt += v % 2;
1610
}
17-
return true;
11+
return cnt < 2;
1812
}
1913
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
class Solution:
22
def canPermutePalindrome(self, s: str) -> bool:
33
counter = Counter(s)
4-
cnt = 0
5-
for val in counter.values():
6-
if (val & 1) == 1:
7-
cnt += 1
8-
if cnt > 1:
9-
return False
10-
return True
4+
return sum(1 for v in counter.values() if v % 2 == 1) <= 1

lcci/02.03.Delete Middle Node/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,42 @@ var deleteNode = function (node) {
8686
};
8787
```
8888

89+
### **C++**
90+
91+
```cpp
92+
/**
93+
* Definition for singly-linked list.
94+
* struct ListNode {
95+
* int val;
96+
* ListNode *next;
97+
* ListNode(int x) : val(x), next(NULL) {}
98+
* };
99+
*/
100+
class Solution {
101+
public:
102+
void deleteNode(ListNode* node) {
103+
node->val = node->next->val;
104+
node->next = node->next->next;
105+
}
106+
};
107+
```
108+
109+
### **Go**
110+
111+
```go
112+
/**
113+
* Definition for singly-linked list.
114+
* type ListNode struct {
115+
* Val int
116+
* Next *ListNode
117+
* }
118+
*/
119+
func deleteNode(node *ListNode) {
120+
node.Val = node.Next.Val
121+
node.Next = node.Next.Next
122+
}
123+
```
124+
89125
### **...**
90126

91127
```

0 commit comments

Comments
 (0)