File tree 4 files changed +85
-21
lines changed
lcci/01.06.Compress String
4 files changed +85
-21
lines changed Original file line number Diff line number Diff line change @@ -63,26 +63,49 @@ class Solution:
63
63
``` java
64
64
class Solution {
65
65
public String compressString (String S ) {
66
- if (S == null || S . length() < 2 ) {
66
+ int n;
67
+ if (S == null || (n = S . length()) < 2 ) {
67
68
return S ;
68
69
}
69
- char [] chars = S . toCharArray();
70
- int p = 0 , q = 1 , n = chars. length;
70
+ int p = 0 , q = 1 ;
71
71
StringBuilder sb = new StringBuilder ();
72
72
while (q < n) {
73
- if (chars[p] != chars[q] ) {
74
- sb. append(chars[p] ). append(q - p);
73
+ if (S . charAt(p) != S . charAt(q) ) {
74
+ sb. append(S . charAt(p) ). append(q - p);
75
75
p = q;
76
76
}
77
- q += 1 ;
77
+ ++ q ;
78
78
}
79
- sb. append(chars[p] ). append(q - p);
79
+ sb. append(S . charAt(p) ). append(q - p);
80
80
String res = sb. toString();
81
81
return res. length() < n ? res : S ;
82
82
}
83
83
}
84
84
```
85
85
86
+ ### ** JavaScript**
87
+
88
+ ``` js
89
+ /**
90
+ * @param {string} S
91
+ * @return {string}
92
+ */
93
+ var compressString = function (S ) {
94
+ if (! S ) return S ;
95
+ let p = 0 , q = 1 ;
96
+ let res = ' ' ;
97
+ while (q < S .length ) {
98
+ if (S [p] != S [q]) {
99
+ res += (S [p] + (q - p));
100
+ p = q;
101
+ }
102
+ ++ q;
103
+ }
104
+ res += (S [p] + (q - p));
105
+ return res .length < S .length ? res : S ;
106
+ };
107
+ ```
108
+
86
109
### ** ...**
87
110
88
111
```
Original file line number Diff line number Diff line change @@ -61,26 +61,49 @@ class Solution:
61
61
``` java
62
62
class Solution {
63
63
public String compressString (String S ) {
64
- if (S == null || S . length() < 2 ) {
64
+ int n;
65
+ if (S == null || (n = S . length()) < 2 ) {
65
66
return S ;
66
67
}
67
- char [] chars = S . toCharArray();
68
- int p = 0 , q = 1 , n = chars. length;
68
+ int p = 0 , q = 1 ;
69
69
StringBuilder sb = new StringBuilder ();
70
70
while (q < n) {
71
- if (chars[p] != chars[q] ) {
72
- sb. append(chars[p] ). append(q - p);
71
+ if (S . charAt(p) != S . charAt(q) ) {
72
+ sb. append(S . charAt(p) ). append(q - p);
73
73
p = q;
74
74
}
75
- q += 1 ;
75
+ ++ q ;
76
76
}
77
- sb. append(chars[p] ). append(q - p);
77
+ sb. append(S . charAt(p) ). append(q - p);
78
78
String res = sb. toString();
79
79
return res. length() < n ? res : S ;
80
80
}
81
81
}
82
82
```
83
83
84
+ ### ** JavaScript**
85
+
86
+ ``` js
87
+ /**
88
+ * @param {string} S
89
+ * @return {string}
90
+ */
91
+ var compressString = function (S ) {
92
+ if (! S ) return S ;
93
+ let p = 0 , q = 1 ;
94
+ let res = ' ' ;
95
+ while (q < S .length ) {
96
+ if (S [p] != S [q]) {
97
+ res += (S [p] + (q - p));
98
+ p = q;
99
+ }
100
+ ++ q;
101
+ }
102
+ res += (S [p] + (q - p));
103
+ return res .length < S .length ? res : S ;
104
+ };
105
+ ```
106
+
84
107
### ** ...**
85
108
86
109
```
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public String compressString (String S ) {
3
- if (S == null || S .length () < 2 ) {
3
+ int n ;
4
+ if (S == null || (n = S .length ()) < 2 ) {
4
5
return S ;
5
6
}
6
- char [] chars = S .toCharArray ();
7
- int p = 0 , q = 1 , n = chars .length ;
7
+ int p = 0 , q = 1 ;
8
8
StringBuilder sb = new StringBuilder ();
9
9
while (q < n ) {
10
- if (chars [ p ] != chars [ q ] ) {
11
- sb .append (chars [ p ] ).append (q - p );
10
+ if (S . charAt ( p ) != S . charAt ( q ) ) {
11
+ sb .append (S . charAt ( p ) ).append (q - p );
12
12
p = q ;
13
13
}
14
- q += 1 ;
14
+ ++ q ;
15
15
}
16
- sb .append (chars [ p ] ).append (q - p );
16
+ sb .append (S . charAt ( p ) ).append (q - p );
17
17
String res = sb .toString ();
18
18
return res .length () < n ? res : S ;
19
19
}
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } S
3
+ * @return {string }
4
+ */
5
+ var compressString = function ( S ) {
6
+ if ( ! S ) return S ;
7
+ let p = 0 , q = 1 ;
8
+ let res = '' ;
9
+ while ( q < S . length ) {
10
+ if ( S [ p ] != S [ q ] ) {
11
+ res += ( S [ p ] + ( q - p ) ) ;
12
+ p = q ;
13
+ }
14
+ ++ q ;
15
+ }
16
+ res += ( S [ p ] + ( q - p ) ) ;
17
+ return res . length < S . length ? res : S ;
18
+ } ;
You can’t perform that action at this time.
0 commit comments