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

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

示例:

输入: "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. <随便写> 多线程的例子

    ''' 一个线程在使用这个共享的时候,其他线程必须等待他结束 通过"锁"实现,作用就是防止多个线程使用这片内存空间 进程:程序的一次执行 线程:cpu运算的基本调度单位 多线程:大 ...

  2. P1006 传纸条 /// DP+滚动数组

    题目大意: https://www.luogu.org/problemnew/show/P1006 题解 不难想到 求从起点到终点的两条不同的路 因为只能向右或向下走 所以纸条1和2不可能同时位于同一 ...

  3. vue 利用intersectionOberver实现全局appear/disappear事件

    搬运自:https://juejin.im/post/5cd10959f265da03a00fe5c6 效果: demo地址: https://codepen.io/deepkolos/pen/OYP ...

  4. JQuery和JavaScript常用方法的一些区别

    jquery 就对javascript的一个扩展,封装,就是让javascript更好用,更简单,为了说明区别,下面与大家分享下JavaScript 与JQuery 常用方法比较   jquery 就 ...

  5. Luogu P3033 [USACO11NOV]牛的障碍Cow Steeplechase(二分图匹配)

    P3033 [USACO11NOV]牛的障碍Cow Steeplechase 题意 题目描述 --+------- -----+----- ---+--- | | | | --+-----+--+- ...

  6. 踩坑记-java mysql 新增获取主键、DIY主键、UUID

    java mysql 获取主键.DIY主键.UUID,简单粗暴,代码如下: mapper.xml insert id="add" parameterType="com.x ...

  7. 新安装一个eclipse,导入一个web项目,右键点击项目选择Properties,找不到project facets和Server选项。

    解决方式: 1.点击:eclipse导航栏中点击Help->Install New Software 2.点击Add添加 3在弹出框中填写以下信息 name:keep(名字随便取) locati ...

  8. 【JZOJ3337】wyl8899的TLE

    description wyl8899今天也很刻苦的在做老师布置下来的题目! 这一天老师布置的题目是这样的: 给出两个仅含小写字母的字符串A和B,输出最大的k,使得A[1..k]是B的子串. A和B的 ...

  9. JDK源码阅读--StringBuffer

    public final class StringBuffer extends AbstractStringBuilder implements java.io.Serializable, CharS ...

  10. 使用Ajax在HTML页面中局部刷新页面(左边菜单右边页面)

    转载自:https://blog.csdn.net/Cenmen_17714/article/details/80969008 index.html <a href="javascri ...