Count and Say
来源:互联网 发布:iphonex专用软件 编辑:程序博客网 时间:2024/05/01 06:36
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.
Subscribe to see which companies asked this question
题意:
数数并打印
题目比较不直观,这里的描述比较好一些 http://www.careercup.com/question?id=4425679
"Count and Say problem" Write a code to do following:
n String to print
0 1
1 1 1 因为前面一行有1个1
2 2 1 因为前面一行有2个1
3 1 2 1 1 因为前面一行有1个2和1个1
...
Base case: n = 0 print "1"
for n = 1, look at previous string and write number of times a digit is seen and the digit itself. In this case, digit 1 is seen 1 time in a row... so print "1 1"
for n = 2, digit 1 is seen two times in a row, so print "2 1"
for n = 3, digit 2 is seen 1 time and then digit 1 is seen 1 so print "1 2 1 1"
for n = 4 you will print "1 1 1 2 2 1"
Consider the numbers as integers for simplicity. e.g. if previous string is "10 1" then the next will be "1 10 1 1" and the next one will be "1 1 1 10 2 1"
思路:
遍历,每次对前面的string进行分析,输出
代码:
- LeetCode: Count and Say
- [LeetCode]Count and Say
- LeetCode Count and Say
- [Leetcode] Count and Say
- Count and Say
- Leetcode: Count and Say
- [LeetCode] Count and Say
- LeetCode Count and Say
- Count and Say
- [38]Count and Say
- Count and Say
- Count and Say
- Count and Say
- [LeetCode]Count and Say
- Count and Say
- Count and Say
- [LeetCode] Count and Say
- LeetCode-Count and Say
- Java学习笔记(二十六):String.split()的使用
- ZCMU-1140-松哥
- Caffe installation on ubuntu16.04 sever
- 开通博客
- shell 1>&2 2>&1 &>filename重定向的含义和区别
- Count and Say
- 前端开发的模块化和组件化的定义,以及两者的关系?
- devexpress 破解版消除运行弹窗
- JQuery实现搜索关键词变红,包含关键词的div显示,无div的隐藏
- 只属于我自己的2016
- leetcode 53. Maximum Subarray(分治,递归)
- 2016年度GitHub上Stars最多的项目
- “兴趣爱好”,蜜糖or砒霜?
- 基本3D变换之World Transform, View Transform and Projection Transform