File tree Expand file tree Collapse file tree 4 files changed +85
-21
lines changed
lcci/01.06.Compress String Expand file tree Collapse file tree 4 files changed +85
-21
lines changed Original file line number Diff line number Diff line change @@ -63,26 +63,49 @@ class Solution:
6363``` java
6464class Solution {
6565 public String compressString (String S ) {
66- if (S == null || S . length() < 2 ) {
66+ int n;
67+ if (S == null || (n = S . length()) < 2 ) {
6768 return S ;
6869 }
69- char [] chars = S . toCharArray();
70- int p = 0 , q = 1 , n = chars. length;
70+ int p = 0 , q = 1 ;
7171 StringBuilder sb = new StringBuilder ();
7272 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);
7575 p = q;
7676 }
77- q += 1 ;
77+ ++ q ;
7878 }
79- sb. append(chars[p] ). append(q - p);
79+ sb. append(S . charAt(p) ). append(q - p);
8080 String res = sb. toString();
8181 return res. length() < n ? res : S ;
8282 }
8383}
8484```
8585
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+
86109### ** ...**
87110
88111```
Original file line number Diff line number Diff line change @@ -61,26 +61,49 @@ class Solution:
6161``` java
6262class Solution {
6363 public String compressString (String S ) {
64- if (S == null || S . length() < 2 ) {
64+ int n;
65+ if (S == null || (n = S . length()) < 2 ) {
6566 return S ;
6667 }
67- char [] chars = S . toCharArray();
68- int p = 0 , q = 1 , n = chars. length;
68+ int p = 0 , q = 1 ;
6969 StringBuilder sb = new StringBuilder ();
7070 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);
7373 p = q;
7474 }
75- q += 1 ;
75+ ++ q ;
7676 }
77- sb. append(chars[p] ). append(q - p);
77+ sb. append(S . charAt(p) ). append(q - p);
7878 String res = sb. toString();
7979 return res. length() < n ? res : S ;
8080 }
8181}
8282```
8383
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+
84107### ** ...**
85108
86109```
Original file line number Diff line number Diff line change 11class Solution {
22 public String compressString (String S ) {
3- if (S == null || S .length () < 2 ) {
3+ int n ;
4+ if (S == null || (n = S .length ()) < 2 ) {
45 return S ;
56 }
6- char [] chars = S .toCharArray ();
7- int p = 0 , q = 1 , n = chars .length ;
7+ int p = 0 , q = 1 ;
88 StringBuilder sb = new StringBuilder ();
99 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 );
1212 p = q ;
1313 }
14- q += 1 ;
14+ ++ q ;
1515 }
16- sb .append (chars [ p ] ).append (q - p );
16+ sb .append (S . charAt ( p ) ).append (q - p );
1717 String res = sb .toString ();
1818 return res .length () < n ? res : S ;
1919 }
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