当前位置:网站首页>【Day_03 0420】字符串中找出连续最长的数字串
【Day_03 0420】字符串中找出连续最长的数字串
2022-07-26 06:08:00 【安河桥畔】
字符串中找出连续最长的数字串
题目来源
牛客网:字符串中找出连续最长的数字串
题目描述
读入一个字符串str,输出字符串str中的连续最长的数字串
输入描述
每个测试输入包含1个测试用例,一个字符串str,长度不超过255。
输出描述
在一行内输出str中里连续最长的数字串。
示例1
输入
abcd12345ed125ss123456789
输出
123456789
思路分析
- 遍历字符串,用一个cur保存当前连续的数字串,用ret保存当前最长的数字串
- 每次出现非数字字符时代表一个cur串结束,比较当前cur与ret的长度
- 每次比较完后,清空cur字符串内容
代码展示
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
string cur;
string ret;
getline(cin, str);
//外层循环的控制条件为<=,否则当数字串在原字符串末尾时
//如果最后一次找到的数字串是最长串,循环条件不满足会直接退出循环,不再进行赋值
for (int i = 0; i <= str.length(); i++)
{
//若当前字符为数字,则进行拼接
if (isdigit(str[i]))
{
cur += str[i];
}
//字符为非数字,则当前cur串结束
else
{
//string定义后初始lenth为0
if (cur.length() > ret.length())
{
//找出更长的字符串,更新字符串
ret = cur;
}
//比较完cur与ret后,对cur进行清零
cur.clear();
}
}
cout << ret << endl;
return 0;
}
总结
- isdigit()函数,判断字符是否为数字字符
- clear()函数,清空字符串内容
边栏推荐
- Acquisition of bidding information
- 【Day_06 0423】把字符串转换成整数
- em和rem
- 二叉树的性质 ~
- 1.12 basis of Web Development
- Day110. Shangyitong: gateway integration, hospital scheduling management: Department list, statistics based on date, scheduling details
- Ganglia installation and deployment process
- 平衡二叉树(AVL) ~
- Excitation method and excitation voltage of hand-held vibrating wire vh501tc acquisition instrument
- 金仓数据库 KingbaseES SQL 语言参考手册 (10. 查询和子查询)
猜你喜欢

Flex layout

Convolutional neural network (III) - target detection

Using dynamic libraries in VS

满二叉树 / 真二叉树 / 完全二叉树 ~

Traversal of the first, middle, and last order of a binary tree -- Essence (each node is a "root" node)

【Day_06 0423】不要二
![[MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM](/img/19/ca3a5710ead3c5b9a222a8ae4ecb37.png)
[MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM

Mysql45 talking about global lock, table lock and row lock

VRRP principle and basic commands

Embedded sharing collection 15
随机推荐
Introduction of four redis cluster schemes + comparison of advantages and disadvantages
Etcd database source code analysis - cluster membership changes log
Acquisition of bidding information
[Oracle SQL] calculate year-on-year and month on month (column to row offset)
时序动作定位 | 用于弱监督时态动作定位的细粒度时态对比学习(CVPR 2022)
Taobao pinduoduo Tiktok 1688 Suning taote jd.com and other keyword search commodity API interfaces (keyword search commodity API interface, keyword search commodity list interface, classification ID s
Properties of binary tree~
Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言
【BM2 链表内指定区间反转】
Xiao He shows his sharp corners and says hello to flutter app
How to divide the disks under the devices and drives in win10 new computer
Distributed | practice: smoothly migrate business from MYCAT to dble
满二叉树 / 真二叉树 / 完全二叉树 ~
A company installs monitoring for each station: write code only to see employees?
Flex layout
Talking about the practice of software defect management
Interview difficulties: difficulties in implementing distributed session, this is enough!
Leetcode:741. picking cherries
K. Link with Bracket Sequence I dp
Webapi collation