leetcode 58
58. Length of Last Word
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) {
if(s.length() == )
{
return ;
}
int n = ;
int l = s.length();
for(int i = l-; i >= ; i--)
{
if(n == && s[i] == ' ')
{
continue;
}
if(s[i] != ' ')
{
n ++;
}
else
{
break;
}
}
return n;
}
};
leetcode 58的更多相关文章
- [LeetCode] 58. Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Leetcode 58 Length of Last Word 难度:0
https://leetcode.com/problems/length-of-last-word/ int lengthOfLastWord(char* s) { int ans = 0; int ...
- LeetCode: 58. Length of Last Word(Easy)
1. 原题链接 https://leetcode.com/problems/length-of-last-word/description/ 2. 题目要求 给定一个String类型的字符串,字符串中 ...
- Leetcode(58)题解:Length of Last Word
https://leetcode.com/problems/length-of-last-word/ 题目: Given a string s consists of upper/lower-case ...
- Java实现 LeetCode 58 最后一个单词的长度
58. 最后一个单词的长度 给定一个仅包含大小写字母和空格 ' ' 的字符串 s,返回其最后一个单词的长度. 如果字符串从左向右滚动显示,那么最后一个单词就是最后出现的单词. 如果不存在最后一个单词, ...
- Leetcode 58 Length of Last Word 字符串
找出最后一个词的长度 class Solution { public: int lengthOfLastWord(string s) { , l = , b = ; while((b = s.find ...
- LeetCode 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Java [Leetcode 58]Length of Last Word
题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- LeetCode 58 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
随机推荐
- VVDocumenter-Xcode 规范注释生成器 插件之安装调试适应新版本
1.下载地址: 很多时候,为了快速开发,很多的技术文档都是能省则省,这个时候注释就变得异常重要,再配合Doxygen这种注释自动生成文档的,就完美了.但是每次都要手动输入规范化的注释,着实也麻烦,但有 ...
- What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
http://stackoverflow.com/questions/998979/difference-between-and-in-rails/25617607#25617607 http://s ...
- 怎么查询局域网内全部电脑IP和mac地址..
在局域网内查询在线主机的IP一般比较简单,但局域网内全部电脑的IP怎么才能够查到呢?查询到IP后我还要知道对方的一些详细信息(如MAC地址.电脑名称等)该怎么查询呢??? 工具/原料 Windows ...
- transactionCurrencyId needs to be supplied to format a transaction money field.
问题背景: 在CRM 4 表单中加入了自定义的,money类型的字段,如果就报错 解决方法:要显示金额类型的字段时,要保证 entity 的 TransactionCurrencyId 这个字段中是有 ...
- [SQL]SQL中把一个字段的数据分多行显示
其实你完全可以写个自定义函数就OK了 . =================================================================== create func ...
- DatabaseError: no such table: django_session
最近我也遇到这个问题了,从网上查了下,说是数据库同步出了问题,只需要运行如下命令:python manage.py syncdb就可以了 (这是django1.4之前的命令,1.4之后的是 pytho ...
- PHP 文件读取 fread、fgets、fgetc、file_get_contents 与 file 函数
fread().fgets().fgetc().file_get_contents() 与 file() 函数用于从文件中读取内容. fread() fread() 函数用于读取文件(可安全用于二进制 ...
- The specified system/compiler is not supported
之前安装了QT的4.5.3版本,现需要用到phonon库,因此卸载后想重新安装4.7版本,但当使用./configure编译时出现The specified system/compiler is no ...
- NOIP 2005 过河
过河 Time Limit: 1000MS Memory Limit: 65536K 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石 ...
- java 实例之杨辉三角
public class study{ public static void main(String args[]){ int i,j,level=7; int Yang[][] = new int[ ...