Skip to content

Commit bfb9a26

Browse files
committed
Merge branch 'main' of github.com:doocs/leetcode into main
2 parents 5481598 + 8a30799 commit bfb9a26

File tree

33 files changed

+605
-168
lines changed

33 files changed

+605
-168
lines changed

images/contributors.svg

Lines changed: 17 additions & 14 deletions
Loading

lcof/面试题50. 第一个只出现一次的字符/README.md

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,59 @@
66

77
**示例:**
88

9-
````
9+
```
1010
s = "abaccdeff"
1111
返回 "b"
1212
1313
s = ""
1414
返回 " "
15-
``` 
15+
```
1616

1717
**限制:**
1818

1919
- `0 <= s 的长度 <= 50000`
2020

2121
## 解法
22-
有序字典解决。
22+
23+
对字符串进行两次遍历:
24+
25+
第一遍,先用 hash 表(或数组)统计字符串中每个字符出现的次数。
26+
27+
第二遍,只要遍历到一个只出现一次的字符,那么就返回该字符,否则在遍历结束后返回空格。
2328

2429
<!-- tabs:start -->
2530

2631
### **Python3**
32+
2733
```python
2834
import collections
2935

3036
class Solution:
3137
def firstUniqChar(self, s: str) -> str:
32-
if s == '':
33-
return ' '
34-
cache = collections.OrderedDict()
38+
counter = collections.Counter()
39+
for c in s:
40+
counter[c] += 1
3541
for c in s:
36-
cache[c] = 1 if cache.get(c) is None else cache[c] + 1
37-
for k, v in cache.items():
38-
if v == 1:
39-
return k
42+
if counter[c] == 1:
43+
return c
4044
return ' '
41-
````
45+
```
4246

4347
### **Java**
4448

4549
```java
4650
class Solution {
4751
public char firstUniqChar(String s) {
48-
if ("".equals(s)) {
49-
return ' ';
50-
}
51-
Map<Character, Integer> map = new LinkedHashMap<>();
52-
char[] chars = s.toCharArray();
53-
for (char c : chars) {
54-
map.put(c, map.get(c) == null ? 1 : 1 + map.get(c));
52+
int n;
53+
if ((n = s.length()) == 0) return ' ';
54+
int[] counter = new int[26];
55+
for (int i = 0; i < n; ++i) {
56+
int index = s.charAt(i) - 'a';
57+
++counter[index];
5558
}
56-
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
57-
if (entry.getValue() == 1) {
58-
return entry.getKey();
59-
}
59+
for (int i = 0; i < n; ++i) {
60+
int index = s.charAt(i) - 'a';
61+
if (counter[index] == 1) return s.charAt(i);
6062
}
6163
return ' ';
6264
}
@@ -71,13 +73,15 @@ class Solution {
7173
* @return {character}
7274
*/
7375
var firstUniqChar = function (s) {
74-
let t = new Array(26).fill(0);
75-
let code = "a".charCodeAt();
76-
for (let i = 0; i < s.length; i++) {
77-
t[s[i].charCodeAt() - code]++;
76+
if (s.length == 0) return " ";
77+
let counter = new Array(26).fill(0);
78+
for (let i = 0; i < s.length; ++i) {
79+
const index = s[i].charCodeAt() - "a".charCodeAt();
80+
++counter[index];
7881
}
79-
for (let i = 0; i < s.length; i++) {
80-
if (t[s[i].charCodeAt() - code] === 1) return s[i];
82+
for (let i = 0; i < s.length; ++i) {
83+
const index = s[i].charCodeAt() - "a".charCodeAt();
84+
if (counter[index] == 1) return s[i];
8185
}
8286
return " ";
8387
};

lcof/面试题50. 第一个只出现一次的字符/Solution.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
class Solution {
22
public char firstUniqChar(String s) {
3-
if ("".equals(s)) {
4-
return ' ';
3+
int n;
4+
if ((n = s.length()) == 0) return ' ';
5+
int[] counter = new int[26];
6+
for (int i = 0; i < n; ++i) {
7+
int index = s.charAt(i) - 'a';
8+
++counter[index];
59
}
6-
Map<Character, Integer> map = new LinkedHashMap<>();
7-
char[] chars = s.toCharArray();
8-
for (char c : chars) {
9-
map.put(c, map.get(c) == null ? 1 : 1 + map.get(c));
10-
}
11-
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
12-
if (entry.getValue() == 1) {
13-
return entry.getKey();
14-
}
10+
for (int i = 0; i < n; ++i) {
11+
int index = s.charAt(i) - 'a';
12+
if (counter[index] == 1) return s.charAt(i);
1513
}
1614
return ' ';
1715
}

lcof/面试题50. 第一个只出现一次的字符/Solution.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
* @return {character}
44
*/
55
var firstUniqChar = function (s) {
6-
let t = new Array(26).fill(0);
7-
let code = "a".charCodeAt();
8-
for (let i = 0; i < s.length; i++) {
9-
t[s[i].charCodeAt() - code]++;
6+
if (s.length == 0) return " ";
7+
let counter = new Array(26).fill(0);
8+
for (let i = 0; i < s.length; ++i) {
9+
const index = s[i].charCodeAt() - "a".charCodeAt();
10+
++counter[index];
1011
}
11-
for (let i = 0; i < s.length; i++) {
12-
if (t[s[i].charCodeAt() - code] === 1) return s[i];
12+
for (let i = 0; i < s.length; ++i) {
13+
const index = s[i].charCodeAt() - "a".charCodeAt();
14+
if (counter[index] == 1) return s[i];
1315
}
1416
return " ";
15-
};
17+
};

lcof/面试题50. 第一个只出现一次的字符/Solution.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
class Solution:
44
def firstUniqChar(self, s: str) -> str:
5-
if s == '':
6-
return ' '
7-
cache = collections.OrderedDict()
5+
counter = collections.Counter()
86
for c in s:
9-
cache[c] = 1 if cache.get(c) is None else cache[c] + 1
10-
for k, v in cache.items():
11-
if v == 1:
12-
return k
7+
counter[c] += 1
8+
for c in s:
9+
if counter[c] == 1:
10+
return c
1311
return ' '

lcof/面试题66. 构建乘积数组/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
class Solution:
3535
def constructArr(self, a: List[int]) -> List[int]:
3636
n = len(a)
37-
output = [1 for _ in a]
37+
output = [1] * n
3838
left = right = 1
3939
for i in range(n):
4040
output[i] = left
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution:
22
def constructArr(self, a: List[int]) -> List[int]:
33
n = len(a)
4-
output = [1 for _ in a]
4+
output = [1] * n
55
left = right = 1
66
for i in range(n):
77
output[i] = left
88
left *= a[i]
99
for i in range(n - 1, -1, -1):
1010
output[i] *= right
1111
right *= a[i]
12-
return output
12+
return output

solution/1000-1099/1045.Customers Who Bought All Products/README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,20 @@ Result 表:
7272
### **SQL**
7373

7474
```sql
75-
75+
# Write your MySQL query statement below
76+
SELECT
77+
customer_id
78+
FROM
79+
Customer
80+
GROUP BY
81+
customer_id
82+
HAVING
83+
COUNT(DISTINCT(product_key)) = (
84+
SELECT
85+
COUNT(1)
86+
FROM
87+
Product
88+
);
7689
```
7790

7891
<!-- tabs:end -->

solution/1000-1099/1045.Customers Who Bought All Products/README_EN.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,20 @@ The customers who bought all the products (5 and 6) are customers with id 1 and
7777
### **SQL**
7878

7979
```sql
80-
80+
# Write your MySQL query statement below
81+
SELECT
82+
customer_id
83+
FROM
84+
Customer
85+
GROUP BY
86+
customer_id
87+
HAVING
88+
COUNT(DISTINCT(product_key)) = (
89+
SELECT
90+
COUNT(1)
91+
FROM
92+
Product
93+
);
8194
```
8295

8396
<!-- tabs:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
customer_id
4+
FROM
5+
Customer
6+
GROUP BY
7+
customer_id
8+
HAVING
9+
COUNT(DISTINCT(product_key)) = (
10+
SELECT
11+
COUNT(1)
12+
FROM
13+
Product
14+
);

solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,20 @@ Result 表:
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
`GROUP BY` + `HAVING` 解决。
56+
5557
<!-- tabs:start -->
5658

5759
### **SQL**
5860

5961
```sql
60-
62+
# Write your MySQL query statement below
63+
SELECT
64+
actor_id, director_id
65+
FROM
66+
ActorDirector
67+
GROUP BY actor_id, director_id
68+
HAVING count(1) >= 3;
6169
```
6270

6371
<!-- tabs:end -->

solution/1000-1099/1050.Actors and Directors Who Cooperated At Least Three Times/README_EN.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,20 @@ The only pair is (1, 1) where they cooperated exactly 3 times.
9090

9191
## Solutions
9292

93+
Use `GROUP BY` & `HAVING`.
94+
9395
<!-- tabs:start -->
9496

9597
### **SQL**
9698

9799
```sql
98-
100+
# Write your MySQL query statement below
101+
SELECT
102+
actor_id, director_id
103+
FROM
104+
ActorDirector
105+
GROUP BY actor_id, director_id
106+
HAVING count(1) >= 3;
99107
```
100108

101109
<!-- tabs:end -->
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
actor_id, director_id
4+
FROM
5+
ActorDirector
6+
GROUP BY actor_id, director_id
7+
HAVING count(1) >= 3;

0 commit comments

Comments
 (0)