LeetCode之Count and Say
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、总结
LeetCode之Count and Say相关推荐
- LeetCode 38. Count and Say
问题链接 LeetCode 38. Count and Say 题目解析 找规律,每一个数字串是上一个数字串的"读法".比如:n=1时为"1",读作" ...
- 【Hard 递归 动态规划 回文串15】LeetCode 730. Count Different Palindromic Subsequences
LeetCode 730. Count Different Palindromic Subsequences 博客转载自:http://zxi.mytechroad.com/blog/dynamic- ...
- [勇者闯LeetCode] 38. Count and Say
[勇者闯LeetCode] 38. Count and Say Description The count-and-say sequence is the sequence of integers b ...
- LeetCode 204. Count Primes--从一开始的质数个数--Python解法--面试算法题
题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...
- LeetCode 250. Count Univalue Subtrees
原题链接在这里:https://leetcode.com/problems/count-univalue-subtrees/ 题目: Given a binary tree, count the nu ...
- 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 ...
- 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 ...
- 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 ...
- Leetcode 1905. Count Sub Islands
文章作者:Tyan 博客:noahsnail.com | CSDN | 简书 1. Description 2. Solution **解析:**Version 1,以第二个矩阵中碰到的1作为 ...
- 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 ...
最新文章
- UITableView 重用机制
- mysql 单列转换为行,mysql行转换为列
- [数字图像处理]图像去噪初步(2)--非线性滤波器
- python为啥这么受欢迎_Python为什么这么受欢迎?原因竟在于它的10个知识点
- 用弹性振子力学系统方法计算一组反对角矩阵的质量和频率n+m=8
- 关于Exception类
- hdu 2881(简单dp)
- 知识库的构建 6-1 语义网 Semantic Web
- httpclient+Jsoup总结
- matlab 从 excel读取 日期_MATLAB一键记账它不香吗?
- 计算机二级excel经典操作题,计算机二级office经典题库
- atmega128 单片机 密码锁 PROTEUS 仿真
- 基于阿里云的基础架构设施保障(一)IAAS云计算
- 列表推导式[x for x in range(n)]
- Java学习总结与反思
- 计算机二级页眉页脚的知识,轻松解决计算机二级之页眉页脚不再是问题-页眉页脚的设置...
- html5理财计算,理财收益怎么算(一般理财产品的收益计算方法)
- 线段树 +懒标记 + P3372 【模板】线段树 1
- CentOS 7账号密码忘了怎么办?
- 搜索引擎是如何抓取网站内容的