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. tomcat的catalina

    Tomcat的核心分为3个部分: (1)Web容器---处理静态页面: (2)catalina --- 一个servlet容器-----处理servlet; (3)还有就是JSP容器,它就是把jsp页 ...

  2. jquery.validate校验文件使用说明

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一导入js库<script src="../js/ ...

  3. HTTP POST和GET的区别[转]

    http://www.cppblog.com/woaidongmao/archive/2008/05/29/51476.aspx 1.HTTP 只有POST和GET 两种命令模式: 2.POST是被设 ...

  4. Mac Please try running this command again as root/Administrator.

    mac 终端安装程序,需要权限,出现以下提示语句: Please try running this command again as root/Administrator. 需要执行以下命令即可: s ...

  5. php的模板引擎

    设计一个交互式的网站,我们需要关注两个主要的问题:分别是图形用户界面和业务逻辑.例如,一个标准的web开发小组由两三个美工和三个程序员组成,则设计流程是:美工设计者制作了项目的网站的界面模板,然后把它 ...

  6. head first 设计模式读书笔记 之 策略模式

    作为一个php开发者,深知曾经很多程序员都鄙视php,为什么呢?因为他们认为php的语法是dirty的,并且由于开发者水平参差不齐导致php的代码更加乱上加乱,维护起来简直一坨shit一样.随着php ...

  7. python Memo

    list&tuple 运算 乘以constant >>> x = ((1,2),) >>> x*2 ((1, 2), (1, 2)) >>> ...

  8. lua学习笔记1

    lua中调用c的函数 #include <stdio.h> #include <string.h> #ifdef __cplusplus extern "C" ...

  9. Oracle EBS-SQL (AR-1):检查应收收款发生额

    SELECT SUM(nvl(dist.amount_dr,0))-SUM(nvl(dist.amount_cr,0)) FROM ar_cash_receipt_history_all hist,  ...

  10. 深入理解Spring Redis的使用 (二)、RedisTemplate事务支持、序列化

    RedisTemplate api详解 1. RedisTemplate的事务 private boolean enableTransactionSupport = false; private bo ...