码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode: Count and Say

时间:2014-05-16 21:27:59      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   java   

一次过

bubuko.com,布布扣
 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 }
bubuko.com,布布扣

 

Leetcode: Count and Say,布布扣,bubuko.com

Leetcode: Count and Say

标签:style   blog   class   code   c   java   

原文地址:http://www.cnblogs.com/EdwardLiu/p/3725265.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!