当前位置:网站首页>434. 字符串中的单词数
434. 字符串中的单词数
2022-07-30 12:42:00 【拽拽就是我】
leetcode力扣刷题打卡
题目:434. 字符串中的单词数
描述:统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。
请注意,你可以假定字符串里不包括任何不可打印的字符。
解题思路
1、s[0]不是空格,++;
2、s[i-1]是空格,s[i]不是空格,++;
3、单词是按空格划分的。。
4、注意,测试用例有一个mu’sic也算一个单词。。。不是两个。。
原代码##
class Solution {
public:
int countSegments(string s) {
int ans= 0;
for (int i = 0; i < s.size(); ++i) {
if ((i == 0 || s[i - 1] == ' ') && s[i] != ' ') ans++;
}
return ans;
}
};
边栏推荐
- 外包干了七年,废了。。。
- int a=8,a=a++,a? int b=8,b=b+1,b?
- Decoding Redis' most overlooked high CPU and memory usage issues
- Using Baidu EasyDL to realize the recognition of the chef's hat of the bright kitchen
- 我又造了个轮子:GrpcGateway
- 物理服务器与虚拟机:主要区别和相似之处
- PanGu-Coder: 函数级的代码生成模型
- 基于反步积分滑模摩擦补偿的光电伺服转台控制
- 干货分享:小技巧大用处之Bean管理类工厂多种实现方式
- dolphinscheduler单机化改造
猜你喜欢

树形dp小总结(换根,基环树,杂七杂八的dp)

结合实战,浅析GB/T28181(三)——实况点播

Using Baidu EasyDL to realize the recognition of the chef's hat of the bright kitchen

缓存一致性

Raja Koduri澄清Arc GPU跳票传闻 AXG年底前新推四条产品线

北上广线下活动丨年底最不可错过的技术聚会都齐了

MySQL【多表查询】

OpenHarmony环境搭建报错: ImportError: cannot import name ‘VERSION‘ from ‘hb.__main__‘

元宇宙的六大支撑技术

Rust from entry to proficient 02-installation
随机推荐
智能指针实现猜想
Unity Beginner 6 - Simple UI production (blood bar production) and audio addition and NPC dialogue bubbles (2d)
[PostgreSQL] - 存储结构及缓存shared_buffers
如何将EasyCVR平台RTSP接入的设备数据迁移到EasyNVR中?
Based on MySQL database, Redis cache, MQ message middleware, ES high availability scheme of search engine parsing
Jackson 的JAR包冲突问题
什么是私有云?您应该知道的 6 个优势
云主机上的MongoDB被威胁,开启AUTH认证
Mysql batch insert transaction unique key repeated processing
监控界的最强王者,没有之一!
忆联:激活数据要素价值潜能,释放SAS SSD创新红利
Hu-cang integrated e-commerce project (1): project background and structure introduction
There is no one of the strongest kings in the surveillance world!
元宇宙的六大支撑技术
[BJDCTF2020]Cookie is so stable-1|SSTI injection
ModelCoder状态机:对柴油机工况判断策略进行建模
Homework 7.29 correlation function directory and file attributes related functions
基于空洞补全的动态SLAM方法
关于File文件的相关知识
Execution order of select, from, join, on where groupby, etc. in MySQL