码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-02-25 01:03:17      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:rgb   int   code   neu   note   style   har   pac   nbsp   

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

标签:rgb   int   code   neu   note   style   har   pac   nbsp   

原文地址:http://www.cnblogs.com/xiejunzhao/p/172cfd283d1b509ad446262e3d99130c.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!