434. Number of Segments in a String
原题:
434. Number of Segments in a String
解题:
刚看到题目时,觉得可以通过统计空格个数,但想想有可能会有多个空格的情况
思路:
一:遍历字符,if条件碰到非空格时,计数加1,然后while循环跳过非空格字符,一直到最后
二:设置flag初始为0,当碰到非空格时,计数加1,且flag置1,当flag为1且碰到空格时,flag再重置为0
思路一是自己想的,思路二更巧妙,是论坛里摘抄的
AC代码:
思路一:
class Solution {
public:
int countSegments(string s)
{
int len = s.length();
int i = 0;
int count = 0;
for(; i < len; i++)
{
if(s[i]!=' ')
{
count++;
while(s[i]!=' '&&i<len)
{
i++;
}
i -= 1; //回退一个位置,因为for循环会累加
}
}
return count;
}
};
思路二:
class Solution {
public:
int countSegments(string s) {
int count=0;
int flag = 0;
for (int i=0;i<s.size();i++) {
if (flag ==0 && s[i]!=' ') {
count++;
flag = 1;
}
if (flag ==1 && s[i]==' ') {
flag = 0;
}
}
return count;
}
};
434. Number of Segments in a String的更多相关文章
- 【LeetCode】434. Number of Segments in a String
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
- 434. Number of Segments in a String 字符串中的单词个数
[抄题]: Count the number of segments in a string, where a segment is defined to be a contiguous sequen ...
- [LC] 434. Number of Segments in a String
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
- 【LeetCode】434. Number of Segments in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 正则表达式 字符串分割 日期 题目地址:htt ...
- 434 Number of Segments in a String 字符串中的单词数
统计字符串中的单词个数,这里的单词指的是连续的非空字符.请注意,你可以假定字符串里不包括任何不可打印的字符.示例:输入: "Hello, my name is John"输出: 5 ...
- LeetCode_434. Number of Segments in a String
434. Number of Segments in a String Easy Count the number of segments in a string, where a segment i ...
- [LeetCode] Number of Segments in a String 字符串中的分段数量
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
- Leetcode: Number of Segments in a String
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
- 每天一道LeetCode--434. Number of Segments in a String
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
随机推荐
- 第10章 网络安全(4)_网络层安全IPSec
5. 网络层安全IPSec 5.1 IPSec协议 (1)前面使用Outlook进行数字签名和数字加密是应用层实现的安全.安全套接字实现的安全是在应用层和传输层之间插入了一层来实现数据通信安全.而IP ...
- 关于javascript三目
三目运算符能使我们的代码更为简洁,因而包括小编的我也很是青睐它,不过有时候我们给予它更多的希望,小编处于学习阶段,先从笔记开始: (3>1)?console.log(1):console.log ...
- Linux使用NFS服务实现远程共享
首先安装 apt install -y nfs-kernel-server nfs-common 编辑配置文件 vim /etc/exports 添加内容: /root/test *(rw,sync, ...
- courator - maven
ZK3.4.x: <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes --> <de ...
- Java中的Html解析:使用jsoup
包:jsoup-1.10.2.jar import java.io.File; import java.io.IOException; import org.jsoup.Jsoup; import o ...
- 新型DenseBody框架:一张照片获得3D人体信息
来自云从科技和上海交通大学的研究者近期提出一种新型框架 DenseBody,可直接从一张彩色照片中获取 3D 人体姿势和形状.该研究设计了一种高效的 3D 人体姿势和形状表示,无需中间表示和任务,端到 ...
- 如何通过权限控制EXP导出指定的表
今天一客户朋友咨询一个Oracle数据库用户EXP权限控制的问题,问我有没有办法可以解决.问题是这样的: 目前他们那边有外面的开发公司人员在核心系统做开发,考虑到系统数据的敏感性,给他们建了一个数据库 ...
- 字符串截取函数slice, substring, substr
在日常项目需求中,常常会遇到需要截取字符串操作的工作,而ECMAScript为我们提供了原生的截取字符串的函数,而且提供了三个:slice, substring, substr.我们怎么判断在什么时候 ...
- 微信小程序笔记<四>page.js —— 页面注册
小程序的每个页面都是独立的,每个页面都必须有 page.js 和 page.wxhl 文件,page.json 和 page.wxss 文件非必要文件,注意如果创建 page.json 则至少保证有一 ...
- 0. 资料官网【从零开始学Spring Boot】
[视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...