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.

题意分析:

  本题是将数字从1开始,将当前数字转化为口语对应的数字。比如1口语是1个1,记作11;11读作2个1,记作21;21读作1个2,1个1,记作1211……

  '1'是第一个数字,根据输入的数字n,计算第n个这样的数字。

  说起来比较拗口,对照着例子可以感受一下……

解答:

  如果你理解题意,那么解答就很简单。直接循环,每个数字按照题目要求翻译成口语对应的数字,按照顺序找到第n个返回就行了。

  Leetcode给的评级是Easy,这是对于程序难度来说,然而对于题意来说容易理解偏。

AC代码:

class Solution(object):
def countAndSay(self, n):
if n < 2: return ''
ret_str = ''
while n > 1:
temp, current_num = '', 0
for i, v in enumerate(ret_str):
if i > 0 and v != ret_str[i - 1]:
temp += str(current_num) + ret_str[i - 1]
current_num = 1
else:
current_num += 1
ret_str = temp + (str(current_num) + ret_str[-1] if current_num != 0 else '')
n -= 1
return ret_str

后话:   

  其实这道题有个很有意思的地方,即输出的数字中永远不可能有大于3的数字,只会出现1、2、3三个数字,你能够证明吗?

【LeetCode题意分析&解答】38. Count and Say的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. 【LeetCode题意分析&解答】43. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  5. 【LeetCode题意分析&解答】42. Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  6. 【LeetCode题意分析&解答】41. First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  7. 【LeetCode题意分析&解答】39. Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  8. 【LeetCode题意分析&解答】36. Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  9. 【LeetCode题意分析&解答】34. Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

随机推荐

  1. 转 C#String与string的区别

    C#是区分大小写的,但是我却发现C#中同时存在String与string,于是我很困惑,于是我上网搜索了一下,于是我了解了一些小知识. MSDN中对string的说明:string is an ali ...

  2. php汉字截取

    /** * 截取HTML,并自动补全闭合 * @param $html * @param $length * @param $end */ function subHtml($html,$length ...

  3. Android 支付宝接入时常见的问题

    1.概述 首先说明下,Android支付宝接入用的是快捷支付,下载地址是https://b.alipay.com/order/techService.htm    支付宝移动接入地址https://b ...

  4. 定时器——Cocos2d-x学习历程(十一)

    1.定时器 利用场景.层和精灵等游戏元素,我们可以构建游戏的框架,但是此时的游戏仍然是静止不动的.在一切游戏中,游戏的状态都会随着时间的流逝而改变,同时我们还需要定时进行一些逻辑判断,例如鱼和子弹的碰 ...

  5. Programming C#.Inheritance and Polymorphism

    继承 C#中,创建派生类要在派生类的名字后面加上冒号,后面再跟上基类的名字: public class ListBox : Control 提示:C++程序员注意了,C#没有私有或者保护继承 多态 继 ...

  6. python dict traversal

    rs=dict() rs['item1'] = 'item1' rs['item2'] = 'item2' for k,d in rs.items(): print k,d for k in rs.k ...

  7. 页面类跳转Demo

    package baidumapsdk.demo; import android.app.Activity; import android.content.BroadcastReceiver; imp ...

  8. javascript 中字符串之比较

    <script type="text/javascript"> var string1="apple"; var string2="Ban ...

  9. 安装solaris_11.2与windows双系统(VM10模拟实现)(一)

    感慨:这周刚接触solaris,装solaris很蛋疼,一个字:慢! 在上面安装软件包依然很慢,无线网也很不稳定. 在上面搭建环境更蛋疼,一个字:惨! 什么环境之类的废话就不多说了,一般的电脑都可以. ...

  10. Oracle EBS-SQL (SYS-5):sys_配置文件查询.sql

    select    distinct l.profile_option_name,             v.profile_option_value,             fu.user_na ...