Skip to content

Commit 98dc199

Browse files
committed
feat: update lcof solutions: No.50. first unique character
1 parent cf2d2c7 commit 98dc199

File tree

4 files changed

+55
-53
lines changed

4 files changed

+55
-53
lines changed

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

+32-28
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

+9-11
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

+9-7
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

+5-7
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 ' '

0 commit comments

Comments
 (0)