LeetCode Count And Say
Problem 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. 1112...
Problem
The count-and-say sequence is the sequence of integers with the first five terms as following:
- 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 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"
题目的意思就是反复的计算字符中,从左向右,每种字符的数量和值。
1 -> 1个1,即11。
11 -> 2个1,即21。
21 -> 1个2,1个1,即1211。以此类推。
Java 实现
package com.coderli.leetcode.algorithms.easy;
/**
* The count-and-say sequence is the sequence of integers with the first five terms as following:
* <p>
* 1. 1 <br>
* 2. 11<br>
* 3. 21<br>
* 4. 1211<br>
* 5. 111221<br>
* 1 is read off as "one 1" or 11.<br>
* 11 is read off as "two 1s" or 21.<br>
* 21 is read off as "one 2, then one 1" or 1211.<br>
* Given an integer n, generate the nth term of the count-and-say sequence.
* <p>
* Note: Each term of the sequence of integers will be represented as a string.
* <p>
* Example 1:
* <p>
* Input: 1
* Output: "1"
* <p>
* Example 2:
* <p>
* Input: 4
* Output: "1211"
*
* @author li.hzh 2017-10-30 13:06
*/
public class CountAndSay {
public static void main(String[] args) {
System.out.println(new CountAndSay().countAndSay(1));
System.out.println(new CountAndSay().countAndSay(2));
System.out.println(new CountAndSay().countAndSay(3));
System.out.println(new CountAndSay().countAndSay(4));
System.out.println(new CountAndSay().countAndSay(5));
}
public String countAndSay(int n) {
String result = "1";
for (int i = 1; i < n; i++) {
StringBuilder builder = new StringBuilder();
char pre = result.charAt(0);
int count = 0;
for (int k = 0; k < result.length(); k++) {
if (result.charAt(k) == pre) {
count++;
} else {
builder.append(count);
builder.append(pre);
count = 1;
pre = result.charAt(k);
}
}
builder.append(count);
builder.append(pre);
result = builder.toString();
}
return result;
}
}
分析
似乎没什么好分析的,遍历字符串,统计每种字符的数量,然后把数量和值依次拼接成一个新的字符串。
看完题解,马上用 240 关实战检验掌握度
覆盖 GESP 1~8 级考纲心法、CSP-J 双栏阅读程序、错题动态变式推演与 90/120 分钟全真模考。
配套题解代码归档于 yummy-code 仓库,完整知识点索引见 GESP Wiki。
同专栏相关推荐
LeetCode Palindrome Number
Problem Determine whether an integer is a palindrome. Do this without extra space. 即返回一个数是否是回数。例如:1,1221,12321是回数,负数不是回数。题目特别提醒,不要使用额外的...
LeetCode Reverse Integer
Problem Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Note: The input is assumed to be a...
LeetCode Roman to Integer
Problem Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 即将罗马数字1-3999转换成对应的整...
OneCoder
始于 2012持续记录 C++ GESP/CSP 信奥算法、Java 服务端架构与 Python 工程实战。官方唯一域名:coderli.com