[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 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的更多相关文章
- 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 ...
- 【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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 正则表达式 字符串分割 日期 题目地址:htt ...
- 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 ...
- 每天一道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 ...
随机推荐
- 动态加载JS文件方法总结
1.JQuery方法 $.getScript("./test.js"); //加载js文件 $.getScript("./test.js",function() ...
- Ajax请求文件下载操作失败的原因和解决办法
使用Poi做excel表格导出功能,第一个想到的就是用Ajax来发送请求,但是Ajax和后台代码都执行了,就是无法下载文件. 前台代码 function exportExl(){ var form = ...
- Thread--线程工作万花筒
线程工作内存图. 线程状态.
- GUI(Graphical User Interface)
译:用户和图形界面 GUI与程序交互的不同方式,包含3基本要素:输入,处理和输出. 常用GUI框架包括以下几种: wxPython Kivy Flexx PyQt Tkinter Pywin32 Py ...
- MySQL--事务,隔离性和隔离级别
事务 事务就是一组数据库操作,要么全部执行成功,要么全部执行失败,在MySQL中,事务是依靠存储引擎层实现的. ACID(Atomicity,Consistency,Isolation,Durabil ...
- PHP静态方法和普通方法的区别
<?php header('content-type:text/html;charset=utf-8'); /* 普通方法,存放类内,只有一份 静态方法,也是存放于类内,只有一份 区别在于:普通 ...
- 结点选择(树形DP)
Description 有一棵 n 个节点的树,树上每个节点都有一个正整数权值.如果一个点被选择了,那么在树上和它相邻的点都不能被选择.求选出的点的权值和最大是多少? Input 接下来的一行包含 n ...
- java线程——线程基础
一,线程之间的关系 线程之间存在两种关系: (1)间接相互制约:相互争夺线程资源: (2)直接相互制约:线程之间的相互合作: 间接相互制约也可以成为互斥,直接相互制约也可以称为同步:同步也包括互斥,互 ...
- PIL库参考文档之Image模块
原文: https://pillow-cn.readthedocs.io/zh_CN/latest/reference/Image.html 中文版参考文档不全,所以自己试着翻译了一下,以下~备注部分 ...
- SOA架构设计分析
SOA(Service-Oriented Architecture,面向服务的架构)是一个组件模型,它将应用程序的不同功能单元(称为服务)进行拆分,并通过这些服务之间定义良好的接口和契约联系起来. S ...