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. HTML文本框

    文本框样式大全   输入框景背景透明:<input style="background:transparent;border:1px solid #ffffff"> 鼠 ...

  2. c++中自增(++)和自减(--)操作符

    自增(++)和自减(--)操作符为对象加1 或减1 操作提供了方便简短的实现方式.它们有前置和后置两种使用形式.到目前为止,我们已经使用过前自增操作,该操作使其操作数加1,操作结果是修改后的值.同理, ...

  3. CRM odata方法如何使用$top

    odata方法 $top $top1 取1个 ¥top100取100个,放在$select前,中间用&符号隔开. 例如: var activeserviceReq = "/xrmse ...

  4. 在EBS中如何创建CUX_TOP

    1.创建cux用户1.1.创建表空间SQL> conn / as sysdba;Connected.SQL> create tablespace CUXD datafile '/vis/d ...

  5. sqlite3插入日期时间出错解决

    正确写法 insert into hhf_records(RegistrationNumber,MachinesNumber,InDataTime,Flag,CType) values (11,1,d ...

  6. 监控mysql执行的sql语句

    linux平台 监控mysql执行的sql语句   为了做好配合开发做性能和功能测试,方便监控正在执行的sql语句, 可以在/etc/mysqld中添加如下:  log =/usr/local/mys ...

  7. html语法之--使用图像映射

    1 什么是图像映射所谓图像映射是指在一幅图中定义若干个区域,每个区域中指定一个不同的超链接,当单击不同的区域时便可以跳转到相应的目标页面. 2 创建图像映射 2.1 定义映射区域 定义映射区域使用MA ...

  8. 软件测试学习日志————round 0 An impressed error in my past projects

    在初学各种语言时总会出现各种错误,比如main携程mian.忘了加各种库,打错字等等等等.虽然这些错误后面看来很幼稚,但是有的时候真的会让人印象很深刻. 在初学JavaScript时,我对JavaSc ...

  9. Oracle EBS-SQL (SYS-20):职责使用菜单2.sql

    select frt.responsibility_name,         fr.menu_id,         fm.menu_name,         fr.request_group_i ...

  10. cad画指定大小矩形

    指定基点后输入(@长度,宽度)回车 举例:如你要画个600*300的矩形 则输入@600,300回车