leetcode_383 Number of Segments in a String(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 Solution {
    public int countSegments(String s) {
        int ans=0;
        for(int i=0;i<s.length();i++){
            if (s.charAt(i)!=‘ ‘&&(i==0||s.charAt(i-1)==‘ ‘)){
                ans++;
            }
        }
        return ans;
    }
}

python:

class Solution(object):
    def countSegments(self, s):
        """
        :type s: str
        :rtype: int
        """
        return len(s.split())
时间: 2024-11-05 16:22:52

leetcode_383 Number of Segments in a String(String)的相关文章

每天一道LeetCode--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. For example, Input: "Hello, my name is John" Output: 5 public int countSegments(String s) { String trimmed = s.trim(); if (

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:

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:

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:

Leetcode: 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:

#Leetcode# 434. Number of Segments in a String

https://leetcode.com/problems/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 cha

(后缀数组)HDU 6194-string string string

string string string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 774    Accepted Submission(s): 225 Problem Description Uncle Mao is a wonderful ACMER. One day he met an easy problem, but Un

【后缀数组】【RMQ】HDU 6194 - string string string (2017ICPC沈阳网络赛)

string string string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Uncle Mao is a wonderful ACMER. One day he met an easy problem, but Uncle Mao was so lazy that he left the problem to you. I

从为什么String=String谈到StringBuilder和StringBuffer

前言 有这么一段代码 1 public class TestMain 2 { 3 public static void main(String[] args) 4 { 5 String str0 = "123"; 6 String str1 = "123"; 7 System.out.println(str0 == str1); 8 } 9 } 运行结果是什么?答案当然是true.对,答案的确是true,但是这是为什么呢?很多人第一反应肯定是两个“123”的Stri