【Leetcode 434. Number of Segments in a String】
2016-12-16 10:41
471 查看
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
java解决方法:
Total Accepted: 3749 Total Submissions: 9646 Difficulty: Easy Contributors: love_FDU_llp
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
java解决方法:
package Test; public class NumberSegments { public static void main(String[] args) { // TODO Auto-generated method stub NumberSegments test=new NumberSegments(); test.countSegments(", , , , a, eaefa"); } public int countSegments(String s) { if("".equals(s)){ return 0; } int count=0; String [] aStrings=s.split(" "); for(int i=0;i<aStrings.length;i++){ if(!"".equals(aStrings[i])){ count++; } } System.out.println(count); return count; } }
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- android上改变listView的选中颜色
- String.intern
- Prototype源码浅析 String部分(二)
- Ruby中的String对象学习笔记
- Redis02 使用Redis数据库(String类型)全面解析
- PostgreSQL ERROR: invalid escape string 解决办法
- C#中Byte[]和String之间转换的方法
- C++中sting类的简单实现方法
- 浅谈C++中的string 类型占几个字节
- 标准C++类string的Copy-On-Write技术
- C++实现string存取二进制数据的方法
- 关于C++ string和c类型字符数组的对比
- C#中string和StingBuilder内存中的区别实例分析
- 详解C++中实现继承string类的MyString类的步骤
- PHP STRING 陷阱原理说明
- c#中 String和string的区别介绍
- C#实现的图片、string相互转换类分享