回答編集履歴
1
追記
answer
CHANGED
@@ -18,4 +18,31 @@
|
|
18
18
|
return -1; // なかったら-1
|
19
19
|
}
|
20
20
|
};
|
21
|
+
```
|
22
|
+
[つけたし] ハッシュ表使うまでもないか...
|
23
|
+
```C++
|
24
|
+
#include <string>
|
25
|
+
#include <algorithm>
|
26
|
+
|
27
|
+
class Solution {
|
28
|
+
public:
|
29
|
+
int firstUniqChar(const std::string& s) {
|
30
|
+
int hist[256];
|
31
|
+
std::fill(hist, hist+256, 0);
|
32
|
+
for ( char ch : s ) {
|
33
|
+
hist[ch]++;
|
34
|
+
}
|
35
|
+
for ( int i = 0; i < s.size(); ++i ) {
|
36
|
+
if ( hist[s.at(i)] == 1 ) return i;
|
37
|
+
}
|
38
|
+
return -1;
|
39
|
+
}
|
40
|
+
};
|
41
|
+
|
42
|
+
#include <iostream>
|
43
|
+
|
44
|
+
int main() {
|
45
|
+
Solution s;
|
46
|
+
std::cout << s.firstUniqChar("abcdefghijkld") << std::endl;
|
47
|
+
}
|
21
48
|
```
|