C#LeetCode刷题之#434-字符串中的单词数(Number of Segments in a String)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3941 访问。
统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。
请注意,你可以假定字符串里不包括任何不可打印的字符。
输入: "Hello, my name is John"
输出: 5
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.
Input: "Hello, my name is John"
Output: 5
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3941 访问。
public class Program {
public static void Main(string[] args) {
var s = "Hello, my name is Iori!";
var res = CountSegments(s);
Console.WriteLine(res);
s = "It's mine!";
res = CountSegments2(s);
Console.WriteLine(res);
s = "using System.Collections.Generic;";
res = CountSegments3(s);
Console.WriteLine(res);
s = "Hello, my daughter's name is Cherry!";
res = CountSegments4(s);
Console.WriteLine(res);
Console.ReadKey();
}
private static int CountSegments(string s) {
var split = s.Split(' ');
var index = 0;
foreach(var item in split) {
if(item.Trim() != "") index++;
}
return index;
}
private static int CountSegments2(string s) {
string[] split = s.Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
return split.Length;
}
private static int CountSegments3(string s) {
var res = 0;
var notEmpty = false;
for(var i = 0; i < s.Length; i++) {
if(s[i] != ' ') notEmpty = true;
else {
if(notEmpty) {
res++;
notEmpty = false;
}
}
}
if(notEmpty) { res++; }
return res;
}
private static int CountSegments4(string s) {
var res = 0;
var preEmpty = true;
for(var i = 0; i < s.Length; i++) {
if(preEmpty && s[i] != ' ') res++;
preEmpty = s[i] == ' ';
}
return res;
}
}
以上给出4种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3941 访问。
5
2
2
6
分析:
考虑到部分运行库的使用,以上4种算法的时间复杂度应当均为: 。
C#LeetCode刷题之#434-字符串中的单词数(Number of Segments in a String)的更多相关文章
- [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 ...
- C#LeetCode刷题之#532-数组中的K-diff数对(K-diff Pairs in an Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3716 访问. 给定一个整数数组和一个整数 k, 你需要在数组里找 ...
- C#LeetCode刷题之#557-反转字符串中的单词 III(Reverse Words in a String III)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3955 访问. 给定一个字符串,你需要反转字符串中每个单词的字符顺 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
- 力扣(LeetCode)字符串中的单词数 个人题解
统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my name is John" ...
- leetcode刷题笔记08 字符串转整数 (atoi)
题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...
- C#LeetCode刷题之#720-词典中最长的单词(Longest Word in Dictionary)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4120 访问. 给出一个字符串数组words组成的一本英语词典.从 ...
- #leetcode刷题之路30-串联所有单词的子串
给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置.注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考 ...
- C#LeetCode刷题之#671-二叉树中第二小的节点(Second Minimum Node In a Binary Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4100 访问. 给定一个非空特殊的二叉树,每个节点都是正数,并且每 ...
- C#LeetCode刷题之#840-矩阵中的幻方(Magic Squares In Grid)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3752 访问. 3 x 3 的幻方是一个填充有从 1 到 9 的不 ...
随机推荐
- 关于Mint-UI中loadmore组件的兼容性问题
源代码 遇到的问题 写完了之后数据加载,渲染等等都是没有问题的,但是测试总是提上滑刷新不能用,因为是远程开发,测试提就得改,看代码看文档,看半天看不出来问题,想到了兼容性问题,发现也有人遇到这个坑.安 ...
- xenomai内核解析--双核系统调用(三)--如何为xenomai添加一个系统调用
版权声明:本文为本文为博主原创文章,转载请注明出处.如有错误,欢迎指正. @ 目录 一.添加系统调用 二.Cobalt库添加接口 三.应用使用 一.添加系统调用 下面给xenomai添加一个系统调用g ...
- C++语法小记---类型转换
类型转换 C++类型转换主要包含两种: 隐式类型转换 小类型向大类型转换 char -> int short ->int int -> unsigned int int -> ...
- python 简单粗暴的生产的验证码
import os import pygame import random from pygame.locals import * count = 0; 生成验证码的函姝 def get_code() ...
- CAS实现SSO 单点登录
结构 CAS分为两部分,CAS Server和CAS Client CAS Server用来负责用户的认证工作,就像是把第一次登录用户的一个标识存在这里,以便此用户在其他系统登录时验证其需不需要再次登 ...
- I 2 C、 SPI、 USB驱动架构
根据图12.4, Linux倾向于将主机端的驱动与外设端的驱动分离, 而通过一个核心层将某种总线的协议进行抽象, 外设端的驱动调用核心层API间接过渡到对主机驱动传输函数的调用. 对于I 2 C. S ...
- JSONObject遍历
导入JSONObject和JSONArray所需要的jar包 JSONObject所必需的6个jar包: commons-beanutils-1.7.0.jar commons-collections ...
- HTML5其他标签应用
HTML5 是下一代 HTML 标准. HTML5 多媒体 音频标签 <audio src=" "></audio> 视频标签 <video src= ...
- Kafka和SpringBoot
事先必备: kafka已安装完成 1.目录结构 2.父pom <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...
- Python os.openpty() 方法
概述 os.openpty() 方法用于打开一个新的伪终端对.返回 pty 和 tty的文件描述符.高佣联盟 www.cgewang.com 语法 openpty()方法语法格式如下: os.open ...