Received SR reply TSC# EB23 - 劳工卡
H*l
1 楼
我把重复的位置记下来,从第7个开始,整体的string就是6部分的string合起来。
但是每次都是
Output Limit Exceeded
不知道为什么。。。
题目是:
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
--------------------------------------------------------------
public String countAndSay(int n) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList lis = new ArrayList();
lis.add("1");
lis.add("11");
lis.add("21");
lis.add("1211");
lis.add("111221");
lis.add("2111121211");
int [] A = {2, 1, 0, 2, 2, 0};
if (n == 0) {
String s = "";
return s;
}
if (n < lis.size()) return lis.get(n - 1);
int count = lis.size();
while (count <= n) {
String str = "";
for (int i = 0; i < A.length; i++) {
str = str + lis.get(A[i] + 1);
A[i]++;
}
lis.add(str);
count++;
}
return lis.get(n - 1);
}
但是每次都是
Output Limit Exceeded
不知道为什么。。。
题目是:
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
--------------------------------------------------------------
public String countAndSay(int n) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList
lis.add("1");
lis.add("11");
lis.add("21");
lis.add("1211");
lis.add("111221");
lis.add("2111121211");
int [] A = {2, 1, 0, 2, 2, 0};
if (n == 0) {
String s = "";
return s;
}
if (n < lis.size()) return lis.get(n - 1);
int count = lis.size();
while (count <= n) {
String str = "";
for (int i = 0; i < A.length; i++) {
str = str + lis.get(A[i] + 1);
A[i]++;
}
lis.add(str);
count++;
}
return lis.get(n - 1);
}