您的位置:首页 > 其它

【Leetcode 434. Number of Segments in a String】

2016-12-16 10:41 471 查看
434. Number of Segments in a String

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode string