Leetcode 58 Length of Last Word
来源:互联网 发布:微信商城源代码 php 编辑:程序博客网 时间:2024/05/16 19:23
题目要求:
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
.
解答比较简单,唯一需要注意的是对在统计last word length前后,对空格的判断
代码如下:
class Solution {public: int lengthOfLastWord(string s) { int len = s.length(); if(len == 0) return 0; int k = 0; int flag = 0; for(int i = 0; i < len; i++) { if(s[len - i - 1] == ' ' && flag == 0) continue; else if(s[len - i - 1] == ' ' && flag == 1) break; else if(s[len - i - 1] != ' ') { k++; flag = 1; } } return k; }};
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
- 一个for求阶层和
- 我是一个Java class
- 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
- PHP 获取服务器详细信息
- win8磁盘占用率经常达到99%的一种解决办法
- Leetcode 58 Length of Last Word
- Java线程池
- 99%的人都理解错了HTTP中GET与POST的区别
- 微信小程序注册教程-详细图文教程
- Eclipse调试模式时候,设置高亮颜色
- Lua table表函数库
- DeepContour论文笔记(CVPR2015)
- POJ 3321 DFS序+线段树
- Data Binding(数据绑定)用户指南