标签: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
题意:求字符串有多少被空格分隔的段
public class Solution {
public int CountSegments(string s) {
int sum = 0;
int newIndex = 0;
for (int i = 0; i < s.Length; i++) {
if (s[i] != ‘ ‘) {
newIndex = i;
while (newIndex < s.Length && s[newIndex] != ‘ ‘) {
newIndex++;
}
sum++;
i = newIndex;
}
}
return sum;
}
}
434. 求字符串有多少段 Number of Segments in a String
标签:rgb int code neu note style har pac nbsp
原文地址:http://www.cnblogs.com/xiejunzhao/p/172cfd283d1b509ad446262e3d99130c.html