当前位置:网站首页>2042. 检查句子中的数字是否递增-力扣双百代码-设置前置数据
2042. 检查句子中的数字是否递增-力扣双百代码-设置前置数据
2022-08-04 13:49:00 【Mr Gao】
2042. 检查句子中的数字是否递增
句子是由若干 token 组成的一个列表,token 间用 单个 空格分隔,句子没有前导或尾随空格。每个 token 要么是一个由数字 0-9 组成的不含前导零的 正整数 ,要么是一个由小写英文字母组成的 单词 。
示例,"a puppy has 2 eyes 4 legs" 是一个由 7 个 token 组成的句子:"2" 和 "4" 是数字,其他像 "puppy" 这样的 tokens 属于单词。
给你一个表示句子的字符串 s ,你需要检查 s 中的 全部 数字是否从左到右严格递增(即,除了最后一个数字,s 中的 每个 数字都严格小于它 右侧 的数字)。
如果满足题目要求,返回 true ,否则,返回 false 。
示例 1:
example-1
输入:s = “1 box has 3 blue 4 red 6 green and 12 yellow marbles”
输出:true
解释:句子中的数字是:1, 3, 4, 6, 12 。
这些数字是按从左到右严格递增的 1 < 3 < 4 < 6 < 12 。
示例 2:
输入:s = “hello world 5 x 5”
输出:false
解释:句子中的数字是:5, 5 。这些数字不是严格递增的。
示例 3:
example-3
输入:s = “sunset is at 7 51 pm overnight lows will be in the low 50 and 60 s”
输出:false
解释:s 中的数字是:7, 51, 50, 60 。这些数字不是严格递增的。
示例 4:
输入:s = “4 5 11 26”
输出:true
解释:s 中的数字是:4, 5, 11, 26 。
这些数字是按从左到右严格递增的:4 < 5 < 11 < 26 。
解题代码如下:
bool areNumbersAscending(char * s){
int pre=0;
for(int i=0;s[i]!='\0';i++){
if(s[i]>='0'&&s[i]<='9'){
int num=0;
while(s[i]>='0'&&s[i]<='9'){
num=num*10+s[i]-'0';
i++;
}
if(num<=pre){
return false;
}
else{
pre=num;
}
}
if(s[i]=='\0'){
break;
}
}
return true;
}
边栏推荐
- c#之winform(软件开发)
- 座舱人机交互「暗潮汹涌」,语音「下」,多模态「上」
- router---编程式导航
- router---dynamic route matching
- 信创是什么意思?涉及哪些行业?为什么要发展信创?
- State security organs conduct criminal arrest and summons review on Yang Zhiyuan, a suspect suspected of endangering national security
- php中的ceil和floo以及round函数「建议收藏」
- Why don't young people like to buy Mengniu and Yili?
- 【LeetCode】1403. 非递增顺序的最小子序列
- 【LeetCode】38、外观数列
猜你喜欢
随机推荐
How to find the location of a pdf file in endnote literature
SLAM 05.视觉里程计-2-特征法
sqlplus报错ORA-12547: TNS:lost contact解决
AlphaFold 如何实现 AI 在结构生物学中的全部潜力
MogDB/openGauss 3.0 扩容及缩容
zabbix自定义图形
电子行业MES管理系统有哪些特殊功能
七夕邂逅爱,那人一定在
Week 7 Latent Variable Models and Expectation Maximization
leetcode 48. Rotate Image 旋转图像(Medium)
"C pitfalls and pitfalls" reading summary
Utility function---string processing
oracle+RAC+linux5.1所需要安装的包
工具函数---字符串处理
内存定位利器-ASAN使用小结
代码越写越乱?那是因为你没用责任链!
router---动态路由匹配
关于redis的几件小事(五)redis保证高并发以及高可用
MySQL性能指标TPS\QPS\IOPS如何压测?
ACL 2022 | 社会科学理论驱动的言论建模









