原题:

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的更多相关文章

  1. 【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 ...

  2. 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 ...

  3. [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 ...

  4. 【LeetCode】434. Number of Segments in a String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 正则表达式 字符串分割 日期 题目地址:htt ...

  5. 434 Number of Segments in a String 字符串中的单词数

    统计字符串中的单词个数,这里的单词指的是连续的非空字符.请注意,你可以假定字符串里不包括任何不可打印的字符.示例:输入: "Hello, my name is John"输出: 5 ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. 每天一道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 ...

随机推荐

  1. 使用docker搭建redis主从模式

    前期准备: 本地Linux版本:CentOS Linux release 7.5.1804 (Core)Docker版本:Docker version 1.13.1, build dded712/1. ...

  2. JVM性能、多线程排查常用命令

    最近遇到很一个很棘手的多线程问题,跟踪了几天终于解决了,在此记录跟踪过程的常用命令,后期有空再做具体的事件总结.软件的开发一定要有监控,一定要有监控,一定要有监控,重要的事情说三遍.没有监控的软件就是 ...

  3. 牛客网剑指Offer——正则表达式匹配

    1. 题目描述 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹配整 ...

  4. CS229 1 .线性回归与特征归一化(feature scaling)

    线性回归是一种回归分析技术,回归分析本质上就是一个函数估计的问题(函数估计包括参数估计和非参数估计),就是找出因变量和自变量之间的因果关系.回归分析的因变量是应该是连续变量,若因变量为离散变量,则问题 ...

  5. 503 Error: need EHLO and AUTH first

    设置OUTLOOK2013使用QQ邮箱,按照QQ邮箱的配置介绍设置好后,收邮件的服务可以了,但是发送邮件的服务失败,报错:503 Error: need EHLO and AUTH first,经查, ...

  6. WPF 获取文件夹路径,目录路径,复制文件,选择下载文件夹/目录

    private void Border_MouseLeftButtonUp_4(object sender, MouseButtonEventArgs e) { //获取项目中文件 , System. ...

  7. js数据类型 --运算符

    基本数据类型: number: var a=1; string: var str='123'; boolean: var b1=false; null:var c1=null; //打印结果为 obj ...

  8. java 日期排序。。。。

    Collections.sort(list, new Comparator<Map<Object, Object>>() { public int compare(Map< ...

  9. spring的IOC 的底层实现原理

    IOC:Inversion of Control  控制反转. 指的是 对象的创建权反转(交给)给 Spring. 作用是实现了程序的解耦合.

  10. py库: jieba (中文词频统计) 、collections (字频统计)、WordCloud (词云)

    先来个最简单的: # 查找列表中出现次数最多的值 ls = [1, 2, 3, 4, 5, 6, 1, 2, 1, 2, 1, 1] ls = ["呵呵", "呵呵&qu ...