434. Number of Segments in a String
来源:互联网 发布:mac安装flash插件 编辑:程序博客网 时间:2024/06/06 01:59
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
计算字符串中的段数,其中段被定义为非空格字符的连续序列。
请注意,字符串不包含任何不可打印的字符。
例:
输入: “Hello, my name is John” 输出: 5
思路:
用split方法分割字符串,接着循环判断字符串数组元素是否为空。注意判断时控制不能用 “” 或者null来判断,要用isEmpty()方法。
public int countSegments(String s) { String[] sa = s.split(" "); int result = 0; for(int i=0 ; i<sa.length ; i++){ if( !sa[i].isEmpty() ) { result++; } } return result; }
0 0
- 434. Number of Segments in a String
- 434. Number of Segments in a String
- 434. Number of Segments in a String*
- 434. Number of Segments in a String
- 434. Number of Segments in a String
- 434. Number of Segments in a String
- 434. Number of Segments in a String
- 434. Number of Segments in a String
- 434. Number of Segments in a String
- 434. Number of Segments in a String
- 434. Number of Segments in a String
- 434. Number of Segments in a String
- 434. Number of Segments in a String
- 434. Number of Segments in a String
- 434. Number of Segments in a String
- 434.Number of Segments in a String(String-Easy)
- String:434. Number of Segments in a String
- Leetcode434. Number of Segments in a String
- 非类型模板参数
- 进程间通信系列(7)匿名管道和命名管道异同
- virtualbox 网络连接 没有host only
- IMWeb训练营作业
- 斐波那契数列,计算n = 50时数列的值;输入n,返回数列结果
- 434. Number of Segments in a String
- UILabel描边效果
- x265文献阅读:CU模式判决
- 树状数组总结
- android:分享 一个很强大的LOG开关---Log.isLoggable
- Android SQLite数据库—SQLiteOpenHelper(一)
- 欢迎使用CSDN-markdown编辑器
- 把Excel中的数据导出成txt文件的注意问题
- 校验正则表达式 JavaScript test() 方法