-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path38.java
More file actions
139 lines (134 loc) · 4.06 KB
/
38.java
File metadata and controls
139 lines (134 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
__________________________________________________________________________________________________
0ms
class Solution {
public String countAndSay(int n) {
String current = "1";
for (int i = 1; i < n; i++)
current = counter(current);
return current;
}
public String counter(String n) {
char[] digits = n.toCharArray();
StringBuilder sb = new StringBuilder();
char prev = '\0';
int counter = 0;
for (char digit : digits) {
if (digit == prev) {
counter++;
} else {
if (prev != '\0')
sb.append(counter).append(prev);
prev = digit;
counter = 1;
}
}
return sb.append(counter).append(prev).toString();
}
public static void main(String[] args) {
System.out.println(new Solution().countAndSay(1024));
}
}
__________________________________________________________________________________________________
1ms
public class Solution {
public String countAndSay(int n) {
if (n <= 0) {
return "";
}
StringBuilder sb = new StringBuilder();
sb.append(1);
while (--n > 0) {
char[] prev = sb.toString().toCharArray();
sb.setLength(0);
for (int i = 0; i < prev.length; ++i) {
int cnt = 1;
while (i + 1 < prev.length && prev[i] == prev[i+1]) {
i++;
cnt++;
}
sb.append(cnt);
sb.append(prev[i]);
}
}
return sb.toString();
}
}
__________________________________________________________________________________________________
2ms
class Solution {
public String countAndSay(int n) {
String str = "1";
for (int i = 2; i <= n; i++) {
StringBuilder builder = new StringBuilder();
int count = 1;
char currentCh = str.charAt(0);
for (int j = 1; j < str.length(); j++) {
if (currentCh == str.charAt(j)) {
count++;
} else {
builder.append(String.valueOf(count));
builder.append(currentCh);
currentCh = str.charAt(j);
count = 1;
}
}
builder.append(String.valueOf(count));
builder.append(currentCh);
str = builder.toString();
}
return str;
}
}
__________________________________________________________________________________________________
34264 kb
public class Solution {
public String countAndSay(int n) {
String s = "1";
for(int i = 1; i < n; i++){
s = countIdx(s);
}
return s;
}
public String countIdx(String s){
StringBuilder sb = new StringBuilder();
char c = s.charAt(0);
int count = 1;
for(int i = 1; i < s.length(); i++){
if(s.charAt(i) == c){
count++;
}
else
{
sb.append(count);
sb.append(c);
c = s.charAt(i);
count = 1;
}
}
sb.append(count);
sb.append(c);
return sb.toString();
}
}
__________________________________________________________________________________________________
36416 k
class Solution {
public String countAndSay(int n) {
String cur = "1";
while(--n>0){
int count = 1;
StringBuilder sb = new StringBuilder();
for(int i = 0; i<cur.length(); i++){
if(i==cur.length()-1 || cur.charAt(i)!=cur.charAt(i+1)){
sb.append(count).append(cur.charAt(i));
count=1;
}else{
count++;
}
}
cur = sb.toString();
}
return cur;
}
}
__________________________________________________________________________________________________