Leecode刷题之旅-C语言/python-434 字符串中的单词数
/*
* @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 字符串中的单词数的更多相关文章
- Leecode刷题之旅-C语言/python-344反转字符串
/* * @lc app=leetcode.cn id=344 lang=c * * [344] 反转字符串 * * https://leetcode-cn.com/problems/reverse- ...
- Leecode刷题之旅-C语言/python-26.删除数组中的重复项
/* * @lc app=leetcode.cn id=26 lang=c * * [26] 删除排序数组中的重复项 * * https://leetcode-cn.com/problems/remo ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-14.最长公共前缀
/* * @lc app=leetcode.cn id=14 lang=c * * [14] 最长公共前缀 * * https://leetcode-cn.com/problems/longest-c ...
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
随机推荐
- layui实现checkbox的目录树tree
layui.use([ 'tree' ], function() {$ = layui.jquery;form = layui.form;//获取节点数据getTreeData();}); funct ...
- 倒计时特效的CountAnimationLabel
倒计时特效的CountAnimationLabel 效果: 源码: CountAnimationLabel.h 与 CountAnimationLabel.m // // CountAnimation ...
- PHPWAMP自启异常,服务器重启后Apache等服务不会自动重启的原因分析
在使用“PHPWAMP自动任务”时,不少学生遇到如下问题: “phpwamp绿色集成环境重启动电脑(服务器)后,不会自动启动网站服务” (如果是其他环境或是自己搭建时遇到此问题,也是可以用此法解决) ...
- jquery-validation验证插件
参考网站:菜鸟教程 一.导入js <script src="http://static.runoob.com/assets/jquery-validation-1.14.0/lib/j ...
- 使用jenkins SonarQube gitlab 构建自动化发布系统
目前持续集成的生态越来越完善,工具也有很多,开源的或商业的.如: 最最流行的,也是使用最多的 Jenkins 有着持续集成DNA的ThoughtWorks GO.理念:"Deployment ...
- 【YY的GCD】
设 \[f(n)=\sum_{i=1}^N\sum_{j=1}^M[(i,j)=n]\] 我们的答案显然是 \[ans=\sum_{p\in prime}f(p)\] 设 \[F(n)=\sum_{i ...
- 3、Spring Cloud - Eureka(高可用Eureka Server集群)
在实际的项目中,可能有几十个或者几百个的微服务实例,这时 Eureka Server 承担了非 常高的负载.由于 Eureka Server 在微服务架构中有着举足重轻的作用,所以需要对 Eureka ...
- 31、springboot与任务
异步任务 测试如下: 进行等待三秒在进行应答 @Service public class AsynService { public void hello(){ try { Thread.sleep() ...
- Kali-linux Gerix Wifi Cracker破解无线网络
Gerix Wifi Cracker是另一个aircrack图形用户界面的无线网络破解工具.本节将介绍使用该工具破解无线网络及创建假的接入点. 9.3.1 Gerix破解WEP加密的无线网络 在前面介 ...
- Kali-linux密码在线破解
为了使用户能成功登录到目标系统,所以需要获取一个正确的密码.在Kali中,在线破解密码的工具很多,其中最常用的两款分别是Hydra和Medusa.本节将介绍使用Hydra和Medusa工具实现密码在线 ...