LeetCode_434. Number of Segments in a String
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
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的更多相关文章
- [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 ...
- 每天一道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
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
- [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 ...
- 434. Number of Segments in a String
原题: 434. Number of Segments in a String 解题: 刚看到题目时,觉得可以通过统计空格个数,但想想有可能会有多个空格的情况 思路: 一:遍历字符,if条件碰到非空格 ...
- 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 ...
- C#LeetCode刷题之#434-字符串中的单词数(Number of Segments in a String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3941 访问. 统计字符串中的单词个数,这里的单词指的是连续的不是 ...
随机推荐
- Python入门篇-装饰器
Python入门篇-装饰器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.装饰器概述 装饰器(无参) 它是一个函数 函数作为它的形参 返回值也是一个函数 可以使用@functi ...
- Guava Cache 工具类
maven依赖 <dependency> <groupId>com.google.guava</groupId> <artifactId>guava&l ...
- 瑞克和莫蒂|S3E03 Rick为什么要把自己变成一根腌黄瓜?
给尚未入坑Rick and Morty的小伙伴们为一点背景知识 S3E1在说什么? rick从监狱里回来,顺手搞垮了统治地球的银河联邦政府.Jerry因此丢了工作,非常不高兴.于是向Beth质问 ...
- js实现文本框自动显示两位小数
转自https://blog.csdn.net/qiji2011/article/details/81270552 1.js: //保留2位小数,如:2,会在2后面补上00.即2.00 functio ...
- Dubbo源码分析:ChannelHandler
背景 一个请求经过序列化二进制数据转化成对象之后.请求进入netty框架,netty框架经过业务处理把主动权转交给NettyHandler对象.NettyHandler进入ChannelHandler ...
- MySQL入门篇之mysqldump备份和恢复
一.备份单个数据库 1.备份命令:mysqldump MySQL数据库自带的一个很好用的备份命令.是逻辑备份,导出 的是SQL语句.也就是把数据从MySQL库中以逻辑的SQL语句的形式直接输出或生成备 ...
- 简述Python的深浅拷贝以及应用场景?
浅拷贝:copy.copy 深拷贝:copy.deepcopy 浅拷贝指仅仅拷贝数据集合的第一层数据,深拷贝指拷贝数据集合的所有层 主要应用在字符串,数字的 ...
- Spark 缓存机制
Spark中的缓存机制:避免spark每次都重算RDD以及它的所有依赖,cache().persist(). checkpoint(). 1.cache():会被重复使用,但是不能太大的RDD,将其c ...
- Java字符串之间拼接时,如果有null值,则会直接拼接上null
package com.fgy.demo; public class demo06 { public static void main(String[] args) { String str1 = & ...
- 样式声明对象:document.styleSheets[0].rules[4].style;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...