LeetCode 434. 字符串中的单词数
原题链接
简单
作者:
bruce
,
2021-01-05 20:58:50
,
所有人可见
,
阅读 238
#include <iostream>
#include <vector>
#include <string>
using namespace std;
/**
* 434 求解字符串中单词的个数
* 输入 s = "hello, this is the world"
* 输出 5个单词
*/
/**
* 方法 1,使用双指针算法来做
*/
int countSegments(string s)
{
int n = s.size();
int cnt = 0;
int i = 0;
while (i < n)
{
int j = i;
while (j < n && s[j] != ' ')
j++;
if (j != i)
cnt++;
i = j + 1;
}
return cnt;
}
/**
* 方法 2,使用stringstream字符流来处理
*/
int countSegments2(string s)
{
int cnt = 0;
stringstream iss(s);
while (iss >> s)
{
cnt++;
}
return cnt;
}