434. Number of Segments in a String

434. Number of Segments in a String_第1张图片

Java

public class Solution {
    public int countSegments(String s) {
        if(s.equals("")) return 0;
        int count=0;
        for(int i=0;i

Javascript,利用split

/**
 * @param {string} s
 * @return {number}
 */
var countSegments = function(s) {
    if(s==="") return 0;
    var count=0;
    var m=s.split(" ");
    for(var i=0;i

优解,Java,有想法

 public int countSegments(String s) {
    return ("x " + s).split(" +").length - 1;
}

你可能感兴趣的:(434. Number of Segments in a String)