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

Example:

Input: "Hello, my name is John"
Output: 5

class Solution {
public int countSegments(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int res = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != ' ' && (i == 0 || s.charAt(i - 1) == ' ')) {
res += 1;
}
}
return res;
}
}

[LC] 434. Number of Segments in a String的更多相关文章

  1. 434. Number of Segments in a String

    原题: 434. Number of Segments in a String 解题: 刚看到题目时,觉得可以通过统计空格个数,但想想有可能会有多个空格的情况 思路: 一:遍历字符,if条件碰到非空格 ...

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

  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. dxSkinController1 皮肤使用

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  2. blueimp,预览遮罩范围控制

    blueimg gallery github地址:https://github.com/blueimp/Gallery/blob/master/README.md 使用前提,引用css和js < ...

  3. VMware CentOS 设置IP地址

    VMware CentOS 设置IP地址 1. 设置虚拟机网络编辑器:参考:https://www.cnblogs.com/1285026182YUAN/p/10440443.html 2. Cent ...

  4. 吴裕雄--天生自然C++语言学习笔记:C++简介

    C++ 是一种中级语言,它是由 Bjarne Stroustrup 于 年在贝尔实验室开始设计开发的.C++ 进一步扩充和完善了 C 语言,是一种面向对象的程序设计语言.C++ 可运行于多种平台上,如 ...

  5. VMware DRS部分知识点

    主机添加到集群中,不需要维护模式(有虚拟机开机状态时也可以添加进去). 主机从集群中移除,需要主机进入维护模式. HA和DRS 全自动 当设置虚拟机必须在主机上时 DRS优先级大于HA 就算主机挂了H ...

  6. dp--最长上升子序列LIS

    1759:最长上升子序列 总时间限制:  2000ms 内存限制:  65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对 ...

  7. JNI的第1种写法 及 JNI通过形参修改Java数据

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 目的:通过形参在Java 和C之间传递数据,尤其是大块的媒体数据 优点:避免通过返回值返回一个巨大的数据块 ...

  8. LeetCode——48. 旋转图像

    给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...

  9. python语法基础-并发编程-进程-进程池以及回调函数

    ###############   进程池    ############## """ 进程池的概念 为什么会有进程池? 1,因为每次开启一个进程,都需要创建一个内存空间 ...

  10. MySql索引机制

    第一部分 MySQL数据库索引的数据结构及算法理论 第二部分 MySQL索引实现机制 第三部分 MySQL中高性能使用索引的策略 数据结构及算法 MySQL官方对索引的定义为:索引(Index)是帮助 ...