[LintCode] Count and Say 计数和读法
and count 计数 lintcode
2023-09-11 14:21:37 时间
The count-and-say sequence is the sequence of integers beginning as follows:
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 n
th sequence.
Notice
The sequence of integers will be represented as a string.
Example
Given n = 5
, return "111221"
.
LeetCode上的原题,请参见我之前的博客Count and Say。
class Solution { public: /** * @param n the nth * @return the nth sequence */ string countAndSay(int n) { if (n < 1) return ""; string res = "1"; while (--n) { res.push_back('#'); string t = ""; int cnt = 1; for (int i = 1; i < res.size(); ++i) { if (res[i] == res[i - 1]) ++cnt; else { t += to_string(cnt) + res[i - 1]; cnt = 1; } } res = t; } return res; } };
相关文章
- Leetcode: Water and Jug Problem && Summary: GCD求法(辗转相除法 or Euclidean algorithm)
- Leetcode: Count and Say
- What is the difference between localStorage, sessionStorage, session and cookies?
- What is the difference between Session.Abandon() and Session.Clear()
- What is the difference between visibility:hidden and display:none?
- 报错:HBase regionserver.HRegionServer: reportForDuty failed; sleeping and then retrying.
- 解决 If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvNamedWindow'
- 转 如何诊断和解决high version count 10.2.0.4 and 11.2.0.4
- Csharp: Listview convert Datatable and ListView.Group count
- [LeetCode] 913. Cat and Mouse 猫和老鼠
- [LeetCode] 38. Count and Say 计数和读法