首页 > 其他 > 详细

LeetCode:434. Number of Segments in a String

时间:2017-02-03 18:27:13      阅读:363      评论:0      收藏:0      [点我收藏+]

package String;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

//Question 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
*/
public class countSegments434 {
/*一开始考虑到上面多个非字母符号的在一起的情况,先做了一个替换的过程,再分离,
* 后来发现自己把题目理解错了,其实很简单,就是以空格为分割,不用考虑引号逗号这些符号
* 但是得考虑空字符串,和只有空格的字符串,
* 后来参考别人的,可以先用trim去除首尾的空格,\\s空格
*/
public static int countSegments(String s) {
/*
if (s.length()==0)
return 0;
String regEx="[^a-zA-Z‘-]";
Pattern pat=Pattern.compile(regEx);
Matcher mat=pat.matcher(s);
String ss=mat.replaceAll("@");
System.out.println(ss);
String[] strs=ss.split("@+");
for(int i=0;i<strs.length;i++){
System.out.println(strs[i]);
}
return strs.length;
*/
String[] strs=s.split(" +");
if(strs.length!=0&&strs[0].length()==0)
return strs.length-1;
return strs.length;

}
//study1
public static int countSegments2(String s){
String ss=s.trim();
if(ss.length()==0)
return 0;
return ss.split("\\s+").length;
}
//study2:好机智啊
public static int countSegments3(String s){
return ("x "+s).split(" +").length-1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
System.out.println(countSegments("Hello, my name is John"));
System.out.println(countSegments("love live! mu‘sic forever"));
System.out.println(countSegments(""));
System.out.println(countSegments("The one-hour drama series Westworld is a dark odyssey about the dawn of artificial consciousness and the evolution of sin. Set at the intersection of the near future and the reimagined past, it explores a world in which every human appetite, no matter how noble or depraved, can be indulged."));
System.out.println(countSegments(", , , , a, eaefa"));
System.out.println(countSegments(" foo bar "));
System.out.println(countSegments(" "));

System.out.println(countSegments2("Hello, my name is John"));
System.out.println(countSegments2("love live! mu‘sic forever"));
System.out.println(countSegments2(""));
System.out.println(countSegments2("The one-hour drama series Westworld is a dark odyssey about the dawn of artificial consciousness and the evolution of sin. Set at the intersection of the near future and the reimagined past, it explores a world in which every human appetite, no matter how noble or depraved, can be indulged."));
System.out.println(countSegments2(", , , , a, eaefa"));
System.out.println(countSegments2(" foo bar "));
System.out.println(countSegments2(" "));
*/

System.out.println(countSegments3("Hello, my name is John"));
System.out.println(countSegments3("love live! mu‘sic forever"));
System.out.println(countSegments3(""));
System.out.println(countSegments3("The one-hour drama series Westworld is a dark odyssey about the dawn of artificial consciousness and the evolution of sin. Set at the intersection of the near future and the reimagined past, it explores a world in which every human appetite, no matter how noble or depraved, can be indulged."));
System.out.println(countSegments3(", , , , a, eaefa"));
System.out.println(countSegments3(" foo bar "));
System.out.println(countSegments3(" "));

}

}

LeetCode:434. Number of Segments in a String

原文:http://www.cnblogs.com/luluqiao/p/6363026.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!