leetcode(16).38. Count and Say
来源:互联网 发布:最好的电子狗软件 编辑:程序博客网 时间:2024/06/05 11:29
题意:
对于一个整数序列:1, 11, 21, 1211, 111221, ...
第二个数是11是因为:第一个数是1-> 1个1 ->11
第三个数是21是因为:第二个数是11 -> 2个1 ->21
返回整数序列的第n个是什么
初步分析:根据n,从零开始遍历到n,一个一个的生成。
每一次都遍历上一次生成的字符串,如果相邻相同的时候,计数加1(相同的个数).最后每次循环结束:string更新为string+计数+那个数。
代码:
public class Solution { public String countAndSay(int n) { String now = "1"; //由题,从1开始 int count = 1; for(int i=1; i<n; i++) { char pre[] = now.toCharArray(); now = ""; for(int j = 0;j<pre.length;j++) //遍历要分析的字符串(上一个字符串) { count = 1; //不相等就是一个 while(j+1<pre.length && pre[j] == pre[j+1]) { count++; j++; } now = now + count + pre[j]; } } return now; }}
0 0
- leetcode(16).38. Count and Say
- [LeetCode]38.Count and Say
- LeetCode --- 38. Count and Say
- LeetCode 38.Count and Say
- [Leetcode] 38. Count and Say
- 38. Count and Say LeetCode
- Leetcode 38. Count and Say
- leetcode 38. Count and Say
- [leetcode] 38. Count and Say
- LeetCode-38.Count and Say
- LeetCode *** 38. Count and Say
- leetcode 38. Count and Say
- LeetCode-38. Count and Say
- LeetCode 38. Count and Say
- LeetCode 38. Count and Say
- Leetcode - 38. Count and Say
- LeetCode 38. Count and Say
- Leetcode 38. Count and Say
- C++中string substr()函数的使用(截取子字符串的函数)
- golang中获取字符串长度的几种方法
- Linux ps命令详解与示例说明
- http的各个状态码代表的意思
- JavaEE:Hibernate笔记
- leetcode(16).38. Count and Say
- 机器学习小组知识点14:均匀分布(Uniform Distribution)
- JS的学习
- 数据库索引的创建原则
- Linux 中rsync 详解 备份或同步必选
- pod 淘宝镜像不能用了卡着不动
- LayoutInflater源码分析之Inflate
- 玩具谜题(NOIP2016提高组Day1T1)
- 加速计陀螺仪6轴数据融合算法解析