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 38_字串_算術運算】Count and SayLeetCode字串
- Leetcode Count and SayLeetCode
- LeetCode:Count and SayLeetCode
- Leetcode-Count and SayLeetCode
- Count and Say leetcode javaLeetCodeJava
- leetcode刷題--Count and SayLeetCode
- [LeetCode] Count and Say 計數和讀法LeetCode
- LeetCode-Count PrimesLeetCode
- LeetCode-Count BitsLeetCode
- LeetCode-Count Univalue SubtreesLeetCode
- LeetCode-Count of Range SumLeetCode
- leetcode刷題--Count PrimesLeetCode
- 【Leetcode】1395. Count Number of TeamsLeetCode
- LeetCode-Count Complete Tree NodesLeetCode
- LeetCode- Count Numbers with Unique DigitsLeetCodeGit
- 【LeetCode從零單排】No38.CountAndSayLeetCode
- [LeetCode] 811. Subdomain Visit CountLeetCodeAI
- LeetCode筆記:204. Count PrimesLeetCode筆記
- [LeetCode] 2960. Count Tested Devices After Test OperationsLeetCodedev
- [LeetCode] 2257. Count Unguarded Cells in the GridLeetCode
- MySQL的COUNT語句--count(*)、 count(常量)、 count(列名)MySql
- count(0),count(1),count(*)總結與count(column)
- 【優化】COUNT(1)、COUNT(*)、COUNT(常量)、COUNT(主鍵)、COUNT(ROWID)等優化
- count(1),count(*),count(列)的區別
- Say "Hello tomorrow" with GoGo
- count(*)、count(1)和count(列名)的區別
- count (*) 和 count (1) 和 count (列名) 區別
- count(*) 和 count(1)和count(列名)區別
- [LeetCode] 3184. Count Pairs That Form a Complete Day ILeetCodeAIORM
- 圖解MySQL:count(*) 、count(1) 、count(主鍵欄位)、count(欄位)哪個效能最好?圖解MySql
- 【Leetcode】1180. Count Substrings with Only One Distinct LetterLeetCode
- LeetCode C++ 204. Count Primes【Math/Hash Table】簡單LeetCodeC++
- count(*) 和count(column)之區別
- 初入 Vue 的世界 Say HelloVue
- Say goodbye to my photos&videosGoIDE
- mysql中count(1)與count(*)比較MySql
- [LeetCode] 2962. Count Subarrays Where Max Element Appears at Least K TimesLeetCodeAPPAST
- 【MySQL】效能優化之 count(*) VS count(col)MySql優化