Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfdebb41

Browse files
Add files via upload
1 parentdfa128d commitfdebb41

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Runtime: 48 ms, faster than 62.52% of C++ online submissions for First Unique Character in a String.
2+
// Memory Usage: 12.8 MB, less than 90.63% of C++ online submissions for First Unique Character in a String.
3+
4+
classSolution
5+
{
6+
public:
7+
intfirstUniqChar(string s)
8+
{
9+
vector<bool>repeat(26,false);
10+
vector<int>memo(26, -1);
11+
12+
for (int i =0; i < s.length(); ++i)
13+
{
14+
if (memo[s[i] -'a'] != -1)
15+
{
16+
repeat[s[i] -'a'] =true;
17+
}
18+
19+
memo[s[i] -'a'] = i;
20+
}
21+
22+
int res = -1;
23+
for (int i =0; i <26; ++i)
24+
{
25+
if (repeat[i] ==false && memo[i] != -1)
26+
{
27+
if (res <0)
28+
{
29+
res = memo[i];
30+
}
31+
else
32+
{
33+
res =min(res, memo[i]);
34+
}
35+
}
36+
}
37+
38+
return res;
39+
}
40+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp