/*
* @lc app=leetcode.cn id=434 lang=c
*
* [434] 字符串中的单词数
*
* https://leetcode-cn.com/problems/number-of-segments-in-a-string/description/
*
* algorithms
* Easy (29.13%)
* Total Accepted: 4.2K
* Total Submissions: 14.2K
* Testcase Example: '"Hello, my name is John"'
*
* 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。
*
* 请注意,你可以假定字符串里不包括任何不可打印的字符。
*
* 示例:
*
* 输入: "Hello, my name is John"
* 输出: 5
*
*
*/
int countSegments(char* s) {
if(s==NULL||strlen(s)==) return ;
int i=,j,count=;
while(i<strlen(s)){
while(i<strlen(s)&&s[i]==' ') i++;//i每次移到第一个非空格处
j = i;
while(j<strlen(s)&&s[j]!=' ')j++;//j每次移到第一个空格处,i和j之间就是一个单词
if(i<j){
i=j;
count++;
}
else
{
return count;//当最后一个单词后面有空格时,从此出口结束。
}
}
return count;//当最后一个单词后面没有空格时,从此出口结束。
}

思路是 双指针,i找到第一个非空格字符,j从i的位置开始找到第一个为空格的字符,i<j,那么ij之间的就是一个单词,计数就加一,然后i移动到j的位置进行下一次寻找。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=434 lang=python3
#
# [434] 字符串中的单词数
#
# https://leetcode-cn.com/problems/number-of-segments-in-a-string/description/
#
# algorithms
# Easy (29.13%)
# Total Accepted: 4.2K
# Total Submissions: 14.2K
# Testcase Example: '"Hello, my name is John"'
#
# 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。
#
# 请注意,你可以假定字符串里不包括任何不可打印的字符。
#
# 示例:
#
# 输入: "Hello, my name is John"
# 输出: 5
#
#
#
class Solution:
def countSegments(self, s: str) -> int:
return len(s.split())

python就很简单了。一个split搞定。

Leecode刷题之旅-C语言/python-434 字符串中的单词数的更多相关文章

  1. Leecode刷题之旅-C语言/python-344反转字符串

    /* * @lc app=leetcode.cn id=344 lang=c * * [344] 反转字符串 * * https://leetcode-cn.com/problems/reverse- ...

  2. Leecode刷题之旅-C语言/python-26.删除数组中的重复项

    /* * @lc app=leetcode.cn id=26 lang=c * * [26] 删除排序数组中的重复项 * * https://leetcode-cn.com/problems/remo ...

  3. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  4. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  5. Leecode刷题之旅-C语言/python-14.最长公共前缀

    /* * @lc app=leetcode.cn id=14 lang=c * * [14] 最长公共前缀 * * https://leetcode-cn.com/problems/longest-c ...

  6. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  7. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  8. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  9. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

随机推荐

  1. 深入浅出SharePoint——使用WinDbg进行调试

  2. Programming Assignment 3: Baseball Elimination

    编程作业三 作业链接:Baseball Elimination & Checklist 我的代码:BaseballElimination.java 问题简介 这是一个最大流模型的实际应用问题: ...

  3. 016.2 String

    内容:String方法+练习 #######################################比较方法:equals()字符串长度:int length()字符的位置:int index ...

  4. 剖析php脚本的超时机制

    在做php开发的时候,经常会设置max_input_time.max_execution_time,用来控制脚本的超时时间.但却从来没有思考过背后的原理. 趁着这两天有空,研究一下这个问题.文中源码取 ...

  5. js字符串和数组

    sustr  substring  slice的联系与区别 str.substr(2,5) //从索引2开始截取5个字符,原有字符串str不变 str.substring(2,5) //从索引2开始截 ...

  6. ZooKeeper学习之路 (五)ZooKeeper API的简单使用 增删改查

    zookeeper文件系统的增删改查 public class ZKDemo1 { private static final String CONNECT_STRING = "hadoop1 ...

  7. 【Git】本地与GitHub同步

    按步骤一步一步来,成功啦~ 以管理员身份运行Git-bash 要求输入用户名,密码 成功推入github~~加油加油 补充: 将仓库中的改动同步到本地 在git-bash中进入项目目录下,使用git ...

  8. HTML 5中的文件处理之FileAPI

    在众多HTML5规范中,有一部分规范是跟文件处理有关的,在早期的浏览器技术中,处理小量字符串是js最擅 长的处理之一.但文件处理,尤其是二进制文件处理,一直是个空白.在一些情况下,我们不得不通过Fla ...

  9. vector详讲(三)实例

    移动语义: push语句有时候会通过移动语义来提高性能 #include <iostream> #include <vector> class Element { public ...

  10. MYSQL统计

    今天  select * from 表名 where to_days(时间字段名) = to_days(now());  昨天  SELECT * FROM 表名 WHERE TO_DAYS( NOW ...