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

题意:求字符串有多少被空格分隔的段

  1. public class Solution {
  2. public int CountSegments(string s) {
  3. int sum = 0;
  4. int newIndex = 0;
  5. for (int i = 0; i < s.Length; i++) {
  6. if (s[i] != ‘ ‘) {
  7. newIndex = i;
  8. while (newIndex < s.Length && s[newIndex] != ‘ ‘) {
  9. newIndex++;
  10. }
  11. sum++;
  12. i = newIndex;
  13. }
  14. }
  15. return sum;
  16. }
  17. }

null

时间: 2024-11-10 07:39:48

434. 求字符串有多少段 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:

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

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

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

求字符串中最大的递增子序列

数据库环境:SQL SERVER 2005 如题,求字符串“abcbklmnodfghijkmer”中最大的递增子序列.这个字符串有点特别, 只由26个小写字母a-z组成. 大概思路如下: 1.将字符串转到一列存储,并生成行号 2.设置一个递增计数器列,默认为1,比较上下行的字符,如果在字典中的顺序是递增, 则计数器加1,否则,计数器置1 3.找出计数器最大的数及对应的行号,根据这2个数截取字符串 思路有了,下面直接贴代码 DECLARE @vtext VARCHAR(255) SET @vte