Leetcode434.Number of Segments in a String字符串中的单词数
统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。
请注意,你可以假定字符串里不包括任何不可打印的字符。
示例:
输入: "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字符串中的单词数的更多相关文章
- 434 Number of Segments in a String 字符串中的单词数
统计字符串中的单词个数,这里的单词指的是连续的非空字符.请注意,你可以假定字符串里不包括任何不可打印的字符.示例:输入: "Hello, my name is John"输出: 5 ...
- [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 ...
- 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 ...
- 【easy】Number of Segments in a String 字符串中的分段数量
以空格为分隔符,判断一个string可以被分成几部分. 注意几种情况:(1)全都是空格 (2)空字符串(3)结尾有空格 思路: 只要统计出单词的数量即可.那么我们的做法是遍历字符串,遇到空格直接跳过, ...
- 每天一道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 ...
- Java实现 LeetCode 434 字符串中的单词数
434. 字符串中的单词数 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my nam ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- [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 ...
- 【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 ...
随机推荐
- visual studio 注释模板
/*----------------------------------------------------------------* 项目名称 :$rootnamespace$* 项目描述 :* 类 ...
- UMP系统架构 RabbitMQ
- 3_基本框架_VMXON
原理参考 3卷 23.7节等 本节实施流程参考Intel手册: 3卷 31.5节 1 vt整体框架; 首先 开锁: 1 开启 Cr4.[VMXE]: 上一节,检测了 VMX 需要的环境:最后一个 CR ...
- mysql主从跳过错误
mysql主从复制,经常会遇到错误而导致slave端复制中断,这个时候一般就需要人工干预,跳过错误才能继续 跳过错误有两种方式: 1.跳过指定数量的事务 mysql>stop slave; m ...
- odoo 在更多下面直接调用方法
<record id="action_get_qc_result" model="ir.actions.server"> <field nam ...
- data hazard in CPU pipeline
1, background info 5 stages in CPU pipeline: IF, ID, EX, MM, WB IF – Instruction Fetch ID – Instruct ...
- CycloneII lcell_comb 和 lcell_FF 的结构
1,lcell_comb结构 2,lcell_FF结构 from : cycloneii_eda_fd.pdf
- zabbix自动发现主机(转)
zabbix自动发现主机 2018年06月15日 18:02:52 loyal-Wang 阅读数:817更多 个人分类: zabbix 版权声明:本文为博主原创文章,转载请注明出处. https: ...
- 84 落单的数 III
原题网址:http://www.lintcode.com/zh-cn/problem/single-number-iii/# 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到 ...
- struts2的default.properties详解
Struts 2框架有两个核心配置文件:struts.xml和struts.properties 其中struts.xml文件主要负责管理应用中的Action映射,以及该Action包含的Result ...