Skip to content

Commit c78ce6a

Browse files
committed
feat: add solutions to lcof problem: No.50
1 parent 167b4ce commit c78ce6a

File tree

7 files changed

+105
-90
lines changed

7 files changed

+105
-90
lines changed

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

+63-53
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626

2727
## 解法
2828

29-
对字符串进行两次遍历:
29+
**方法一:数组或哈希表**
3030

31-
第一遍,使用 hash 表(或数组)统计字符串中每个字符出现的次数
31+
我们可以使用哈希表或数组来统计每个字符出现的次数,然后再遍历一遍字符串,找到第一个出现次数为 $1$ 的字符
3232

33-
第二遍,只要遍历到一个只出现一次的字符,那么就返回该字符,否则在遍历结束后,返回 `' '`
33+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串长度;而 $C$ 为字符集大小,本题中 $C=26$
3434

3535
<!-- tabs:start -->
3636

@@ -39,68 +39,45 @@
3939
```python
4040
class Solution:
4141
def firstUniqChar(self, s: str) -> str:
42-
counter = Counter(s)
42+
cnt = Counter(s)
4343
for c in s:
44-
if counter[c] == 1:
44+
if cnt[c] == 1:
4545
return c
46-
return ' '
46+
return " "
4747
```
4848

4949
### **Java**
5050

5151
```java
5252
class Solution {
5353
public char firstUniqChar(String s) {
54-
int n;
55-
if ((n = s.length()) == 0) return ' ';
56-
int[] counter = new int[26];
57-
for (int i = 0; i < n; ++i) {
58-
int index = s.charAt(i) - 'a';
59-
++counter[index];
54+
int[] cnt = new int[26];
55+
for (int i = 0; i < s.length(); ++i) {
56+
++cnt[s.charAt(i) - 'a'];
6057
}
61-
for (int i = 0; i < n; ++i) {
62-
int index = s.charAt(i) - 'a';
63-
if (counter[index] == 1) return s.charAt(i);
58+
for (int i = 0; i < s.length(); ++i) {
59+
char c = s.charAt(i);
60+
if (cnt[c - 'a'] == 1) {
61+
return c;
62+
}
6463
}
6564
return ' ';
6665
}
6766
}
6867
```
6968

70-
### **JavaScript**
71-
72-
```js
73-
/**
74-
* @param {string} s
75-
* @return {character}
76-
*/
77-
var firstUniqChar = function (s) {
78-
if (s.length == 0) return ' ';
79-
let counter = new Array(26).fill(0);
80-
for (let i = 0; i < s.length; ++i) {
81-
const index = s[i].charCodeAt() - 'a'.charCodeAt();
82-
++counter[index];
83-
}
84-
for (let i = 0; i < s.length; ++i) {
85-
const index = s[i].charCodeAt() - 'a'.charCodeAt();
86-
if (counter[index] == 1) return s[i];
87-
}
88-
return ' ';
89-
};
90-
```
91-
9269
### **C++**
9370

9471
```cpp
9572
class Solution {
9673
public:
9774
char firstUniqChar(string s) {
98-
unordered_map<char, bool> um;
99-
for (char c : s) {
100-
um[c] = um.find(c) == um.end();
75+
int cnt[26]{};
76+
for (char& c : s) {
77+
++cnt[c - 'a'];
10178
}
102-
for (char c : s) {
103-
if (um[c]) {
79+
for (char&c : s) {
80+
if (cnt[c - 'a'] == 1) {
10481
return c;
10582
}
10683
}
@@ -109,6 +86,44 @@ public:
10986
};
11087
```
11188
89+
### **Go**
90+
91+
```go
92+
func firstUniqChar(s string) byte {
93+
cnt := [26]int{}
94+
for _, c := range s {
95+
cnt[c-'a']++
96+
}
97+
for _, c := range s {
98+
if cnt[c-'a'] == 1 {
99+
return byte(c)
100+
}
101+
}
102+
return ' '
103+
}
104+
```
105+
106+
### **JavaScript**
107+
108+
```js
109+
/**
110+
* @param {string} s
111+
* @return {character}
112+
*/
113+
var firstUniqChar = function (s) {
114+
const cnt = new Array(26).fill(0);
115+
for (const c of s) {
116+
cnt[c.charCodeAt(0) - 97]++;
117+
}
118+
for (const c of s) {
119+
if (cnt[c.charCodeAt(0) - 97] === 1) {
120+
return c;
121+
}
122+
}
123+
return ' ';
124+
};
125+
```
126+
112127
### **TypeScript**
113128

114129
```ts
@@ -151,18 +166,13 @@ impl Solution {
151166
```cs
152167
public class Solution {
153168
public char FirstUniqChar(string s) {
154-
Dictionary<char, bool> dic = new Dictionary<char, bool>();
155-
foreach (var c in s) {
156-
if (dic.ContainsKey(c)) {
157-
dic[c] = false;
158-
}
159-
else {
160-
dic.Add(c, true);
161-
}
169+
var cnt = new int[26];
170+
foreach(var c in s) {
171+
cnt[c - 'a'] ++;
162172
}
163-
foreach (var d in dic) {
164-
if (d.Value) {
165-
return d.Key;
173+
foreach(var c in s) {
174+
if (cnt[c - 'a'] == 1) {
175+
return c;
166176
}
167177
}
168178
return ' ';
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Solution {
22
public:
33
char firstUniqChar(string s) {
4-
unordered_map<char, bool> um;
5-
for (char c : s) {
6-
um[c] = um.find(c) == um.end();
4+
int cnt[26]{};
5+
for (char& c : s) {
6+
++cnt[c - 'a'];
77
}
8-
for (char c : s) {
9-
if (um[c]) {
8+
for (char&c : s) {
9+
if (cnt[c - 'a'] == 1) {
1010
return c;
1111
}
1212
}
1313
return ' ';
1414
}
15-
};
15+
};

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

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
public class Solution {
22
public char FirstUniqChar(string s) {
3-
Dictionary<char, bool> dic = new Dictionary<char, bool>();
4-
foreach (var c in s) {
5-
if (dic.ContainsKey(c)) {
6-
dic[c] = false;
7-
}
8-
else {
9-
dic.Add(c, true);
10-
}
3+
var cnt = new int[26];
4+
foreach(var c in s) {
5+
cnt[c - 'a'] ++;
116
}
12-
foreach (var d in dic) {
13-
if (d.Value) {
14-
return d.Key;
7+
foreach(var c in s) {
8+
if (cnt[c - 'a'] == 1) {
9+
return c;
1510
}
1611
}
1712
return ' ';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func firstUniqChar(s string) byte {
2+
cnt := [26]int{}
3+
for _, c := range s {
4+
cnt[c-'a']++
5+
}
6+
for _, c := range s {
7+
if cnt[c-'a'] == 1 {
8+
return byte(c)
9+
}
10+
}
11+
return ' '
12+
}

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

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
class Solution {
22
public char firstUniqChar(String s) {
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];
3+
int[] cnt = new int[26];
4+
for (int i = 0; i < s.length(); ++i) {
5+
++cnt[s.charAt(i) - 'a'];
96
}
10-
for (int i = 0; i < n; ++i) {
11-
int index = s.charAt(i) - 'a';
12-
if (counter[index] == 1) return s.charAt(i);
7+
for (int i = 0; i < s.length(); ++i) {
8+
char c = s.charAt(i);
9+
if (cnt[c - 'a'] == 1) {
10+
return c;
11+
}
1312
}
1413
return ' ';
1514
}

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

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
* @return {character}
44
*/
55
var firstUniqChar = function (s) {
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];
6+
const cnt = new Array(26).fill(0);
7+
for (const c of s) {
8+
cnt[c.charCodeAt(0) - 97]++;
119
}
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];
10+
for (const c of s) {
11+
if (cnt[c.charCodeAt(0) - 97] === 1) {
12+
return c;
13+
}
1514
}
1615
return ' ';
1716
};
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def firstUniqChar(self, s: str) -> str:
3-
counter = Counter(s)
3+
cnt = Counter(s)
44
for c in s:
5-
if counter[c] == 1:
5+
if cnt[c] == 1:
66
return c
7-
return ' '
7+
return " "

0 commit comments

Comments
 (0)