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 non-space characters.

Please note that the string does not contain any non-printable characters.

Example:

Input: "Hello, my name is John"
Output: 5
package leetcode.easy;

public class NumberOfSegmentsInAString {
public int countSegments1(String s) {
String trimmed = s.trim();
if (trimmed.equals("")) {
return 0;
}
return trimmed.split("\\s+").length;
} public int countSegments2(String s) {
int segmentCount = 0; for (int i = 0; i < s.length(); i++) {
if ((i == 0 || s.charAt(i - 1) == ' ') && s.charAt(i) != ' ') {
segmentCount++;
}
} return segmentCount;
} @org.junit.Test
public void test() {
System.out.println(countSegments1("Hello, my name is John"));
System.out.println(countSegments2("Hello, my name is John"));
}
}

LeetCode_434. Number of Segments in a String的更多相关文章

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

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

  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

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

  5. [Swift]LeetCode434. 字符串中的单词数 | 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 ...

  6. 434. Number of Segments in a String

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

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

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

  9. C#LeetCode刷题之#434-字符串中的单词数​​​​​​​(Number of Segments in a String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3941 访问. 统计字符串中的单词个数,这里的单词指的是连续的不是 ...

随机推荐

  1. Relief 过滤式特征选择

    给定训练集{(x1,y1),(x2,y2).....(xm,ym)} ,对每个示例xi,Relief在xi的同类样本中寻找其最近邻xi,nh(猜中近邻),再从xi的异类样本中寻找其最近邻xi,nm(猜 ...

  2. Linux TTY介绍

    1. TTY介绍 TTY(TeleType)指Linux中的一类终端(Terminal)设备, 是一种字符设备 在Linux中, tty可分为如下几类- 串行端口终端(serial port term ...

  3. Visual Studio Code 写Python代码

    之前用nodepad++,sublime text3,ultraedit,最近上手微软的vsc感觉上手还行,如果没有pycharm照样可以使用它 https://code.visualstudio.c ...

  4. axio 请求中参数是数组

    前言 最近在做 Vue 项目中,Get 请求中有的参数是数组,传 JSON 字符串是没有问题的,但是直接传数组就一直报错,有问题. 参数后面无故加了 [],例如:UserIds 变成 UserIds[ ...

  5. c#3.0 Lambda 表达式

    使用c# 2.0 中的匿名方法查找“内部包含abc子串的所有字符串”: list.FindAll( delegate(string s) { renturn s.indexof("abc&q ...

  6. web api 2.0 上传文件超过4M时,出现404错误

    客户端代码 string path = "C:\\text.txt"; WebClient client = new WebClient(); Uri _address = new ...

  7. 经肝药酶CYP3A4代谢的药物对比记录

    罗非昔布 罗非昔布,解热镇痛抗炎药,选择性环氧化酶-2(COX-2)抑制药,有研究表明,该类药可增加心脏病发作.卒中或其他严重后果概率,不良反应为,增加心肌梗死和心脏猝死的风险,现已撤市.经肝和肠壁细 ...

  8. php web开发——文件夹的上传和下载

    核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...

  9. java 数组遍历(方法内部的代码)

    //数组遍历(依次输出数组中的每一个元素)二维数组: int[][] arr={{1,2},{3,4,5},{6,7}}; for(int i=0;i<arr.length;i++){ for( ...

  10. Nodejs中的JavaScript

    一.Ecmascript ①基本语法:if var function Object Array等 ②特别注意:Node.js中没有DOM和BOM 二.核心模块: 1.Node.js为JavaScrip ...