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 or
1"11.
11 is read off as "two or
1s"21.
21 is read off as "one, then
2one 1" or 1211.
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string。
原题链接:https://oj.leetcode.com/problems/count-and-say/
能够看出,后一个数字是前个数字的读法,21读作1个2,先写下12,1个1,再写下11,连起来1211.
public static String countAndSay(int n) {
if(n <= 0)
return null;
String str = "1";
int count = 1;
for(int i=0;i<n-1;i++){
StringBuilder builder = new StringBuilder();
for(int j=0;j<str.length();j++){
if(j<str.length()-1 && str.charAt(j)==str.charAt(j+1))
count++;
else{
builder.append(count + "" + str.charAt(j));
count = 1;
}
}
str = builder.toString();
}
return str;
}
LeetCode——Count and Say的更多相关文章
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- [LeetCode] Count of Smaller Numbers After Self 计算后面较小数字的个数
You are given an integer array nums and you have to return a new counts array. The counts array has ...
- [LeetCode] Count Univalue Subtrees 计数相同值子树的个数
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- LeetCode Count of Range Sum
原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...
- LeetCode Count of Smaller Numbers After Self
原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...
- LeetCode Count Complete Tree Nodes
原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count ...
随机推荐
- NYOJ-开灯问题
开灯问题 时间限制:3000 ms | 内存限制:65535 KB 难度: 描写叙述 有n盏灯,编号为1~n.第1个人把全部灯打开,第2个人按下全部编号为2 的倍数的开关(这些灯将被关掉),第3 ...
- MVC 向页面传值方式总结
总结发现ASP.NET MVC中Controller向View传值的方式共有6种,分别是: ViewBag ViewData TempData 向普通View页面传一个Model对象 向强类型页面传传 ...
- tomcat启动后ids页面无法访问
修改servers-->tomcat6.0-->server.xml <Context docBase="/tds7030-web" path="&qu ...
- MDCC为移动开发者服务:一看、一聊、一聚
MDCC为移动开发者服务:一看.一聊.一聚-CSDN.NET MDCC为移动开发者服务:一看.一聊.一聚 发表于2013-11-05 20:54| 2698次阅读| 来源CSDN| 6 ...
- 饭卡(HDOJ2546)
饭卡 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
- android Gallery滑动不流畅的解决
import android.content.Context; import android.util.AttributeSet; import android.view.KeyEvent; impo ...
- ADO.NET 对象 结构图
- 全面解读Python Web开发框架Django
全面解读Python Web开发框架Django Django是一个开源的Web应用框架,由Python写成.采用MVC的软件设计模式,主要目标是使得开发复杂的.数据库驱动的网站变得简单.Django ...
- IOS开发之UIView总结
如果想调用某个类的某个方法可以写成这样,这个方法来自NSObject类 performSelector: performSelector:withObject: performSelector:wit ...
- Qt之文件操作 QFile
原地址:http://blog.csdn.net/liuhongwei123888/article/details/6084761 今天学习QT的文件操作 1.QIODevice直接继承自QObjec ...