(Java)leetcode-38 Count and Say (令人费解的题目)

博客围绕LeetCode的报数题展开,该题要求输入1 - 30的行数输出对应行内容。介绍了两种解题思路,一是递归,利用每一行结果依赖上一行的关系,用递归从第一行推算当前行;二是迭代,将递归换成迭代,节省堆栈开销,还给出了相应Java代码及提交结果。

题目

【报数】
The count-and-say sequence is the sequence of integers with the first five terms as following:

  1. 1
    
  2. 11
    
  3. 21
    
  4. 1211
    
  5. 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 where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

这道题目来来回回、回回来来地看了许多遍才get到它的意思:

初始值第一行是 1。
第二行读第一行,1 个 1,去掉个字,所以第二行就是 11。
第三行读第二行,2 个 1,去掉个字,所以第三行就是 21。
第四行读第三行,1 个 2,1 个 1,去掉所有个字,所以第四行就是 1211。
第五行读第四行,1 个 1,1 个 2,2 个 1,去掉所有个字,所以第五航就是 111221。
第六行读第五行,3 个 1,2 个 2,1 个 1,去掉所以个字,所以第六行就是 312211。
然后题目要求输入 1 - 30 的任意行数,输出该行是啥。

例子扩展:

  1. 1
    
  2. 11
    
  3. 21
    
  4. 1211
    
  5. 111221 
    
  6. 312211
    
  7. 13112221
    
  8. 1113213211
    
  9. 31131211131221
    
  10. 13211311123113112211

思路1:递归

读懂题目后易知,每一行的结果依赖于上一行的结果,这样就可以形成递归的关系。
那么如何对上一行的结果进行表示呢?由例子中的规律可知,每一行连续相等的数字x被计数一次,若有n个,则在下一行写入“n个x”;继续读下一个连续相等的数字y,有m个,则在下一行写入“m个y”…此时下一行看起来是这样的“nxmy…”,这样,根据上一行的结果可以将当前行表示出来。
于是,代码用递归的方法从第一行推算到当前行。

代码1

涉及到写字符串的时候,使用StringBuilder是一种不错的选择

class Solution{
public String countAndSay(int n) {
        if(n==1)//退出条件
            return "1";

        StringBuilder sb=new StringBuilder();

        //找到n-1的结果
        String str=countAndSay(n-1);
       
        //对n-1的结果进行表示
        char c='0';//表示当前正在计数的字符
        int count=0;//计数
        for (int i=0;i<str.length();i++){//遍历上一个结果的字符串
            c=str.charAt(i);
            count=1;
            while ((i+1)<str.length()&&str.charAt(i+1)==c){//前面的判断防止数组下标越界
            	//若后一个字符与当前字符相等
                count++;//计数+1
                i++;//指针后移
            }
            sb.append(count+""+c);//计数+数字写入字符串缓冲区sb
        }
        return sb.toString();
    }
}

提交结果

Runtime: 2 ms, faster than 92.85% of Java online submissions for Count and Say.
Memory Usage: 36.9 MB, less than 32.42% of Java online submissions for Count and Say.

代码1.1

依然是递归,在读上一行写当前行的循环中做了修改,大同小异

class Solution{
public String countAndSay(int n) {
        if(n==1)//退出条件
            return "1";

        StringBuilder sb=new StringBuilder();

        //找到n-1的结果
        String str=countAndSay(n-1);
       
        //对n-1的结果进行表示
        char c=str.charAt(0);
        int count=1;
        for (int i=1;i<str.length();i++){//遍历上一个结果的字符串
            
            
            if (str.charAt(i)!=c){
            	sb.append(count).append(c);
            	count = 1;
            	c=str.charAt(i);
            }
            else count++;
            
        }
        sb.append(count).append(c);//计数+数字写入字符串缓冲区sb
        return sb.toString();
    }
}

提交结果

Runtime: 1 ms, faster than 100.00% of Java online submissions for Count and Say.
Memory Usage: 36.8 MB, less than 42.86% of Java online submissions for Count and Say.

思路2:迭代

表示方法依然与思路1相同,只不过把递归换成了迭代,第一层循环完成从第2行到目标行的推移,第二层循环完成“根据上一行写出当前行”的操作。
这样的方法节省了堆栈的开销。

代码2

public class Solution {
    public String countAndSay(int n) {
	    	StringBuilder curr=new StringBuilder("1");
	    	StringBuilder prev;
	    	int count;
	    	char say;
	        for (int i=1;i<n;i++){
	        	prev=curr;//上一次循环的当前行->本次循环的上一行
	 	        curr=new StringBuilder(); //用来写入当前行      
	 	        count=1;
	 	        say=prev.charAt(0);//表示当前正在计数的字符
	 	        
	 	        for (int j=1,len=prev.length();j<len;j++){//遍历上一行
	 	        	if (prev.charAt(j)!=say){
	 	        		curr.append(count).append(say);
	 	        		count=1;//重置计数
	 	        		say=prev.charAt(j);//移到下一个字符开始重新计数
	 	        	}
	 	        	else count++;//否则相等,计数+1
	 	        }
	 	        curr.append(count).append(say);//最后一组元素
	        }	       	        
	        return curr.toString();
        
    }
}

提交结果

Runtime: 2 ms, faster than 92.85% of Java online submissions for Count and Say.
Memory Usage: 36.6 MB, less than 98.88% of Java online submissions for Count and Say.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值