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. 剖析和解决Python中网络粘包的正确姿势

    目录 1.粘包及其成因 1.1.粘包产生 1.2.粘包产生的原因 2.尝试解决粘包 2.1.指定数据包的长度 2.2.固定数据包的长度 2.3.用函数实现多次调用发送数据 3.解决粘包问题的正确姿势 ...

  2. Eclipse安装JDK11方式

    安装JDK11JDK下载网址:https://www.oracle.com/technetwork/java/javase/downloads Java SE Development Kit 11ht ...

  3. Kotlin字节码生成机制详尽分析

    通过注解修改Kotlin的class文件名: 对于Kotlin文件在编译之后生成的class文件名默认是有一定规则的,比如: 而其实这个生成字节码的文件名称是可以被改的,之前https://www.c ...

  4. Python应用之-修改通讯录

    #-*- coding:utf-8 -*- import sqlite3 #打开本地数据库用于存储用户信息 conn = sqlite3.connect('mysql_person.db') #在该数 ...

  5. danci2

    composite 英 ['kɒmpəzɪt] 美 [kɑm'pɑzɪt] n. 复合材料:合成物:菊科 adj. 复合的:合成的:菊科的 vt. 使合成:使混合 a composite of 网络释 ...

  6. 与你一起学习MS Project——基础篇:Project基础应用

    为了更清晰容易地熟悉掌握Project的基础应用,我们在基础篇中一起来学习掌握在Project中如何做进度计划.资源计划.成本计划以及跟踪项目的执行情况并生成所需的项目报表. 一.进度计划 这里,首先 ...

  7. Windows环境下设置Tomcat8以服务的形式运行,不再打开Tomcat窗口

    内容简介 在Windows操作系统下,设置Tomcat8以服务的形式运行,按照以下3步来操作即可.前提条件:已安装好Java环境,并配置好java的环境变量:已下载好Tomcat8并解压到某目录. s ...

  8. make 命令出现:"make:*** No targets specified and no makefile found.Stop."

    我们在Linux 安装包的时候,使用make 命令出现:"make:*** No targets specified and no makefile found.Stop."这样的 ...

  9. KMP + BZOJ 4974 [Lydsy1708月赛]字符串大师

    KMP 重点:失配nxtnxtnxt数组 意义:nxt[i]nxt[i]nxt[i]表示在[0,i−1][0,i-1][0,i−1]内最长相同前后缀的长度 图示: 此时nxt[i]=jnxt[i]=j ...

  10. LeetCode 842. Split Array into Fibonacci Sequence

    原题链接在这里:https://leetcode.com/problems/split-array-into-fibonacci-sequence/ 题目: Given a string S of d ...