Skip to content

Instantly share code, notes, and snippets.

@guolinaileen
Last active December 14, 2015 15:18
Show Gist options
  • Select an option

  • Save guolinaileen/5106297 to your computer and use it in GitHub Desktop.

Select an option

Save guolinaileen/5106297 to your computer and use it in GitHub Desktop.
public class Solution {
public String countAndSay(int n) {
// Start typing your Java solution below
// DO NOT write main() function
String result="1";
while(n>1)
{
int length=result.length();
StringBuffer sb=new StringBuffer();
int count=1;
for(int i=1; i<length; i++)
{
if(result.charAt(i)==result.charAt(i-1)) count++;
else {
sb.append(Integer.toString(count)); sb.append(result.charAt(i-1)); count=1;
}
}
sb.append(Integer.toString(count)); sb.append(result.charAt(length-1));
n--;
result=sb.toString();
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment