Advertisement
Guest User

Untitled

a guest
May 30th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. public class Solution {
  2. public String countAndSay(int n) {
  3. if(n <= 0)
  4. return "";
  5. if(n == 1)
  6. return "1";
  7. String currNum = "1";
  8. for(int i=2; i<=n; i++) {
  9. currNum = countAndSay(currNum);
  10. }
  11. return currNum;
  12. }
  13. public String countAndSay(String num) {
  14. StringBuilder sb = new StringBuilder();
  15. for(int i=0; i<num.length(); i++) {
  16. int count = 1;
  17. char c = num.charAt(i);
  18. while(i < num.length() - 1 && num.charAt(i) == num.charAt(i+1)) {
  19. count++;
  20. i++;
  21. }
  22. sb.append(count);
  23. sb.append(c);
  24. }
  25. return sb.toString();
  26. }
  27. }
  28. /*
  29. int aint = 1;
  30. char achar = 1;
  31. //you will get "50" in this way because the + is not the string operator
  32. sb.append(aint+achar);
  33. //Correct code
  34. sb.append(aint);
  35. sb.append(achar);
  36. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement