首页 > 代码库 > Leetcode: Count and Say
Leetcode: Count and Say
一次过
1 public class Solution { 2 public String countAndSay(int n) { 3 if (n <= 0) return ""; 4 int i = 1; 5 String current = "1"; 6 while (i < n){ 7 current = getnext(current); 8 i++; 9 } 10 return current; 11 } 12 13 public String getnext(String current){ 14 int j = 0, count = 0; 15 char st = current.charAt(0); 16 StringBuffer result = new StringBuffer(); 17 while (j < current.length()){ 18 if (current.charAt(j) == st){ 19 count++; 20 j++; 21 } else{ 22 result.append(count); 23 result.append(st); 24 st = current.charAt(j); 25 count = 1; 26 j++; 27 } 28 } 29 result.append(count); 30 result.append(st); 31 return result.toString(); 32 } 33 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。