LeetCode Count and Say

题目:

The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.

Given an integer n, generate the nth sequence.

Note: The sequence of integers will be represented as a string.
题意:

第一个为1,第二个数的第一位表示前一个数的每一位的数字出现的个数,然后后一位表示的是哪一个数字。

比如:1,11,21,1211,111221,312211,13112221...以此类推。此题必须要依次来做,因为后一个数是基于前一个数的,所以要用循环依次来做。我考虑用StringBuilder来模拟,每一次有新的数字加进来,那么就用StringBuilder来生成新的数字组合。用双层循环来做,但是LZ有一个问题,就是每做一次前,都是将0和1次单独考虑,但是这会带来一个问题,那就是代码比较复杂,但我看了一些网上的相关代码,发现其实有些地方不用考虑这么细,可以用一次循环就可以实现。

public static String countAndSay(int n)
	{
		String str = new String();
		if(n == 0)
			return null;
		else
		{
			if(n == 1)
				str = "1";
			else 
			{
				str = "1";
				for(int i = 1; i < n; i++)
				{
					int length = str.length();
					int j = 1;
					StringBuilder sb = new StringBuilder();
					int num = 0;
					Set uniqueSet = new HashSet();
					uniqueSet.add(str.charAt(0));
					num += 1;
					if(j == length)       //针对n == 2的情况
					{
						sb.append(num);
						sb.append(str.charAt(0));
						str = sb.toString();
						num = 0;
					}
					while(j < length)
					{
						if(uniqueSet.contains(str.charAt(j)))
						{
							num += 1;
							j += 1;
						}
						else if(!uniqueSet.contains(str.charAt(j)))
						{
							uniqueSet.clear();
							sb.append(num);
							num = 1;
							sb.append(str.charAt(j - 1));
							uniqueSet.add(str.charAt(j));
							j += 1;
						}
						if(j == length)   //循环到末尾,特别要注意
						{
							sb.append(num);
							num = 0;
							sb.append(str.charAt(j - 1));
							uniqueSet.clear();   //用这种方式来让set清空
						}
					}
					str = sb.toString();
				}
			}
			return str;
		}
	}

另附网上的其他的一个代码:

public class Solution <span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;">{  </span>
    String countAndSayForOneString(String input) {  
        char tmp = input.charAt(0);  
        int  num = 1;  
        StringBuffer newString = new StringBuffer("");  
        for(int k=1;k<input.length();k++) {  
            if(input.charAt(k)==tmp) {  
                num++;  
                continue;  //注意这里用了continue,这个是结束此次循环,也就是不执行if外面的语句,与break有区别。但是应该要想到这么用。
            }  
            newString.append(Integer.toString(num) + tmp);  
            tmp = input.charAt(k);  
            num = 1;  
        }  
        newString.append(Integer.toString(num) + tmp);  
        return newString.toString();  
    }  
      
    public String countAndSay(int n) {  
        String result = "1";  
        int i = 1;  
        while(i<n) {  
            result = countAndSayForOneString(result);  
            i++;  
        }  
        return result;  
    }  
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值