Leetcode 58 - Length of Last Word
来源:互联网 发布:mysql建立数据库 编辑:程序博客网 时间:2024/06/07 03:17
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 temp = 0; int result = 0; if(s.empty()) return 0; int n = s.length(); for(int i=0;i<n;i++){ if(s[i]!=' ') temp++; //此处注意,如果不增加s[i-1]!=' '这个条件 //出现连续空格时,result = temp会多次执行,导致result被0覆盖 //增加此条件可以确保result = temp只执行一次 if(s[i]==' ' && s[i-1]!=' '){ result = temp; temp = 0; } } //如果字符串不以空格结尾,则for循环结束时,temp还未赋值给result //故直接返回temp if(s[n-1]!=' ') return temp; //否则返回result else return result; }};
0 0
- [leetcode 58] Length of Last Word
- leetcode-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, 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
- [leetcode-58]Length of Last Word(c)
- leetcode 58: Length of Last Word
- leetCode #58 Length of Last Word
- Leetcode 55 - Jump Game
- Thz
- C# BitmapImage对象和byte[]之间的互转、BitmapImage和Bitmap互换
- SQLite数据库的分页查询
- intent机制详解
- Leetcode 58 - Length of Last Word
- 解决ubuntu上VMware:(vmware-installer.py:3847): Gtk-WARNING **: 无法在模块路径中找到主题引擎:“murrine”,
- 指示符“include”、“-include”和“sinclude”
- list<T>作为datagridview的datasource
- ElasticSearch学习
- laravel之模板使用
- python threading中处理主进程和子线程的关系
- VPN——代理技术
- 安全测试之传输层保护不足