Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- public String countAndSay(int n) {
- if(n <= 0)
- return "";
- if(n == 1)
- return "1";
- String currNum = "1";
- for(int i=2; i<=n; i++) {
- currNum = countAndSay(currNum);
- }
- return currNum;
- }
- public String countAndSay(String num) {
- StringBuilder sb = new StringBuilder();
- for(int i=0; i<num.length(); i++) {
- int count = 1;
- char c = num.charAt(i);
- while(i < num.length() - 1 && num.charAt(i) == num.charAt(i+1)) {
- count++;
- i++;
- }
- sb.append(count);
- sb.append(c);
- }
- return sb.toString();
- }
- }
- /*
- int aint = 1;
- char achar = 1;
- //you will get "50" in this way because the + is not the string operator
- sb.append(aint+achar);
- //Correct code
- sb.append(aint);
- sb.append(achar);
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement