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. SQL Server数据库---》增删查改

    ***数据的插入:(增) insert into 表名(字段列表) values(值列表) 如果不写字段列表就要为表添加全部的列数据 其实into也可以省略 每次只能插入一条数据 1.如果字段可以为n ...

  2. CSS3 border属性的妙用

    .ribbon { background: #45c9c8; position: absolute; width: 75px; height: 25px; line-height: 25px; top ...

  3. 解决VS2010打开Web页面时经常由于内存较低而导致VS2010自动关闭的问题

    在使用VS2010 开发Web应用程序的时候,经常打开一个Web页面进行编辑前台代码的时候要等待很久(甚至等了半天结果还挂掉,简直令人抓狂), 之前也在网上找了很多相关的方法,都没办法解决,今天无意中 ...

  4. 获取UIButton的一些属性

    获取文字  button.currentTitle 更多如下: @property(nullable, nonatomic,readonly,strong) NSString *currentTitl ...

  5. CSS中链接文本为图片时的问题(塌陷、对应的图片建立倒角的问题)

    我在做Javascript DOM编程艺术的时候,在12章自己做练习时遇到了一个问题,<a>的内容<img>从<a>的盒子中溢出.代码如下: <a href= ...

  6. 灵光一闪-VS设计界面能访问到private修饰的各种控件

    大家都知道,用VS设计界面时,VS默认控件的访问修饰符为private,但是我就很奇怪,private修饰的字段不是只有类内部才能访问吗? 好神奇的VS,这到底是怎么实现的?难道就是类似文本编辑器的作 ...

  7. 不要伤害指针(5)--void和void指针详解

    原文转载地址:http://blog.csdn.net/sunchaoenter/article/details/6587426 增加自己的想法,作为笔记. 1.概述 许多初学者对C/C++语言中的v ...

  8. c语言中的#ifndef、#def、#endif等宏是什么意思

    #ifndef.(或者#ifndef).#def.#endif等宏这几个宏是为了进行条件编译.一般情况下,源程序中所有的行都参加编译.但是有时希望对其中一部分内容只在满足一定条件才进行编译,也就是对一 ...

  9. Matlab图像直方图相关函数

    图像的灰度直方图(H是图像a.bmp的数据矩阵) imhist(H):%显示a的直方图 histeq(H); %将图像a进行直方图均衡化 adapthisteq(H); %将图像a进行直方图均衡化 i ...

  10. CreateFileMapping共享内存时添加Global的作用

    来源:http://www.cnblogs.com/elvislogs/articles/ShareMemory.html 通常使用CreateFileMapping建立共享内存时名称中没有加入&qu ...