您的位置:首页 > 其它

【转载】VMware 宿主机没插网线无法与虚拟机通讯如何解决?

2011-09-23 10:49 507 查看
http://leetcode.com/onlinejudge#question_38

Question:

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.

Solution:

Recursive.

public class Solution {
public String countAndSay(int n) {
// Start typing your Java solution below
// DO NOT write main() function
int i = 1;
String[] result = new String
;
while(i<=n){
if(i==1){
result[i-1] = "1";
}else {
getNext(result, i-1);
}
i++;
}
return result[n-1];
}

public void getNext(String[] aray, int index){
String cur = aray[index-1];
String next = "";
for(int i=0; i< cur.length();){

int j = i;
while(j<cur.length()){
if(cur.charAt(j) == cur.charAt(i)){
j++;
}else {
break;
}
}

next += j-i;
next += cur.charAt(i);
i=j;
}
aray[index] = next;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐