26
26
27
27
## 解法
28
28
29
- 对字符串进行两次遍历:
29
+ ** 方法一:数组或哈希表 **
30
30
31
- 第一遍,使用 hash 表(或数组)统计字符串中每个字符出现的次数 。
31
+ 我们可以使用哈希表或数组来统计每个字符出现的次数,然后再遍历一遍字符串,找到第一个出现次数为 $1$ 的字符 。
32
32
33
- 第二遍,只要遍历到一个只出现一次的字符,那么就返回该字符,否则在遍历结束后,返回 ` ' ' ` 。
33
+ 时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串长度;而 $C$ 为字符集大小,本题中 $C=26$ 。
34
34
35
35
<!-- tabs:start -->
36
36
39
39
``` python
40
40
class Solution :
41
41
def firstUniqChar (self , s : str ) -> str :
42
- counter = Counter(s)
42
+ cnt = Counter(s)
43
43
for c in s:
44
- if counter [c] == 1 :
44
+ if cnt [c] == 1 :
45
45
return c
46
- return ' '
46
+ return " "
47
47
```
48
48
49
49
### ** Java**
50
50
51
51
``` java
52
52
class Solution {
53
53
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' ];
60
57
}
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
+ }
64
63
}
65
64
return ' ' ;
66
65
}
67
66
}
68
67
```
69
68
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
-
92
69
### ** C++**
93
70
94
71
``` cpp
95
72
class Solution {
96
73
public:
97
74
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' ] ;
101
78
}
102
- for (char c : s) {
103
- if (um [ c ] ) {
79
+ for (char& c : s) {
80
+ if (cnt [ c - 'a' ] == 1 ) {
104
81
return c;
105
82
}
106
83
}
@@ -109,6 +86,44 @@ public:
109
86
};
110
87
```
111
88
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
+
112
127
### ** TypeScript**
113
128
114
129
``` ts
@@ -151,18 +166,13 @@ impl Solution {
151
166
``` cs
152
167
public class Solution {
153
168
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' ] ++ ;
162
172
}
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 ;
166
176
}
167
177
}
168
178
return ' ' ;
0 commit comments