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

用split()

 1 public class Solution {
 2     public int countSegments(String s) {
 3         if (s==null || s.length()==0) return 0;
 4         String[] strs = s.split(" ");
 5         int count = 0;
 6         for (String str : strs) {
 7             if (str.length() != 0) count++;
 8         }
 9         return count;
10     }
11 }

不用API, better solution, O(N) time O(1) space

1 public int countSegments(String s) {
2     int res=0;
3     for(int i=0; i<s.length(); i++)
4         if(s.charAt(i)!=‘ ‘ && (i==0 || s.charAt(i-1)==‘ ‘))
5             res++;
6     return res;
7 }
时间: 2024-10-12 13:13:14

Leetcode: Number of Segments in a String的相关文章

#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 (

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

目录 题目描述: 示例: 解法: 题目描述: 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "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

【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(

[LeetCode] Number of Lines To Write String

We are to write the letters of a given string S, from left to right into lines. Each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, it is written on the next line. We are given an arra