Leetcode NO.58 Length of Last Word
来源:互联网 发布:python 数值计算精度 编辑:程序博客网 时间:2024/04/25 22:39
本题题目要求如下:
Given a string s consists of upper/lower-case alphabets and empty space characters ' '
, return the length of last word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
For example,
Given s = "Hello World"
,
return 5
.
这次用正着数,就是全遍历一遍,遇见空格的话,则停止记数,当然细节方面还要看代码。。
时间复杂度一样,不过理论上第一次的应该要快一些。
代码如下:
class Solution {public: int lengthOfLastWord(string s) { int cnt = 0; for (int i = s.length() - 1; i >= 0; --i) { if (s[i] != ' ') { ++cnt; } else if (cnt != 0) { break; } } return cnt; }};
0 0
- Leetcode NO.58 Length of Last Word
- LeetCode--No.58--Length of Last Word
- leetCode No.58 Length of Last Word
- LeetCode No.58 Length of Last Word
- [leetcode 58] Length of Last Word
- leetcode-58 Length of Last Word
- LeetCode #58 length of Last Word
- [LeetCode 58]Length of Last Word
- leetcode || 58、Length of Last Word
- LeetCode 58 Length of Last Word
- [Leetcode 58, easy] Length of Last Word
- Length of Last Word (leetCode 58)
- leetcode[58]:Length of Last Word
- 【leetcode c++】58 Length of Last Word
- leetcode 58:Length of Last Word
- LeetCode---(58)Length of Last Word
- leetcode-58-Length of Last Word
- Leetcode# 58 Length of Last Word
- AC++第二章答案
- 计算机PCB基础
- HDU 1541 Stars
- 两个海量文件找重复项
- 蓝桥杯 剪格子
- Leetcode NO.58 Length of Last Word
- Section 1.1 PROB Your Ride Is Here
- 《数据结构编程实验》 1.5.7Vertical Histogram
- Cause and Effect Diagram- Fishbone Diagram Analysis
- Linux crontab定时执行任务 命令格式与详细例子
- C# 异常处理经验(原则与方法)
- 隐藏及显示文件夹
- ios 学习总结day01关于UIview和IBAction 常见错误
- 1236排名