Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 555086b

Browse files
author
zongyanqi
committed
add Easy_387_First_Unique_Character_in_a_String
1 parent 09d99f1 commit 555086b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
3+
4+
Examples:
5+
6+
s = "leetcode"
7+
return 0.
8+
9+
s = "loveleetcode",
10+
return 2.
11+
Note: You may assume the string contain only lowercase letters.
12+
13+
14+
*/
15+
16+
/**
17+
* @param {string} s
18+
* @return {number}
19+
*/
20+
var firstUniqChar = function (s) {
21+
22+
for (var i = 0; i < s.length; i++) {
23+
var ch = s[i];
24+
if (s.lastIndexOf(ch) === s.indexOf(ch)) return i;
25+
}
26+
return -1;
27+
};
28+
29+
console.log(firstUniqChar('leetcode'));
30+
console.log(firstUniqChar('loveleetcode'));

0 commit comments

Comments
 (0)