434. Number of Segments in a String
原题:
434. Number of Segments in a String
解题:
刚看到题目时,觉得可以通过统计空格个数,但想想有可能会有多个空格的情况
思路:
一:遍历字符,if条件碰到非空格时,计数加1,然后while循环跳过非空格字符,一直到最后
二:设置flag初始为0,当碰到非空格时,计数加1,且flag置1,当flag为1且碰到空格时,flag再重置为0
思路一是自己想的,思路二更巧妙,是论坛里摘抄的
AC代码:
思路一:
class Solution {
public:
int countSegments(string s)
{
int len = s.length();
int i = 0;
int count = 0;
for(; i < len; i++)
{
if(s[i]!=' ')
{
count++;
while(s[i]!=' '&&i<len)
{
i++;
}
i -= 1; //回退一个位置,因为for循环会累加
}
}
return count;
}
};
思路二:
class Solution {
public:
int countSegments(string s) {
int count=0;
int flag = 0;
for (int i=0;i<s.size();i++) {
if (flag ==0 && s[i]!=' ') {
count++;
flag = 1;
}
if (flag ==1 && s[i]==' ') {
flag = 0;
}
}
return count;
}
};
434. Number of Segments in a String的更多相关文章
- 【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 ...
- 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 ...
- [LC] 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 ...
- 【LeetCode】434. Number of Segments in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 正则表达式 字符串分割 日期 题目地址:htt ...
- 434 Number of Segments in a String 字符串中的单词数
统计字符串中的单词个数,这里的单词指的是连续的非空字符.请注意,你可以假定字符串里不包括任何不可打印的字符.示例:输入: "Hello, my name is John"输出: 5 ...
- LeetCode_434. Number of Segments in a String
434. Number of Segments in a String Easy Count the number of segments in a string, where a segment i ...
- [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 ...
- 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 ...
- 每天一道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 ...
随机推荐
- 0002 - Spring MVC 拦截器源码简析:拦截器加载与执行
1.概述 Spring MVC中的拦截器(Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户请求并作相应的处理.例如通过拦截器可以进行权限验证.记录请求信息的日 ...
- 做好平衡有多难?谈MMO的职业设计
转自:http://www.gameres.com/804893.html 首先要明确个概念:平衡不是在YY好的职业设计基础上去做调整,而是从游戏设计的开始就要打造一套有标准.可调节的游戏设计框架. ...
- Java开发各层对象专用名词含义 PO,VO,DAO,BO,DTO,POJO, BYO,Entity,JavaBean,JavaBeans
Java的几种名词(PO,VO,DAO,BO,POJO)解释 PO:persistant object 持久对象.可以看成是与数据库中的表相映射的java对象.最简单的PO就是对应数据库中某个表中的一 ...
- kafka的API操作
在集群的接收端 启动producer 在consumer这边能接收到producer发来的数据
- Django中的ORM相关操作:F查询,Q查询,事物,ORM执行原生SQL
一 F查询与Q查询: 1 . F查询: 在上面所有的例子中,我们构造的过滤器都只是将字段值与某个常量做比较.如果我们要对两个字段的值做比较,那该怎么做呢? Django 提供 F() 来做这样的 ...
- Session原理
详情移步:https://www.jianshu.com/p/2b7c10291aad
- ROS学习手记 - 2.1: Create and Build ROS Package 生成包(Python)
ROS学习手记 - 2.1: Create and Build ROS Package 生成包(Python) 时隔1年,再回来总结这个问题,因为它是ros+python开发中,太常用的一个操作,需要 ...
- 一段JS控制TD中图片的大小的代码
一段JS控制TD中图片的大小的代码 <table><tr><td id="otd"><div></div><img ...
- 【3-28】JavaScript的DOM操作
一.定义:DOM是一种用于HTML和XML文档的编程接口. 二.Windows对象操作: (一)Window.open(URL,name,features,replace) 1.URL;页面地址 2. ...
- Dubbo(2)--Dubbo常用配置文件解析及核心源码阅读
1.多版本支持 服务端 创建第二个接口实现类 package com.lf; public class HelloImpl2 implements IHello{ @Override public S ...