[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

第一位只会是单词,不会是空格

[思维问题]:

以为从空格开始数,计算单词数

[一句话思路]:

一般都是正面思考,直接数单词就行

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

正面思考就行

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

class Solution {
public int countSegments(String s) {
//cc
if (s.length() == 0) {
return 0;
}
//ini
int ans = 0; //for loop
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != ' ' && (i == 0 || s.charAt(i - 1) == ' ')) {
ans++;
}
} //return
return ans;
}
}

[代码风格] :

434. Number of Segments in a String 字符串中的单词个数的更多相关文章

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

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

  2. Leetcode434.Number of Segments in a String字符串中的单词数

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

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

  4. 【easy】Number of Segments in a String 字符串中的分段数量

    以空格为分隔符,判断一个string可以被分成几部分. 注意几种情况:(1)全都是空格 (2)空字符串(3)结尾有空格 思路: 只要统计出单词的数量即可.那么我们的做法是遍历字符串,遇到空格直接跳过, ...

  5. 434. Number of Segments in a String

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

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

  7. 【LeetCode】434. Number of Segments in a String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 正则表达式 字符串分割 日期 题目地址:htt ...

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

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

随机推荐

  1. (四)canvas绘制路径

    save() 样式不受污染的起始范围 shadowOffsetX 阴影x轴的距离 shadowOffsetY 阴影y轴的距离 shadowBlur 模糊度 shadowColor 阴影颜色 resto ...

  2. EL表达式可以直接放在url的“ ”里面

    <div class="hc-prm-search search flr"> <form action="/userCenter/projectInfo ...

  3. hexo配置主题发表文章

    将本地hexo博客部署到github上了,如果想换我们自己喜欢的主题(默认为landscape),同时也要发表发表文章呢,下面给介绍下: 1,进入hexo官网,预览自己想要的主题,https://gi ...

  4. 【ftp】服务器的链接命令

    1. 连接ftp服务器 格式:ftp [hostname| ip-address] a)在Linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密 ...

  5. 洛谷 P3225 [HNOI2012]矿场搭建

    传送门 题目大意:建设几个出口,使得图上无论哪个点被破坏,都可以与出口联通. 题解:tarjian求割点 首先出口不能建在割点上,找出割点,图就被分成了几个联通块. 每个联通块,建出口.如果割点数为0 ...

  6. Python学习流程

    这是我在过去几家公司招聘到工程师,Python入职培训的过程. 时间分为4周,全部自学,仅提供大纲.适用于Web方向: 1.Week1:读完<简明Python教程>,适应Python开发环 ...

  7. css关系选择符

    <!Doctype html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. C# 用wps(api v9) 将word转成pdf

    我们不产生代码只是代码的搬运工 我们先来看一段跑不起来的代码  ..各种未将对象应用到实例.. using System; using System.Collections.Generic; usin ...

  9. 致Oracle DBA 的一封信 (网上流传)

    1. 数据库的可用度,DBA 说了“不算”   --物化视图,加快查询速度 某些时候数据库的可用性,并不由DBA所设定.因为即使DBA对数据库有绝对掌控权,但用户可能从自己的工作和应用角度,与DBA的 ...

  10. mysql 统计sql

    1.按照月份统计数据 SELECT DATE_FORMAT(d.create_time,'%Y-%m') months,COUNT(id) AS scannum FROM detail d GROUP ...