标签:print tput lease ace inpu 遍历 table class output
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
计算给定字符串中非空子串的个数
直接遍历处理即可。
class Solution {
public int countSegments(String s) {
int count = 0;
int len = 0;
for (char c : s.toCharArray()) {
if (c != ‘ ‘) {
len++;
} else {
count += len > 0 ? 1 : 0;
len = 0;
}
}
count += len > 0 ? 1 : 0;
return count;
}
}
0434. Number of Segments in a String (E)
标签:print tput lease ace inpu 遍历 table class output
原文地址:https://www.cnblogs.com/mapoos/p/13296896.html