【LeetCode】434. Number of Segments in a String 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/number-of-segments-in-a-string/#/description
题目描述
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.
Please note that the string does not contain any non-printable characters.
Example:
Input: "Hello, my name is John"
Output: 5
题目大意
字符串中有多少个不包含空字符的子字符串。
解题方法
统计
这个题方法应该自己使用过的,不过是倒着来。回想自己以前在每个单词后面输入一个空格,但是行尾不要空格的时候怎么做的?不就是判断第一个单词的前面不打空格,在之后的所有单词的前面打了一个空格。这个题的思路是不是倒着来?
判断某个单词的开始的字符前面是不是空格,如果是字符串的第一个字符也会把技术加1,这样就能统计出所有用空格分割的字符串段的个数。
public class Solution {
public int countSegments(String s) {
int count = 0;
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) != ' ' && (i == 0 || s.charAt(i - 1) == ' ')){
count++;
}
}
return count;
}
}
正则表达式
正则\s
表示匹配空格,+
表示匹配一次或者任意多次。所以有以下代码。
public int countSegments(String s) {
String trimmed = s.trim();
if (trimmed.length() == 0) return 0;
else return trimmed.split("\\s+").length;
}
python版本:
class Solution(object):
def countSegments(self, s):
"""
:type s: str
:rtype: int
"""
s = re.sub("\s+", " ", s)
s = s.strip()
if not s: return 0
return len(s.split(" "))
另外,如果\S
就是匹配所有的非空字符,所以我们只要知道有多少个匹配就好了。
class Solution(object):
def countSegments(self, s):
"""
:type s: str
:rtype: int
"""
return len(re.findall("\S+", s))
字符串分割
python的split()函数,默认的参数就是按照连续空格进行分割。注意,千万不要写参数为" "
。
class Solution(object):
def countSegments(self, s):
"""
:type s: str
:rtype: int
"""
return len(s.split())
日期
2017 年 5 月 5 日
2018 年 11 月 24 日 —— 周六快乐
【LeetCode】434. Number of Segments in a String 解题报告(Python)的更多相关文章
- LeetCode 806 Number of Lines To Write String 解题报告
题目要求 We are to write the letters of a given string S, from left to right into lines. Each line has m ...
- 434. Number of Segments in a String
原题: 434. Number of Segments in a String 解题: 刚看到题目时,觉得可以通过统计空格个数,但想想有可能会有多个空格的情况 思路: 一:遍历字符,if条件碰到非空格 ...
- 【LeetCode】833. Find And Replace in String 解题报告(Python)
[LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 【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 ...
- [LeetCode] 434. Number of Segments in a String_Easy
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 ...
- 434 Number of Segments in a String 字符串中的单词数
统计字符串中的单词个数,这里的单词指的是连续的非空字符.请注意,你可以假定字符串里不包括任何不可打印的字符.示例:输入: "Hello, my name is John"输出: 5 ...
- 【LeetCode】806. Number of Lines To Write String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用ASIIC码求长度 使用字典保存长度 日期 题目 ...
随机推荐
- snakmake 小练习
最近在学习snakemake 用于生信流程管理,现在用一个snakemake 来完成小任务:将在某一文件夹下的多个bam文件截取一部分,然后建立索引,在提取出fastq序列,最后比对回基因组. 需要两 ...
- Generic recipe for data analysis with general linear model
Generic recipe for data analysis with general linear model Courtesy of David Schneider State populat ...
- Python time&datetime模块
1.time&datetime模块 time&datetime是时间模块,常用以处理时间相关问题 time.time() #返回当前时间的时间戳timestamp time.sleep ...
- Go 性能提升tips--边界检查
1. 什么是边界检查? 边界检查,英文名 Bounds Check Elimination,简称为 BCE.它是 Go 语言中防止数组.切片越界而导致内存不安全的检查手段.如果检查下标已经越界了,就会 ...
- 总结HashSet以及分析部分底层源码
总结HashSet以及分析部分底层源码 1. HashSet继承的抽象类和实现的接口 继承的抽象类:AbstractSet 实现了Set接口 实现了Cloneable接口 实现了Serializabl ...
- linux 常用查看命令
linux 常用查看命令 目录 linux 常用查看命令 linux 查看内存/进程-ps/top linux 查看磁盘存储-df linux 查看io读写-iotop linux 查看端口占用-ne ...
- Swift-技巧(十一)重写运算符
摘要 基础数据的运算可以直接使用四则运算符.在 Swift 中也可以通过重写四则运算符的方式,让 struct 或者 class 创建的结构体或者对象也能像基础数据那样直接使用四则运算符. Swift ...
- 对于vue项目更新迭代导致上传至服务器后出现Loading chunk {n} failed和Unexpected token <的解决方式
相信大家对于vue项目的维护与更新中会遇见很多问题,其中有两种情况最为常见. 一种是Loading chunk {n} failed,这种情况出现的原因是vue页面更新上传至服务器后,由于vue默认打 ...
- A Child's History of England.35
The other two clung to the yard for some hours. At length the young noble said faintly, 'I am exhaus ...
- 【讨论】APP的免填邀请码解决方案
00x0 具体需求 app中已注册的用户分享一个含有邀请码的二维码,分享到朋友圈新用户在朋友圈打开这个这个链接下载app.新用户安装后打开app后就自动绑定邀请码要求用户不填写任何东西 朋友老板出差给 ...