File tree 2 files changed +30
-1
lines changed
2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change 13
13
| 189| [ 轮转数组] ( https://leetcode-cn.com/problems/rotate-array/ ) | [ JavaScript] ( ./algorithms/rotate-array.js ) | Medium|
14
14
| 344| [ 反转字符串] ( https://leetcode-cn.com/problems/reverse-string/ ) | [ JavaScript] ( ./algorithms/reverse-string.js ) | Easy|
15
15
| 349| [ 两个数组的交集] ( https://leetcode-cn.com/problems/intersection-of-two-arrays/ ) | [ JavaScript] ( ./algorithms/intersection-of-two-arrays.js ) | Easy|
16
- | 350| [ 两个数组的交集 II] ( https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/ ) | [ JavaScript] ( ./algorithms/intersection-of-two-arrays-ii.js ) | Easy|
16
+ | 350| [ 两个数组的交集 II] ( https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/ ) | [ JavaScript] ( ./algorithms/intersection-of-two-arrays-ii.js ) | Easy|
17
+ | 387| [ 字符串中的第一个唯一字符] ( https://leetcode-cn.com/problems/first-unique-character-in-a-string/ ) | [ JavaScript] ( ./algorithms/first-unique-character-in-a-string.js ) | Easy|
Original file line number Diff line number Diff line change
1
+ // 方法一:Map(映射)
2
+
3
+ /**
4
+ * 字符串中的第一个唯一字符
5
+ * @param {string } s
6
+ * @return {number }
7
+ */
8
+ var firstUniqChar = function ( s ) {
9
+ const map = new Map ( ) ;
10
+
11
+ for ( let i = 0 ; i < s . length ; i ++ ) {
12
+ let char = s [ i ] ;
13
+
14
+ if ( map . has ( char ) ) {
15
+ let count = map . get ( char ) ;
16
+ map . set ( char , count + 1 ) ;
17
+ } else {
18
+ map . set ( char , 1 ) ;
19
+ }
20
+ }
21
+ for ( let i = 0 ; i < s . length ; i ++ ) {
22
+ if ( map . get ( s [ i ] ) === 1 ) {
23
+ return i ;
24
+ }
25
+ }
26
+
27
+ return - 1 ;
28
+ } ;
You can’t perform that action at this time.
0 commit comments