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 ...
随机推荐
- man iptables 8
IPTABLES(8) iptables 1.6.0 IPTABLES(8) NAME iptables/ip6tables — administration tool for IPv4/IPv6 p ...
- 第7章 网络层协议(3)_ARP协议
3. ARP协议 3.1 ARP(Address Resolution Protocol)协议的工作过程和安全隐患 (1)计算机A和C通信之前,先检查ARP缓存中是否有计算机C的IP地址对应的MAC地 ...
- 普通数组和json数组的区别
PHP 数组 什么是数组? 数组是特殊的变量,它可以同时保存一个以上的值. 在 PHP 中创建数组 在 PHP 中, array() 函数用于创建数组 在 PHP 中,有三种数组类型: 索引数组 - ...
- Java 7-Java 循环结构 - for, while 及 do…while
Java 循环结构 - for, while 及 do...while 顺序结构的程序语句只能被执行一次.如果您想要同样的操作执行多次,,就需要使用循环结构. Java中有三种主要的循环结构: whi ...
- Centos7使用pxe安装KVM虚拟机
Centos7使用pxe安装KVM虚拟机 一.安装服务所需的软件 [root@localhost ~]yum install nginx dhcp vsftpd syslinux -y [root@l ...
- 在django中使用django_debug_toolbar进行日志记录
一.概述 django_debug_toolbar 是django的第三方工具包,给django扩展了调试功能. 包括查看执行的sql语句,db查询次数,request,headers,调试概览等. ...
- is 和 == 区别,id() ,回顾编码,encode(),decode()
1. is 和 == 区别 id()函数 == 判断两边的值 is 判断内存地址例 s = "alex 是 大 xx"# abc = id(s) # 得到内存地址# print(a ...
- Java GC如何判断对象是否为垃圾
查找内存中不再使用的对象 引用计数法 引用计数法就是如果一个对象没有被任何引用指向,则可视之为垃圾.这种方法的缺点就是不能检测到环的存在. 2.根搜索算法 根搜索算法的基本思路就是通过一系列名为”GC ...
- HANA私有云解决方案
在移动互联网时代,不支持在云上的部署一定会落伍的,HANA作为SAP力推的技术,对云的支持也做的很不错,今天我们就来探讨一下HANA私有云解决方案,至于公有云或者混合云,思路也是大同小异了. ...
- sql server ldf 日志文件清理