leetcode_434. Number of Segments in a String 计算字符串中段的个数,只需遍历一遍字符串
2016-12-22 17:22
176 查看
题目:
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:
题意:
计算字符串中段的数量,其中段被定义为非空格字符的连续序列。
请注意,字符串不包含任何不可打印的字符。
代码:
public class Solution {
public int countSegments(String s) {
int n = s.length(); //计算字符串长度
int i = 0; //下标遍历字符串
int res = 0; //记录段的个数
int flag = 0; //标记正在遍历的是段,还是空格;初始化flag = 0,表示正在遍历空格;flag = 1,表示正在遍历段;
while( i < n ){
if( s.charAt(i) != ' ' && flag == 0 ){ //当前字符不是空格,且之前是在遍历空格状态,则开始进入段,段个数res加1
res++;
flag = 1; //已进入段,修改状态
i++;
}
else if( s.charAt(i) != ' ' && flag == 1 ){ //当前字符不是空格,但之前是在遍历段的状态,表示还在段中,直接i++
i++;
}
else if( s.charAt(i) == ' ' && flag == 0 ){ //当前字符是空格,且之前是在遍历空格的状态,表示还在遍历空格中,直接i++
i++;
}
else if( s.charAt(i) == ' ' && flag == 1 ){ //当前字符是空格,但之前是在遍历段的状态,表示之前的段遍历结束,开始进入空格状态,修改flag,i++
flag = 0; //前一个段遍历结束,进入遍历空格状态
i++;
}
}
return res;
}
}
笔记:
1、几种状态是互斥的,要用if--else 的逻辑
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 n = s.length(); //计算字符串长度
int i = 0; //下标遍历字符串
int res = 0; //记录段的个数
int flag = 0; //标记正在遍历的是段,还是空格;初始化flag = 0,表示正在遍历空格;flag = 1,表示正在遍历段;
while( i < n ){
if( s.charAt(i) != ' ' && flag == 0 ){ //当前字符不是空格,且之前是在遍历空格状态,则开始进入段,段个数res加1
res++;
flag = 1; //已进入段,修改状态
i++;
}
else if( s.charAt(i) != ' ' && flag == 1 ){ //当前字符不是空格,但之前是在遍历段的状态,表示还在段中,直接i++
i++;
}
else if( s.charAt(i) == ' ' && flag == 0 ){ //当前字符是空格,且之前是在遍历空格的状态,表示还在遍历空格中,直接i++
i++;
}
else if( s.charAt(i) == ' ' && flag == 1 ){ //当前字符是空格,但之前是在遍历段的状态,表示之前的段遍历结束,开始进入空格状态,修改flag,i++
flag = 0; //前一个段遍历结束,进入遍历空格状态
i++;
}
}
return res;
}
}
笔记:
1、几种状态是互斥的,要用if--else 的逻辑
相关文章推荐
- Leetcode 434. Number of Segments in a String 字符串的分段 解题报告
- leetcode -- 434. Number of Segments in a String 【字符串处理】
- leetcode 434. Number of Segments in a String 按照空格分割字符串 + C++的stringstream的一个很好应用示范
- 只允许遍历一遍字符串,找出字符串中第一个只出现一次的字符
- [LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法
- LeetCode 434. Number of Segments in a String
- [leetcode]434. Number of Segments in a String
- )RGB排序,一个字符串,里面只有三种字符R G B,所有的R都在G的前面,所有的G都在B的前面。将给定字符串按照此规律排序。要求不允许用辅助空间,复杂度控制在O(N)。遍历一遍就排好序。
- LeetCode - 434. Number of Segments in a String
- leetcode 434. Number of Segments in a String
- leetcode 434.Number of Segments in a String(easy)[分割字符串]
- Leetcode 434. Number of Segments in a String
- leetcode 逆转字符串 当年的第一题,今天再写一遍,物是人非
- LeetCode 434. Number of Segments in a String
- LeetCode之计算字符串相似度或编辑距离EditDistance
- LeetCode 434. Number of Segments in a String
- [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现
- [leetcode 241] Different Ways to Add Parentheses--------计算运算公式字符串中所有可能结果
- 只允许遍历一遍字符串,找出字符串中第一个只出现一次的字符
- 【LeetCode】 434. Number of Segments in a String