Last active
December 14, 2015 15:18
-
-
Save guolinaileen/5106297 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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