[抄题]:

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. 细说 const

    1.const 简单应用 const int pp=0 //pp 为整形常量,不能修改 还有另外一种不常用的方式 但是最容易误导 int const pp=0 //pp 为整形常量,不能修改 记住这两 ...

  2. 转:使用django-admin.py创建django工程

    原文:http://blog.csdn.net/a921800467b/article/details/8257352 安装Django首先需要安装数据库,可选的数据库有好几种,我选择的是MySQL数 ...

  3. django国际化总结

    转:http://blog.csdn.net/ybdesire/article/details/46806739

  4. Jmeter ----关于上传图片接口

    转自:http://www.cnblogs.com/linglingyuese/p/4514808.html 需求 1 2 3 4 5 6 7 8 9 post上传   Request: { &quo ...

  5. implicit declaration of function 'init_MUTEX'

    'init_MUTEX' 在新版本系统上已被淘汰 改用sema_init(sem, 1);

  6. php时间 显示刚刚 几分钟前等

    功能示例: $now = time();foreach($sys_res as $k => $v){ $day = intval(floor(($now - $v->system_time ...

  7. 监控mysql 脚本

    监控mysql脚本 http://oldboy.blog.51cto.com/2561410/986905

  8. 使用妹子UI开发的体验分享

    前阵子看到一个类似bootstrap的前端UI框架,好奇心驱使下,去琢磨了一些,最终决定网站改版用这个UI试试效果: 首页+头部: 投稿页: 现成拷贝过来的评论列表: 总结: 上手难度: (熟悉boo ...

  9. windows下通过.bat运行java程序

    在windows下运行Java项目,单独的jar可以使用,java -jar xxx.jar 运行,如果是一个zip包,里面包含了class文件和所依赖的jar的时候,可以使用 (也可以以看看这里): ...

  10. java代码水仙花

    总结:分离出百位,十位,各位,我总是模模糊糊的,总是分不清取膜与除号的作用区别: “%”的意思是“取膜”,表示取得的是余数 “/”的意思是除,得到的是除数. package com.a; //求水仙花 ...