public class Solution {
    public int CountSegments(string s) {
        s = s.Trim();
            if (s.Length == 0)
            {
                return 0;
            }
            else
            {
                var whitespacecount = 0;
                var prewhiteindex = -1;
                for (int i = 0; i < s.Length; i++)
                {
                    var c = s[i];
                    if (char.IsWhiteSpace(c))
                    {
                        if (prewhiteindex + 1 != i)
                        {
                            whitespacecount++;
                        }
                        prewhiteindex = i;
                    }
                }
                return whitespacecount + 1;
            }
    }
}

https://leetcode.com/problems/number-of-segments-in-a-string/#/description

posted on 2017-04-21 17:29  Sempron2800+  阅读(95)  评论(0编辑  收藏  举报