[抄题]:

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. [leetcode]_Validate Binary Search Tree

    题目:判断一棵二叉树是否合法.要求二叉树满足 左子树所有值 < 当前值 < 右子树所有值,并且所有点都满足这个条件. 思路: 1.从当前根节点判断,求根节点左子树最大值maxLeft,右子 ...

  2. win键盘映射成mac键盘

    在win7系统下安装了mac虚拟机,mac的快捷键与win的键盘不一样,所以ctrl+c,ctrl+v都用不了,于是找方法映射. 搜索到 keyremap4macbook,,进到官网Karabiner ...

  3. 关于ORM,以及Python中SQLAlchemy的sessionmaker,scoped_session

    orm(object relational mapping):对象关系映射. python面向对象,而数据库是关系型. orm是将数据库关系映射为Python中的对象,不用直接写SQL. 缺点是性能略 ...

  4. 在web.config中配置httpHandlers节点是的说明

    <system.web> <httpHandlers> <add verb="*" path="*.lcj" type=" ...

  5. tensorflow学习笔记(三十九):双向rnn

    tensorflow 双向 rnn 如何在tensorflow中实现双向rnn 单层双向rnn 单层双向rnn (cs224d) tensorflow中已经提供了双向rnn的接口,它就是tf.nn.b ...

  6. 基于ThinkPHP的开发笔记3-登录功能(转)

    1.前台登录用的form ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <for ...

  7. Note: log switch off, only log_main and log_events will have logs!

    真机(华为c8813)在Eclipase上测试,打不出logcat信息,只有这样的一句话:Note: log switch off, only log_main and log_events will ...

  8. 插入排序的JavaScript实现

    思想 每次在现有已经排好的数组的基础上排入一个新的数组项. 先把第一项看做是已经排好的,第二项应该排在第一项之前还是之后呢?当前两项排好后,第三项应该排在这已排好的两项的之前还是之后还是中间呢?当前三 ...

  9. RAC 11gR2模拟OCR和Votedisk损坏恢复过程

    1)破坏前的ocr和votedisk信息检查 检查ocr自动备份 [root@rac1 ~]# ocrconfig -showbackup rac2 2013/10/13 09:45:30 /u01/ ...

  10. UiAutomator viewer 在tools下启动、资源jar包在platforms下(如android22)

    1.UiAutomator viewer 在tools下启动. 2.资源jar包在platforms下(如android22):android.jar/ uiautomator.jar