统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。

请注意,你可以假定字符串里不包括任何不可打印的字符。

示例:

输入: "Hello, my name is John" 输出: 5

class Solution {
public:
int countSegments(string s) {
int len = s.size();
string temp = "";
int res = 0;
for(int i = 0; i < len; i++)
{
if(s[i] == ' ')
{
if(temp == "")
continue;
else
{
res++;
temp = "";
}
}
else
{
temp += s[i];
} if(i == len - 1 && temp != "")
res++;
}
return res;
}
};

Leetcode434.Number of Segments in a String字符串中的单词数的更多相关文章

  1. 434 Number of Segments in a String 字符串中的单词数

    统计字符串中的单词个数,这里的单词指的是连续的非空字符.请注意,你可以假定字符串里不包括任何不可打印的字符.示例:输入: "Hello, my name is John"输出: 5 ...

  2. [LeetCode] Number of Segments in a String 字符串中的分段数量

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

  3. 434. Number of Segments in a String 字符串中的单词个数

    [抄题]: Count the number of segments in a string, where a segment is defined to be a contiguous sequen ...

  4. 【easy】Number of Segments in a String 字符串中的分段数量

    以空格为分隔符,判断一个string可以被分成几部分. 注意几种情况:(1)全都是空格 (2)空字符串(3)结尾有空格 思路: 只要统计出单词的数量即可.那么我们的做法是遍历字符串,遇到空格直接跳过, ...

  5. 每天一道LeetCode--434. Number of Segments in a String

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

  6. Java实现 LeetCode 434 字符串中的单词数

    434. 字符串中的单词数 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my nam ...

  7. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  8. [Swift]LeetCode434. 字符串中的单词数 | Number of Segments in a String

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

  9. 【LeetCode】434. Number of Segments in a String

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

随机推荐

  1. Bubble Cup 12 - Finals Online Mirror, unrated, Div. 1

    Bubble Cup 12 - Finals Online Mirror, unrated, Div. 1 C. Jumping Transformers 我会状压 DP! 用 \(dp[x][y][ ...

  2. 获取调用U9接口时报错的方法

  3. wpf datepicker 样式

    在项目中用到的 <Style TargetType="{x:Type DatePicker}"> <Setter Property="Foregroun ...

  4. 自定义Jquery:ajax,get,post方法

    var myAjax = { request: function(url, type, data, callback) { $.ajax(url, { type: type, data: data, ...

  5. No converter found for return value of type: class com.alibaba.fastjson.JSON解决办法

    默认情况下,springMVC的@ResponseBody返回的是String类型,如果返回其他类型则会报错.使用fastjson的情况下,在springmvc.xml配置里加入: <mvc:a ...

  6. 2-sat——hdu3062

    对于怎么建边还是不太清楚 选了a,那么b c不选,所以连边 选了b或c,那么a必定不选 /* 每个点拆成i*2,i*2+1 队长选,那么队友不选 队长不选,那么队友必定要选 */ #include&l ...

  7. 使用 data-* 属性来嵌入自定义数据:

    <!DOCTYPE html> <html> <head> <script> function showDetails(animal) { var an ...

  8. 移动端开发框架Zepto.js

    一.概述 Zepto.js是一个轻量的js库,它与jQuery有类似的API. zepto的设计目的是不到10K的通用库,快速下载,有一个熟悉的api-->精力专注在开发上. 流行起来的原因:轻 ...

  9. 使用Java代码获取Java进程ID的方法

    需要jre/lib下的tools.jar包 public class Test { public static void main(String[] args) throws Exception {  ...

  10. Lint found fatal errors while assembling a release target问题的解决方案

    此问题发生在编译为 release 版本时,出现错误提示如下: Lint found fatal errors while assembling a release target. To procee ...