统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。

请注意,你可以假定字符串里不包括任何不可打印的字符。

示例:

输入: "Hello, my name is John"
输出: 5

class Solution(object):
def countSegments(self, s):
"""
:type s: str
:rtype: int
"""
s = s.split() return len(s)

split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串.

str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等

leetcode434 字符串中的单词树(python)的更多相关文章

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

  2. leetcode python反转字符串中的单词

    # Leetcode 557 反转字符串中的单词III### 题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. **示例1:** 输入: "L ...

  3. LeetCode 557:反转字符串中的单词 III Reverse Words in a String III

    公众号:爱写bug(ID:icodebugs) 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. Given a string, you need to reve ...

  4. [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  5. [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  6. [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  7. leetcode-解题记录 557. 反转字符串中的单词 III

    题目: 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出 ...

  8. [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  9. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

随机推荐

  1. 当有多个相同的DIV时,我怎么判断我点击的是哪个

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. libev个人问题解惑

    我们的游戏后端一直以来用的都是libev,之前尝试过去读源码,因为里面用了大量宏和自己也不够耐心的原因,一直没有看懂.这次终于痛下决心,一定要啃下它,于是在这个星期调整自己的工作学习方式(在读源码的过 ...

  3. 爬虫笔记八——Scrapy实战项目

    (案例一)手机App抓包爬虫 1. items.py import scrapy class DouyuspiderItem(scrapy.Item): # 存储照片的名字 nickName = sc ...

  4. linux 读取文本的最后几行

    tail -n 100 filename    读取file的最后100行 tail: n. 尾巴:踪迹:辫子:燕尾服 vt. 尾随:装上尾巴 vi. 跟踪:变少或缩小 adj. 从后面而来的:尾部的 ...

  5. 格兰杰因果 Granger causality

    格兰杰因果关系(Granger causality )是基于预测的因果关系统计概念.根据格兰杰因果关系,如果信号X1“格兰杰Causes”(或“G-Causes”)信号X2,则X1的过去值应该包含有助 ...

  6. multi gpu inference with tfserving or keras

    在tfserving上,目测只能在每个gpu上起docker    https://github.com/tensorflow/serving/issues/311#issuecomment-4801 ...

  7. PCRE does not support \L, \l, \N{name}, \U, or \u...

    PCRE does not support \L, \l, \N{name}, \U, or \u... 参考文章:YCSUNNYLIFE 的 <php 正则匹配中文> 一.报错情景: 使 ...

  8. CSS3参数matrix(n,n,n,n,n,n)定义 2D 转换,使用六个值的矩阵。那这六个值分别代表了什么参数?

    matrix( a, b, c, d, e, f );a 水平缩放b 水平倾斜c 垂直倾斜d 垂直缩放e 水平移动f 垂直移动

  9. zabbix简单的操作(添加主机)

    zabbix是一种监控软件,我用的是centos7.5版本 Zabbix是一个基于WEB界面的提供分布式监视功能的企业级的开源解决方案. Zabbix既可以监控操作系统(Linux/Windows/A ...

  10. u-boot initf_bootstage函数分析

    这篇博客主要分析 init_sequence_f 函数指针数组中的initf_bootstage函数: static int initf_bootstage(void){    bool from_s ...