[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 non-space characters.
Please note that the string does not contain any non-printable characters.
Example:
Input: "Hello, my name is John"
Output: 5
思路:
从头到尾遍历,如果是空格,直接跳过。
非空格字符都遍历完后,segment++。
int countSegments(string s)
{
int seg = ;
int len = s.length();
for (int i = ; i < len;)
{
while (i < len && s[i] == ' ')i++;//去掉空格
if (i>=len) break;
while (i < len && s[i] != ' ')i++;
seg++;
} return seg;
}
[leetcode-434-Number of Segments in a String]的更多相关文章
- 434. Number of Segments in a String
原题: 434. Number of Segments in a String 解题: 刚看到题目时,觉得可以通过统计空格个数,但想想有可能会有多个空格的情况 思路: 一:遍历字符,if条件碰到非空格 ...
- 【LeetCode】434. Number of Segments in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 正则表达式 字符串分割 日期 题目地址:htt ...
- 【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 ...
- [LeetCode] 434. Number of Segments in a String_Easy
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 ...
- 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 ...
随机推荐
- 如何实现在Windows上运行Linux程序,附示例代码
微软在去年发布了Bash On Windows, 这项技术允许在Windows上运行Linux程序, 我相信已经有很多文章解释过Bash On Windows的原理, 而今天的这篇文章将会讲解如何自己 ...
- Centos7.0下将Python更新到Python2.7.13
在云服务器下默认安装的python版本过低,所有我们要手动进行更新(不建议卸载老的版本,然后安装新的,这样会导致大量的异常错误) 为了防止在安装编译python时出错,需先更新gcc :yum - ...
- Day5模块-time和datetime模块
模块是封装一段代码来实现某种功能. 分为三类: 1.自定义模块 2.标准库,内置模块 3.开源模块 -------------------------------------------------- ...
- 几张图带你轻轻松松了解小程序和APP的区别
微信"小程序"的公测一开放,立即在朋友圈刷屏无数,仿佛人人都在互联网圈.但是因为微信限制,程序还不能发布使用,所以也极少人看到真正的小程序是怎么样的. 小程序驿站专注微信小程序的开 ...
- 5.Java 加解密技术系列之 DES
Java 加解密技术系列之 DES 序 背景 概念 基本原理 主要流程 分组模式 代码实现 结束语 序 前 几篇文章讲的都是单向加密算法,其中涉及到了 BASE64.MD5.SHA.HMAC 等几个比 ...
- Hibernate SQLQuery 原生SQL 查询及返回结果集处理-2
1. 返回List, .setResultTransformer( Transformers.ALIAS_TO_ENTITY_MAP);将结果转为Map,存放到list中,即list中为若干 ...
- 016 多对多关联映射 单向(many-to-many)
一般的设计中,多对多关联映射,需要一个中间表 Hibernate会自动生成中间表 Hibernate使用many-to-many标签来表示多对多的关联 多对多的关联映射,在实体类中,跟一对多一样,也是 ...
- java虚拟机学习-JVM内存管理:深入垃圾收集器与内存分配策略(4)
Java与C++之间有一堵由内存动态分配和垃圾收集技术所围成的高墙,墙外面的人想进去,墙里面的人却想出来. 概述: 说起垃圾收集(Garbage Collection,下文简称GC),大部分人都把这项 ...
- Apache许可翻译
https://www.apache.org/licenses/LICENSE-2.0 Apache许可 2.0 2004.1 使用.复制和发行的术语和条件. 1 定义 "License&q ...
- js继承之借用构造函数继承
我的上一篇文章介绍了,原型链继承模式.但是单纯的原型链模式并不能很好地实现继承. 一.原型链的缺点 1.1 单纯的原型链继承最大的一个缺点,来自于原型中包含引用类型的值. 本来,我们没有通过原型链实现 ...