原题:

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. IFE2017笔记

    1. jQuery.trim()方法 jQuery 杂项方法 实例 删除字符串开始和末尾的空格 2.jQuery的map方法可以批量操作数组中元素,可以替代传统在循环中处理完数据挨个push进数据的代 ...

  2. tensorFlow 三种启动图的用法

    tf.Session(),tf.InteractivesSession(),tf.train.Supervisor().managed_session()  用法的区别: tf.Session() 构 ...

  3. [TFS]TFS2015禁止多人迁出设置

  4. 02-Sockent客户端

    package com.day1; import java.io.IOException; import java.io.OutputStream; import java.net.Inet4Addr ...

  5. spark streaming插入hbase

    import java.sql.{DriverManager, ResultSet} import org.apache.spark._ import org.apache.spark.streami ...

  6. MyBatis 工作原理

    参考链接: 深入理解Mybatis原理:http://blog.csdn.net/luanlouis/article/details/40422941 MyBatis原理:http://www.jia ...

  7. 用树莓派开Wifi热点

    安装软件 首先设置软件源: vim /etc/apt/sources.list 查看软件源后面的版本,如果是wheezy,需要换成jessie wheezy是基于deb 7的版本 而现在是基于jess ...

  8. boost 学习笔记 0: 安装环境

    boost 学习笔记 0: 安装环境 最完整的教程 http://einverne.github.io/post/2015/12/boost-learning-note-0.html Linux 自动 ...

  9. h5登录页面

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

  10. CF603EPastoral Oddities

    /* LCT管子题(说的就是你 水管局长) 首先能得到一个结论, 那就是当且仅当所有联通块都是偶数时存在构造方案 LCT动态加边, 维护最小生成联通块, 用set维护可以删除的边, 假如现在删除后不影 ...