问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章

  1. [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 ...

  2. C#LeetCode刷题之#532-数组中的K-diff数对(K-diff Pairs in an Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3716 访问. 给定一个整数数组和一个整数 k, 你需要在数组里找 ...

  3. C#LeetCode刷题之#557-反转字符串中的单词 III(Reverse Words in a String III)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3955 访问. 给定一个字符串,你需要反转字符串中每个单词的字符顺 ...

  4. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  5. 力扣(LeetCode)字符串中的单词数 个人题解

    统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my name is John" ...

  6. leetcode刷题笔记08 字符串转整数 (atoi)

    题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...

  7. C#LeetCode刷题之#720-词典中最长的单词(Longest Word in Dictionary)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4120 访问. 给出一个字符串数组words组成的一本英语词典.从 ...

  8. #leetcode刷题之路30-串联所有单词的子串

    给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置.注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考 ...

  9. C#LeetCode刷题之#671-二叉树中第二小的节点(Second Minimum Node In a Binary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4100 访问. 给定一个非空特殊的二叉树,每个节点都是正数,并且每 ...

  10. C#LeetCode刷题之#840-矩阵中的幻方(Magic Squares In Grid)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3752 访问. 3 x 3 的幻方是一个填充有从 1 到 9 的不 ...

随机推荐

  1. SqlServer同义词

    最近在项目中遇到跨库操作具有相同表结构的数据表的问题.(A库中的数据表a和B库中的数据表b,两者表结构相同) 跨库操作中我们一般是为了实现不同数据库中表字段信息,字段状态等实时同步,可能很多人会想到使 ...

  2. 阅读手札 | 手把手带你探索『图解 HTTP』

    前言 本文已经收录到我的 Github 个人博客,欢迎大佬们光临寒舍: 我的 Github 博客 学习清单: 一.网络基础 TCP/IP 通常使用的网络(包括互联网)是在 TCP/IP 协议族的基础上 ...

  3. Python+selenium+unittest+HTMLTestReportCN单元测试框架分享

    分享一个比较基础的,系统性的知识点.Python+selenium+unittest+HTMLTestReportCN单元测试框架分享 Unittest简介 unittest是Python语言的单元测 ...

  4. vuex : 模块化改造

    我们知道,vuex是vue技术栈中很重要的一部分,是一个很好用的状态管理库. 如果你的项目没有那么复杂,或者对vuex的使用没有那么重度,那么,是用不着modules功能的. 但如果你写着写着就发现你 ...

  5. C++语法小记---同名覆盖

    同名覆盖 子类中的同名成员会覆盖父类中的同名成员,但是在内存中仍然存在,只是无法直接访问,需要加上域名才能访问 子类中的同名函数会覆盖父类中的函数,复写是同名覆盖的一种特殊情况,只要不是多态场景,复写 ...

  6. 理解Linux的硬链接与软链接-转载

    理解Linux的硬链接与软链接 来自:https://www.ibm.com/developerworks/cn/linux/l-cn-hardandsymb-links/index.html

  7. springMVC -- 对接UEditor(富文本编辑器)

    工作中需要用到UEditor编辑文本,在与springMVC进行整合时,出现了一些问题,结果导致,在进行图片上传时出现如下提示: 上网查询了很多相关资料,此处简要记录下,防止以后遇到类似问题. 一种方 ...

  8. vue 修改路由

    直接放代码: this.$router.push({ path: "/login" });

  9. RecyclerView设置空视图

    RecyclerView貌似不能直接设置空视图,所以可以自定义一个RecyclerView继承自RecyclerView并设置一个数据监听者监视数据状态. MyCyclerView.java pack ...

  10. 手写Vuex源码

    Vuex原理解析 Vuex是基于Vue的响应式原理基础,所以无法拿出来单独使用,必须在Vue的基础之上使用. 1.Vuex使用相关解析 main.js   import store form './s ...