原题:

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. MongoDB集群搭建之主从模式

    单机搭建 #创建docker持久化数据目录 [root@docker ~]# mkdir -p /root/application/program/mongodb/data/master-slaveM ...

  2. Hadoop 2.x常用端口及查看方法

    Hadoop集群的各部分一般都会使用到多个端口,有些是daemon之间进行交互之用,有些是用于RPC访问以及HTTP访问.而随着Hadoop周边组件的增多,完全记不住哪个端口对应哪个应用,特收集记录如 ...

  3. C,C#,Java:枚举类型

    #include <stdio.h> main() { // 枚举默认从0开始,这里指定为1起头 , Tues, Wed, Thurs, Fri, Sat, Sun } day; scan ...

  4. Java 13 - Java 数组

    Java 数组 数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同. Java语言中提供的数组是用来存储固定大小的同类型元素. 你可以声明一个数组变量,如num ...

  5. jmeter插件如何协助进行内存监控 之 PerfMon Metrics Collector设置

    参考文章: http://www.cnblogs.com/zhaoxd07/p/5197669.html 当然最重要的是自己的实践,之前试的别人用的老的包 如XXstand.jar,结果并没有成功. ...

  6. MySQL C API(23)

    C API 提供了对 MySQL c/s 模型的底层访问.C API 代码在 mysqlclient 库中实现.可以从该库中引用到的变量及含义: 环境变量 含义 MYSQL_UNIX_PORT 本地连 ...

  7. android 开发 碎片Fragment布局例子(用按键切换碎片布局)

    实现思路: 1.写一个父类布局,里面写一个按键和一个帧布局(用于给Fragment布局后续替代) 2.写3个子布局,并且在写3个class继承Fragment布局 3.在MainActivity的cl ...

  8. 25.纯 CSS 创作一个慧星拖尾效果的 loader 动画

    原文地址:https://segmentfault.com/a/1190000014916281 简化地址:https://codepen.io/pen/?editors=1100 HTML代码: & ...

  9. checked选择器实现tab切换

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  10. 1. 报错:1130-host ... is not allowed to connect to this MySql server 开放mysql远程连接 不使用localhost

    在服务器上打开mysql命令行,依次执行下面这两句: GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRA ...