File tree 2 files changed +51
-22
lines changed
2 files changed +51
-22
lines changed Original file line number Diff line number Diff line change 3
3
* @return {number }
4
4
*/
5
5
var compress = function ( chars ) {
6
- let charAppearMap = new Map ( ) , s = [ ]
6
+ let index = 0 , i = 0 , j = 0
7
7
8
- for ( let i = 0 ; i < chars . length ; i ++ ) {
9
- charAppearMap . set ( chars [ i ] , ( charAppearMap . get ( chars [ i ] ) ?? 0 ) + 1 ) ;
10
- }
8
+ console . log ( chars )
11
9
12
- for ( let [ key , value ] of charAppearMap ) {
13
- if ( value === 1 ) {
14
- s . push ( key ) ;
15
- } else {
16
- s . push ( key ) ;
17
- let keys = value . toString ( ) . split ( "" ) ;
18
- while ( keys . length > 0 ) {
19
- let key = keys . shift ( ) ;
20
- s . push ( key ) ;
21
- }
10
+ while ( i < chars . length ) {
11
+ while ( chars [ i ] === chars [ j ] ) {
12
+ j ++ ;
22
13
}
23
- }
24
14
25
- console . log ( "s" )
26
- console . log ( s )
27
- for ( let i = 0 ; i < s . length ; i ++ ) {
28
- chars [ i ] = s [ i ] ;
15
+ let
16
+ l = j - i ,
17
+ s = `${ chars [ i ] } ${ ( l == 1 ) ? "" : l } ` . split ( "" ) ;
18
+
19
+ console . log ( `s: ${ s } , s.length: ${ s . length } ` )
20
+
21
+ while ( s . length > 0 ) {
22
+ let curr = s . shift ( ) ;
23
+ console . log ( `index: ${ index } , curr: ${ curr } ` )
24
+ chars [ index ] = curr ;
25
+ index ++
26
+ }
27
+
28
+ i = j ;
29
+ j ++ ;
30
+
29
31
}
30
32
31
- console . log ( "char" )
32
33
console . log ( chars )
33
- console . log ( chars . length )
34
- return s . length ;
34
+
35
+ return index ;
35
36
} ;
36
37
37
38
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {character[] } chars
3
+ * @return {number }
4
+ */
5
+ var compress = function ( chars ) {
6
+ let index = 0 , i = 0 , j = 0
7
+
8
+ while ( i < chars . length ) {
9
+ while ( chars [ i ] === chars [ j ] ) {
10
+ j ++ ;
11
+ }
12
+
13
+ let
14
+ l = j - i ,
15
+ s = `${ chars [ i ] } ${ ( l == 1 ) ? "" : l } ` . split ( "" ) ;
16
+
17
+ while ( s . length > 0 ) {
18
+ let curr = s . shift ( ) ;
19
+ chars [ index ] = curr ;
20
+ index ++
21
+ }
22
+
23
+ i = j ;
24
+ j ++ ;
25
+ }
26
+
27
+ return index ;
28
+ } ;
You can’t perform that action at this time.
0 commit comments