作者: 负雪明烛
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)的更多相关文章

  1. 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 ...

  2. 434. Number of Segments in a String

    原题: 434. Number of Segments in a String 解题: 刚看到题目时,觉得可以通过统计空格个数,但想想有可能会有多个空格的情况 思路: 一:遍历字符,if条件碰到非空格 ...

  3. 【LeetCode】833. Find And Replace in String 解题报告(Python)

    [LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  4. 【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 ...

  5. [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 ...

  6. 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 ...

  7. [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 ...

  8. 434 Number of Segments in a String 字符串中的单词数

    统计字符串中的单词个数,这里的单词指的是连续的非空字符.请注意,你可以假定字符串里不包括任何不可打印的字符.示例:输入: "Hello, my name is John"输出: 5 ...

  9. 【LeetCode】806. Number of Lines To Write String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用ASIIC码求长度 使用字典保存长度 日期 题目 ...

随机推荐

  1. 简单mvc框架核心笔记

    简单mvc框架核心笔记 看了thinkphp5的源码,模仿写了一个简单的框架,有一些心得笔记,记录一下 1.目录结构 比较简单,没有tp那么复杂,只是把需要的核心类写了一些. 核心类库放在mykj里, ...

  2. A Child's History of England.47

    CHAPTER 13 ENGLAND UNDER RICHARD THE FIRST, CALLED THE LION-HEART In the year of our Lord one thousa ...

  3. Qt5的安装和编译

    Ubuntu18.04安装Qt5 1.配置unbuntu 和宿主机共享文件夹安装vmware-tools 2.下载 Qt  http://download.qt.io/archive/qt/ 3.修改 ...

  4. Spark基础:(七)Spark Streaming入门

    介绍 1.是spark core的扩展,针对实时数据流处理,具有可扩展.高吞吐量.容错. 数据可以是来自于kafka,flume,tcpsocket,使用高级函数(map reduce filter ...

  5. 【编程思想】【设计模式】【行为模式Behavioral】观察者模式Observer

    Python转载版 https://github.com/faif/python-patterns/blob/master/behavioral/observer.py #!/usr/bin/env ...

  6. 【Linux】【Shell】【Basic】Programming

    shell脚本编程: 编程语言的分类:根据运行方式 编译运行:源代码-->编译器(编译)-->程序文件 解释运行:源代码-->运行时启动解释器,又解释器边解释边运行 根据其编程过程中 ...

  7. 阿里巴巴Java开发手册摘要(一)

    一命名风格 1.代码中的命名均不能以下划线或美元符号开始,也不能以下划线或美元符号结尾. 反例:_name / $name / name_ / name$ 2.类名使用UpperCamelCase风格 ...

  8. 沉淀vue相关知识(主要还是个人积累用)

    路由懒加载的配置: const Home= () =>import('../components/Home') //使用ES6中的路由懒加载的方式 const About= () =>im ...

  9. 【代码优化】List.remove() 剖析

    一.犯错经历 1.1 故事背景 最近有个需求大致的背景类似: 我已经通过一系列的操作拿到一批学生的考试成绩数据,现在需要筛选成绩大于 95 分的学生名单. 善于写 bug 的我,三下五除二完成了代码的 ...

  10. centos添加本地yum源

    一.简介 centos6系列于2020年11月份已经停止提供服务,现在各大镜像源已经关闭centos6的yum源,需要下载镜像后在本地搭建yum源方便使用. 最好将镜像下载后传到OSS中,这样从阿里云 ...