(字符串)count and say
- https://www.nowcoder.com/practice/c5e8e84b62bb48398ec3c88153950fb5?tpId=46&tqId=29141&tPage=3&rp=3&ru=/ta/leetcode&qru=/ta/leetcode/question-ranking
- 题意:这个题目的意思是,输入一个整数n,输出第n-1个字符串怎么读的串。
输入n=1:“1”表示一个1
输入n=2:读出这个“1”,一个1用"11"表示。输入n=3:读出"11",两个1,用"21"表示。
依次类推,输入n,第n-1个字符串的读法。 - 思路:这个题目很显然要用到迭代法,从第一个开始进行迭代。n进行循环处理,传入一个要读的字符串,将这个字符串循环处理,找到相同的字符并且计数,然后将他们加入字符串,返回一个读过的字符串即可。
- 代码:
class Solution {
public:
string countAndSay(int n) {
if(n == )return string("");
string res("");
for(int i = ; i < n; i++) {
res = build(res);
}
return res;
}
string build(const string& res) {
string ret;
int len = res.size();
for(int i = ; i < len; i++) {
int count = ;
char x = res[i];
while(i < len && res[i+]==x) {
count++;
i++;
}
ret.push_back(count+'');
ret.push_back(x);
}
return ret;
}
};
(字符串)count and say的更多相关文章
- Python 字符串(count)
字符串 count:(python中的count()函数,从字面上可以知道,他具有统计功能) Python count() 方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位 ...
- [Swift]LeetCode730. 统计不同回文子字符串 | Count Different Palindromic Subsequences
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- select 数字/字符串/count(参数)/sum(数字) from table
第一种的写法是增加临时列,每行的列值是写在select后的数: --1select 1 from W_EC_PLACESTATION_COLLECT t--2select 2 from W_EC_PL ...
- python中常用的一些字符串
capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) 将字符串居中,并使用空格填充至长度 width 的新字符串 c ...
- 【Python扩展阅读【转】】字符串的方法及注释
capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) 将字符串居中,并使用空格填充至长度wi ...
- PHP基础之 string 字符串函数
/*=================常用字符串处理函数================== ltrim(); //去掉字符串左边的空格 rtrim(); //去掉字符串 ...
- 【LeetCode】38 - Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- [python]字符串方法
字符串的方法及注释 字符串的方法及注释 capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 cente ...
- Python字符串方法
capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) 将字符串居中,并使用空格填充至长度 width 的新字符串 c ...
- PHP 字符串替换 substr_replace 与 str_replace 函数
PHP 字符串替换 用于从字符串中替换指定字符串. 相关函数如下: substr_replace():把字符串的一部分替换为另一个字符串 str_replace():使用一个字符串替换字符串中的另一些 ...
随机推荐
- CodeForces - 1025F:Disjoint Triangles (几何)
A point belongs to a triangle if it lies inside the triangle or on one of its sides. Two triangles a ...
- java06-数组动手动脑
1.阅读QiPan.java示例程序了解如何利用二维数组和循环语句绘制五子棋盘. 定义了一个私有的二维数组作为棋盘.并定义了长度.之后打印符号使之连接起来作为棋盘在控制台显示.建立缓冲区用来读取输入的 ...
- loopqueue
import java.util.Arrays; public class loopQueue <E>{ public Object[] data=null; private int ma ...
- element table固定表头,表的高度自适应解决方法
主要是通过在mounted生命周期中,改变tableHeight的值,来让表格的高度自适应. 标签: <el-table ref="table" :data="ta ...
- ubuntu 挂载exfat
在ubuntu下,由于版权的原因,默认不支持exfat格式的u盘,不过可以很方便就能添加对exfat的支持: 1.对于ubuntu 14.04版本,直接运行下面的命令就可以了: sudo apt-ge ...
- bzoj 2242 [SDOI2011]计算器——BSGS模板
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2242 第一道BSGS! 咳咳,我到底改了些什么?…… 感觉和自己的第一版写的差不多……可能是 ...
- webrtc自带client的视频引擎创建代码走读
src\webrtc\examples\peerconnection\client\conductor.ccbool Conductor::InitializePeerConnection()1 we ...
- Spark Streaming之一:整体介绍
提到Spark Streaming,我们不得不说一下BDAS(Berkeley Data Analytics Stack),这个伯克利大学提出的关于数据分析的软件栈.从它的视角来看,目前的大数据处理可 ...
- 转:InnoDB Crash Recovery 流程源码实现分析
此文章转载给登博的文章,给大家分享 InnoDB Crash Recovery 流程源码实现分析 Crash Recovery问题 本文主要分析了InnoDB整个crash recovery的源码处理 ...
- Delphi BLE 控件
TBluetoothLEDevice LDevice.Address;//"00:11:22:DD:EE:FF". LDevice.DeviceName//Mi LDevice.I ...