File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ ```
2
+ public static String reverseWords(String s) {
3
+ int len = s.length();
4
+ int j = len;
5
+ String result = "";
6
+ for (int i = len - 1; i >= 0; i--) {
7
+ if (s.charAt(i) == ' '){
8
+ j = i ;
9
+ } else if (i == 0 || s.charAt(i - 1) == ' ') {
10
+ if (result.length() != 0) {
11
+ result += ' ';
12
+ }
13
+ result += s.substring(i, j);
14
+ }
15
+ }
16
+ return result;
17
+ }
Original file line number Diff line number Diff line change
1
+ ```
2
+ public int compress(char[] chars) {
3
+ int index = 0;
4
+ if (chars.length < 2) {
5
+ return chars.length;
6
+ }
7
+
8
+ for (int i = 0; i < chars.length; ) {
9
+ int times = 1;
10
+ chars[index++] = chars[i++];
11
+ while (i < chars.length && chars[i] == chars[i - 1]) {
12
+ times++;
13
+ i++;
14
+ }
15
+ if (times > 1) {
16
+ char[] chs = String.valueOf(times).toCharArray();
17
+ for (int j = 0; j < chs.length; j++) {
18
+ chars[index++] = chs[j];
19
+ }
20
+ }
21
+ }
22
+ return index;
23
+ }
You can’t perform that action at this time.
0 commit comments