Leetcode 38 Count and Say
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 where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence.
Note: Each term of the sequence of integers will be represented as a string.
這個題目的意思就是此時的字串由上一個字串來定義,也就是count+say,程式碼如下:
class Solution {
public String countAndSay(int n) {
StringBuffer cur = new StringBuffer("1");
StringBuffer prev;
int count = 0;
char say;
for(int i = 1 ; i < n ; i++){
prev = cur;
cur = new StringBuffer();
count = 1;
say = prev.charAt(0);
for(int j = 1,len = prev.length() ; j < len;j++){
if(prev.charAt(j) != say){
cur.append(count).append(say);
count = 1;
say = prev.charAt(j);
}else{
count++;
}
}
cur.append(count).append(say);
}
return cur.toString();
}
}
相關文章
- [LeetCode] 811. Subdomain Visit CountLeetCodeAI
- 【Leetcode】1395. Count Number of TeamsLeetCode
- [LeetCode] 2257. Count Unguarded Cells in the GridLeetCode
- Say and act
- [LeetCode] 2960. Count Tested Devices After Test OperationsLeetCodedev
- [LeetCode] 3184. Count Pairs That Form a Complete Day ILeetCodeAIORM
- 【Leetcode】1180. Count Substrings with Only One Distinct LetterLeetCode
- 222. Count Complete Tree Nodes(Leetcode每日一題-2020.11.24)LeetCode每日一題
- Say "Hello tomorrow" with GoGo
- MySQL的COUNT語句--count(*)、 count(常量)、 count(列名)MySql
- [LeetCode] 2962. Count Subarrays Where Max Element Appears at Least K TimesLeetCodeAPPAST
- LeetCode C++ 204. Count Primes【Math/Hash Table】簡單LeetCodeC++
- LeetCode1002. 查詢常用字元(雜湊表、count)LeetCode字元
- count(*)、count(1)和count(列名)的區別
- count (*) 和 count (1) 和 count (列名) 區別
- count(*) 和 count(1)和count(列名)區別
- 圖解MySQL:count(*) 、count(1) 、count(主鍵欄位)、count(欄位)哪個效能最好?圖解MySql
- 初入 Vue 的世界 Say HelloVue
- Java輕鬆搞定leetcode前100系列之38. 外觀數列JavaLeetCode
- [20180727]再論count(*)和count(1).txt
- 7.65 COUNT
- MySQL:count(*) count(欄位) 實現上區別MySql
- SQL Server中count(*)和Count(1)的區別SQLServer
- 日常 38
- Count BFS Graph
- count(*) 優化優化
- suffice it to say 一言以蔽之
- 204. Count Primes
- std::count 函式函式
- 7.36 BITMAP_COUNT
- 7.13 APPROX_COUNTAPP
- 解析Count函式函式
- vue38Vue
- C# 中List中的Count和Count(),有什麼區別C#
- mysql count()的使用解析MySql
- MySQL:SELECT COUNT 小結MySql
- 7.14 APPROX_COUNT_DISTINCTAPP
- 《HelloGitHub》第 38 期Github