【easy】Number of Segments in a String 字符串中的分段数量

以空格为分隔符,判断一个string可以被分成几部分。

注意几种情况:(1)全都是空格 (2)空字符串(3)结尾有空格

思路:

只要统计出单词的数量即可。那么我们的做法是遍历字符串,遇到空格直接跳过,如果不是空格,则计数器加1,然后用个while循环找到下一个空格的位置,这样就遍历完了一个单词,再重复上面的操作直至结束,就能得到正确结果:

class Solution {
public:
    int countSegments(string s) {
        int res = 0, n = s.size();
        for (int i = 0; i < n; ++i) {
            if (s[i] == ‘ ‘) continue;
            ++res;
            while (i < n && s[i] != ‘ ‘) ++i;
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/sherry-yang/p/8454620.html

时间: 2024-07-31 05:38:03

【easy】Number of Segments in a String 字符串中的分段数量的相关文章

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:

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

#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

每天一道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 (

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)

目录 题目描述: 示例: 解法: 题目描述: 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my name is John" 输出: 5 解法: class Solution { public: int countSegments(string s) { int res = 0; int sz = s.size(); int i = 0, j = 0; while(i < sz

三. Anagram detection problem for string(字符串中回文词汇检测问题)

anagram 相同字母异序词.heart vs earth 1.Our first solution to the anagram problem will check to see that each character in the first string actually occurs in the second. If it is possible to "checkoff" each character, then the two strings must be anag