44
44
45
45
<!-- 这里可写通用的实现逻辑 -->
46
46
47
- 遍历字符串,用一个 map 或者字典存放字符串中每个字符出现的次数。然后再次遍历字符串,取出对应字符出现的次数,若次数为 1,直接返回当前字符串的下标。遍历结束,返回 -1。
47
+ ** 方法一:数组或哈希表**
48
+
49
+ 我们可以用数组或哈希表 $cnt$ 记录字符串 $s$ 中每个字符出现的次数。
50
+
51
+ 然后我们再遍历字符串 $s$,当遍历到某个字符 $c$ 时,如果 $cnt[ c] =1$,则说明 $c$ 是第一个不重复的字符,返回它的索引即可。
52
+
53
+ 如果遍历完字符串 $s$ 仍然没有找到不重复的字符,返回 $-1$。
54
+
55
+ 时间复杂度 $O(n)$,空间复杂度 $O(\Sigma)$,其中 $\Sigma$ 是字符集的大小。
48
56
49
57
<!-- tabs:start -->
50
58
55
63
``` python
56
64
class Solution :
57
65
def firstUniqChar (self , s : str ) -> int :
58
- counter = Counter(s)
66
+ cnt = Counter(s)
59
67
for i, c in enumerate (s):
60
- if counter [c] == 1 :
68
+ if cnt [c] == 1 :
61
69
return i
62
70
return - 1
63
71
```
@@ -69,13 +77,13 @@ class Solution:
69
77
``` java
70
78
class Solution {
71
79
public int firstUniqChar (String s ) {
72
- int [] counter = new int [26 ];
73
- for (char c : s. toCharArray()) {
74
- ++ counter[c - ' a' ];
80
+ int [] cnt = new int [26 ];
81
+ int n = s. length();
82
+ for (int i = 0 ; i < n; ++ i) {
83
+ ++ cnt[s. charAt(i) - ' a' ];
75
84
}
76
- for (int i = 0 ; i < s. length(); ++ i) {
77
- char c = s. charAt(i);
78
- if (counter[c - ' a' ] == 1 ) {
85
+ for (int i = 0 ; i < n; ++ i) {
86
+ if (cnt[s. charAt(i) - ' a' ] == 1 ) {
79
87
return i;
80
88
}
81
89
}
@@ -84,32 +92,22 @@ class Solution {
84
92
}
85
93
```
86
94
87
- ### ** TypeScript**
88
-
89
- ``` ts
90
- function firstUniqChar(s : string ): number {
91
- let record = new Map ();
92
- for (let cur of [... s ]) {
93
- record .set (cur , record .has (cur ));
94
- }
95
- for (let i = 0 ; i < s .length ; i ++ ) {
96
- if (! record .get (s [i ])) return i ;
97
- }
98
- return - 1 ;
99
- }
100
- ```
101
-
102
95
### ** C++**
103
96
104
97
``` cpp
105
98
class Solution {
106
99
public:
107
100
int firstUniqChar(string s) {
108
- vector<int > counter(26);
109
- for (char& c : s) ++counter[ c - 'a'] ;
110
- for (int i = 0; i < s.size(); ++i)
111
- if (counter[ s[ i] - 'a'] == 1)
101
+ int cnt[ 26] {};
102
+ for (char& c : s) {
103
+ ++cnt[ c - 'a'] ;
104
+ }
105
+ int n = s.size();
106
+ for (int i = 0; i < n; ++i) {
107
+ if (cnt[ s[ i] - 'a'] == 1) {
112
108
return i;
109
+ }
110
+ }
113
111
return -1;
114
112
}
115
113
};
@@ -119,12 +117,12 @@ public:
119
117
120
118
```go
121
119
func firstUniqChar(s string) int {
122
- counter := make([ ]int, 26)
120
+ cnt := [26 ]int{}
123
121
for _, c := range s {
124
- counter [c-'a']++
122
+ cnt [c-'a']++
125
123
}
126
124
for i, c := range s {
127
- if counter [c-'a'] == 1 {
125
+ if cnt [c-'a'] == 1 {
128
126
return i
129
127
}
130
128
}
@@ -140,19 +138,36 @@ func firstUniqChar(s string) int {
140
138
* @return {number}
141
139
*/
142
140
var firstUniqChar = function (s ) {
143
- const counter = new Map ( );
144
- for (let c of s) {
145
- counter[c] = (counter[c] || 0 ) + 1 ;
141
+ const cnt = new Array ( 26 ). fill ( 0 );
142
+ for (const c of s) {
143
+ ++ cnt[ c . charCodeAt () - ' a ' . charCodeAt ()] ;
146
144
}
147
145
for (let i = 0 ; i < s .length ; ++ i) {
148
- if (counter [s[i]] == 1 ) {
146
+ if (cnt [s[i]. charCodeAt () - ' a ' . charCodeAt ()] = == 1 ) {
149
147
return i;
150
148
}
151
149
}
152
150
return - 1 ;
153
151
};
154
152
```
155
153
154
+ ### ** TypeScript**
155
+
156
+ ``` ts
157
+ function firstUniqChar(s : string ): number {
158
+ const cnt = new Array (26 ).fill (0 );
159
+ for (const c of s ) {
160
+ cnt [c .charCodeAt (0 ) - 97 ]++ ;
161
+ }
162
+ for (let i = 0 ; i < s .length ; i ++ ) {
163
+ if (cnt [s .charCodeAt (i ) - 97 ] === 1 ) {
164
+ return i ;
165
+ }
166
+ }
167
+ return - 1 ;
168
+ }
169
+ ```
170
+
156
171
### ** PHP**
157
172
158
173
``` php
@@ -162,14 +177,11 @@ class Solution {
162
177
* @return Integer
163
178
*/
164
179
function firstUniqChar($s) {
165
- $hashmap = [];
166
180
for ($i = 0; $i < strlen($s); $i++) {
167
- $word = $s[$i];
168
- $hasmap[$word] += 1;
181
+ $hashtable[$s[$i]]++;
169
182
}
170
183
for ($i = 0; $i < strlen($s); $i++) {
171
- $word = $s[$i];
172
- if ($hasmap[$word] == 1) {
184
+ if ($hashtable[$s[$i]] == 1) {
173
185
return $i;
174
186
}
175
187
}
0 commit comments