LeetCode解题报告-- Count and Say
来源:互联网 发布:用手机听小说的软件 编辑:程序博客网 时间:2024/05/01 23:02
题目:
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.
点击解题:Count and Say
分析:题意是要求通过程序生成一组符合题意要求的字符串,
题目难度不高,基本思路是:以n为例,直接从左向右扫描n-1字符串,计算出现相同数字的个数 ,直至扫描结束!示意图如下:初始:count = 1,,每遇到数字相同,count++,然后将count转为字符串或字符类型 + 当前数组字符 以此为规律找出nth字符串。
java代码: Accepted
public class Solution { public String countAndSay(int n) { String newS = "1"; int count = 1; int i = 1; while(i < n){ String s = newS; newS = ""; for(int j = 0;j < s.length();j ++){ if( (j + 1) < s.length() && s.charAt(j) == s.charAt(j + 1)){ count ++; }else{ newS = newS + count + s.charAt(j); count = 1; } } i ++; } return newS; }}
python代码 Accepted
class Solution(object): def countAndSay(self, n): """ :type n: int :rtype: str """ i = 1 count = 1 newS = "1" while i < n: s= newS newS = "" for j in range(len(s)): if((j + 1) < len(s) and s[j] == s[j + 1]): count = count + 1 else: newS = newS + str(count) + s[j] count = 1 i = i + 1 return newS
1 0
- LeetCode Count and Say 解题报告
- LeetCode解题报告-- Count and Say
- [leetcode] 38. Count and Say 解题报告
- [Leetcode] 38. Count and Say 解题报告
- 【LeetCode】Count and Say 解题报告
- Leetcode #38. Count and Say 数数报数 解题报告
- leetcode解题方案--038--count and say
- count and say 解题
- LeetCode: Count and Say
- [LeetCode]Count and Say
- LeetCode Count and Say
- [Leetcode] Count and Say
- Leetcode: Count and Say
- [LeetCode] Count and Say
- LeetCode Count and Say
- [LeetCode]Count and Say
- [LeetCode] Count and Say
- LeetCode-Count and Say
- android View 事件分发
- 俞敏洪老师2014年底写的
- LOG 调试中的V、D、I、W、E
- ListView滑动到底部加载数据
- python与BeautifulSouop计算SUMO仿真的到达速率
- LeetCode解题报告-- Count and Say
- JS获取浏览器窗口大小 获取屏幕,浏览器,网页高度宽度
- 【夜读】有些人注定不会失败
- 【转载】Android样式的开发:drawable汇总篇
- Oracle监听服务没有
- listview 用viewhold优化
- linux makefile 第一阶段 初级认识篇
- exit() 只析构全局变量不析构局部变量
- STM32 IAP文档 common.h文件参考