LeetCode 434. Number of Segments in a String
来源:互联网 发布:aerial mac 下载 编辑:程序博客网 时间:2024/05/23 23:48
题目
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
思考
通过空格来判断字符串分多少部分,主要是考虑多种特殊情况(比如全空格等)。
答案
c++
class Solution {public: int countSegments(string s) { if (s.length() == 0) return 0; int result = 0; for (int i = 1; i < s.length(); i++) { if (s[i] == ' ' && s[i-1] != ' ') { result++; } } if (s[s.length()-1] != ' ') { result++; } return result; }};
阅读全文
0 0
- 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
- 《刻意练习》读书笔记之二
- 国庆音标集训收获总结
- jsp+sevlet
- CentOS7命令
- c语言中关于二维数组的指针访问
- LeetCode 434. Number of Segments in a String
- Spring(一)框架学习
- 深度学习与中医的对比
- Hibernate 一对多关联映射对象 级联增删改查示例
- 使用JPCAP实现ARP欺骗
- 循环结构
- ftp 下载
- ubuntu 16.06LTS 环境安装eclipse 太卡的问题
- u-boot FIT image