434. 求字符串有多少段 Number of Segments in a String

Posted Long Long Journey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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. }





以上是关于434. 求字符串有多少段 Number of Segments in a String的主要内容,如果未能解决你的问题,请参考以下文章

#Leetcode# 434. Number of Segments in a String

434. Number of Segments in a String

434. Number of Segments in a String

434. Number of Segments in a String

每天一道LeetCode--434. Number of Segments in a String

UVA - 434 Matty&#39;s Blocks