1、题目

The count-and-say sequence is the sequence of integers with the first five terms as following:

1.     1
2.     11
3.     21
4.     1211
5.     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 term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

Example 1:

Input: 1
Output: "1"

Example 2:

Input: 4
Output: "1211"

Exapmle

       // 1 1// 2 11// 3 21// 4 1211// 5 111221// 6 312211// 7 13112221

2、代码实现

public class Solution {public String returnLast(String s) {if (s == null || s.length() == 0)return null;String result = "";int length = s.length();if (length == 1) {return "1" + s;}int count = 1;int init = s.charAt(0);for (int i = 1; i < s.length(); i++) {if (s.charAt(i) == s.charAt(i - 1)) {count++;if (i == length - 1) {char ss = s.charAt(i - 1);result = result + count + s.charAt(i - 1);}} else {char ss = s.charAt(i - 1);result = result + count + s.charAt(i - 1);count = 1;if (i == length - 1) {result += ("1" + s.charAt(i));}}}return result;}public String countAndSay(int n) {if (n <= 0)return null;if (n == 1) return "1";else {return returnLast(countAndSay(n - 1));}}
}

3、总结

1、用递归方法
2、我们递归的时候,先实现默认包含字符串,如何得到下一个字符串,这也是我们需要把每次得到的结果递归,所以,我们先写个函数简单实现从这个字符串如果得到下一个字符串
3、我们在写递归公共函数的实现时候,要注意,末尾和数字前一位是否相同和不同的情况。

LeetCode之Count and Say相关推荐

  1. LeetCode 38. Count and Say

    问题链接 LeetCode 38. Count and Say 题目解析 找规律,每一个数字串是上一个数字串的"读法".比如:n=1时为"1",读作" ...

  2. 【Hard 递归 动态规划 回文串15】LeetCode 730. Count Different Palindromic Subsequences

    LeetCode 730. Count Different Palindromic Subsequences 博客转载自:http://zxi.mytechroad.com/blog/dynamic- ...

  3. [勇者闯LeetCode] 38. Count and Say

    [勇者闯LeetCode] 38. Count and Say Description The count-and-say sequence is the sequence of integers b ...

  4. LeetCode 204. Count Primes--从一开始的质数个数--Python解法--面试算法题

    题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...

  5. LeetCode 250. Count Univalue Subtrees

    原题链接在这里:https://leetcode.com/problems/count-univalue-subtrees/ 题目: Given a binary tree, count the nu ...

  6. leetcode -- 357. Count Numbers with Unique Digits

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  7. LeetCode 1885. Count Pairs in Two Arrays(二分查找)

    文章目录 1. 题目 2. 解题 1. 题目 Given two integer arrays nums1 and nums2 of length n, count the pairs of indi ...

  8. leetcode 1925. Count Square Sum Triples(python)

    描述 A square triple (a,b,c) is a triple where a, b, and c are integers and a^2 + b^2 = c^2. Given an ...

  9. Leetcode 1905. Count Sub Islands

    文章作者:Tyan 博客:noahsnail.com  |  CSDN  |  简书 1. Description 2. Solution **解析:**Version 1,以第二个矩阵中碰到的1作为 ...

  10. leetcode 1534. Count Good Triplets(python)

    描述 Given an array of integers arr, and three integers a, b and c. You need to find the number of goo ...

最新文章

  1. UITableView 重用机制
  2. mysql 单列转换为行,mysql行转换为列
  3. [数字图像处理]图像去噪初步(2)--非线性滤波器
  4. python为啥这么受欢迎_Python为什么这么受欢迎?原因竟在于它的10个知识点
  5. 用弹性振子力学系统方法计算一组反对角矩阵的质量和频率n+m=8
  6. 关于Exception类
  7. hdu 2881(简单dp)
  8. 知识库的构建 6-1 语义网 Semantic Web
  9. httpclient+Jsoup总结
  10. matlab 从 excel读取 日期_MATLAB一键记账它不香吗?
  11. 计算机二级excel经典操作题,计算机二级office经典题库
  12. atmega128 单片机 密码锁 PROTEUS 仿真
  13. 基于阿里云的基础架构设施保障(一)IAAS云计算
  14. 列表推导式[x for x in range(n)]
  15. Java学习总结与反思
  16. 计算机二级页眉页脚的知识,轻松解决计算机二级之页眉页脚不再是问题-页眉页脚的设置...
  17. html5理财计算,理财收益怎么算(一般理财产品的收益计算方法)
  18. 线段树 +懒标记 + P3372 【模板】线段树 1
  19. CentOS 7账号密码忘了怎么办?
  20. 搜索引擎是如何抓取网站内容的

热门文章

  1. WTMPlus 1.1 发布
  2. .NET 6 预览版 7 Released
  3. 云原生 | 阿里巴巴的Dapr实践与探索
  4. Git 常用操作 | 重写 commit 历史
  5. FluentAspects -- 基于 Fluent API 的 Aop
  6. 《ASP.NET Core 微服务实战》-- 读书笔记(第6章)
  7. HttpClientFactory日志不好用,自己扩展一个?
  8. 深入Dapper.NET源码
  9. 用 C# 来守护 Python 进程
  10. EF Core 3 的 40 个中断性变更