LeetCode-434. Number of Segments in a String
来源:互联网 发布:儿童可视化编程软件 编辑:程序博客网 时间:2024/06/06 02:47
Description
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
题目分析
本题中要求计算字符串中段(segment)的个数,例如:
str[]="Hello, my name is John"
return 5;
字符串石头人中由4个空格将字符串分割为5部分,每部分均由连续的非空字符构成,因此返回5。
这里的段(segment)可以理解为“单词”,但是需要注意的是一般意义上的单词并不包含后边接着的标点符号。而这里的如果标点符号与单词字母之间没有空格,则认为标点符号也属于“单词”的一部分,属于同一个段(segment).例如Hello,单词hello后边的逗号与o之间并没有空格,而是紧挨在一起,因此hello,同属一段(segment)。
解题思路
本题字符串中各段与段之间均有空格符分开,而且字符串中不含有非可打印字符。通过逐个扫描字符串中的字符,如果发现空格,则段数加一,直到字符串最后的’\0’。但是如果让字符串中出现连续的空格就会使上述方法失效。因此,我们需要同时判断两个字符,如果前一个字符不是空格,而当前字符为空格则,段数加一,这种方法可以处各理段之间以空格隔开(包括单个空格与连续多个空格)和最后一段后接空格的情况。
特殊情况:
(1) 最后一段的判断。字符串均是以’\0’结尾,因此最后一段后通常不跟空格,而是跟’\0’。因此,若当前字符不为空,而下一个字符你是’\0’,则段数加一。
(2) 字符串为空字符串。此时直接返回0。
(3) 字符串只含有一个字符且不是空格。对于长度为一的字符串,若其内容不是空格,则返回1。
(4) 字符串中仅由空格组成(包括单个空格和多个空格)。字符串段数,初始值为0,对于空格字符串,由于不含有非空格字符,不满足段数+1的条件,因此,输出仍然为0。
C代码实现
#include <stdio.h>#include <string.h>#include <stdlib.h>int countSegments(char* s) { int strle=strlen(s); if(strle==0) //空字符串,长度为0; return 0; if(strle==1 &&s[0]!=32) //单个非空字符串 return 1; printf("strlen=%d\n",strle); int i=1,j=0; while(s[i]!='\0') { if((s[i-1]!=32&&s[i]==32 )||( s[i]!=32&&s[i+1]=='\0')) //连续空格与最末尾段 { j++; } i++; } return j;}int main(){ char* strs1=" "; //空格 char* strs2="a"; //单个非空字符串 char* strs3="Hello, my name is John"; //正常字符串 int segment1=0,segment2=0,segment3=0; segment1=countSegments(strs1); segment2=countSegments(strs2); segment3=countSegments(strs3); printf("segment1=%d\n",segment1); printf("segment2=%d\n",segment2); printf("segment3=%d\n",segment3); return 0;}
参考文献
[1] https://leetcode.com/problems/number-of-segments-in-a-string/#/description
[2] http://www.cnblogs.com/grandyang/p/6137386.html
- Leetcode Number of Segments in a String
- LEETCODE--Number of Segments in a String
- LeetCode 434. Number of Segments in a String
- LeetCode 434. Number of Segments in a String
- 【Leetcode 434. Number of Segments in a String】
- leetcode 434. Number of Segments in a String
- LeetCode 434. Number of Segments in a String
- LeetCode 434. Number of Segments in a String
- Leetcode 434. Number of Segments in a String (Easy) (cpp)
- 【LeetCode】 434. Number of Segments in a String
- [leetcode]434. Number of Segments in a String
- [leetcode]434. Number of Segments in a String
- LeetCode 434. Number of Segments in a String
- Leetcode 434. Number of Segments in a String
- Leetcode-434. Number of Segments in a String
- LeetCode-434. Number of Segments in a String
- [LeetCode]434. Number of Segments in a String
- 【Leetcode】434. Number of Segments in a String
- 第12节-Linux 文件系统的运行方式
- F
- selenium 错误集锦->ElementNotVisibleException
- HBuilder使用夜神模拟器调试Android应用
- 简单选择排序(C)
- LeetCode-434. Number of Segments in a String
- 16.5的PowerDesigner中的to be Implemented找不到,inherited也找不到
- win10+VS2013编译caffe的common.cpp错误error C4996: ‘getpid': The POSIX name for this item is deprecated.
- Linux达人养成记Ⅰ笔记
- python——数据可视化:matplotlib,seaborn,pandas
- 十大基础排序 · 四 --- 快速排序(不稳定)挖坑填补法+区间分割法+3种优化方法
- GNU/GPL/Linux/GCC工具链历史
- eclipse快捷键
- SqlServer存储过程介绍