LeetCode 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
.
这个题目只要注意如果字符串以‘ ’结尾的情况就行了例如"Hello World "的返回值也是5
class Solution {
public:
int lengthOfLastWord(string s) {
size_t length=;
bool tag=false;
for(int i=s.size()-;i>=;--i)
{
if(s[i]!=' ')
{
tag=true;
++length;
}
if(s[i]==' '&&tag)return length;
}
return length;
}
};
LeetCode 58. Length of Last Word的更多相关文章
- [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. 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 (最后单词的长度) 解题思路和方法
Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space charact ...
- 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 难度:0
https://leetcode.com/problems/length-of-last-word/ int lengthOfLastWord(char* s) { int ans = 0; int ...
- 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
1.题目 58. Length of Last Word——Easy Given a string s consists of upper/lower-case alphabets and empty ...
- 【一天一道LeetCode】#58. Length of Last Word
一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...
随机推荐
- vs2013创建mvc项目体系找不到指定文件
在Visual Studio 2013中创建新MVC项目,(2013默认创建的就是mvc5的项目) 断定后提示,体系找不到指定的文件.(Exception HRESULT:08x0070002): 究 ...
- jquery.find()
http://www.365mini.com/page/jquery-find.htm
- Bouncycastle中的RSA技术以及解决之道
一个使用bouncycastle进行安全操作的实用类 2007-04-13 12:54 import java.io.*;import java.security.*;import java.secu ...
- js 标签云效果
下载:http://files.cnblogs.com/zjfree/js_tag_list.rar 效果如下: 源码如下: <html> <head> <meta ht ...
- webform 页面传值的方法总结
ASP.NET页面之间传递值的几种方式 页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值.存储对象传值.ajax.类.model.表单等.但是一般来说,常用的较简单有Quer ...
- (转)颜色渐变CSS
本文转载自:http://www.cnblogs.com/yichengbo/archive/2012/10/27/2742618.html IE系列 filter: progid:DXImageTr ...
- Puppet master/agent installation on RHEL7
==================================================================================================== ...
- golang rbac框架
在 https://github.com/mikespook/gorbac/tree/v1.0 github上新的版本是开发板,得用这里的老版 demo package main import ( & ...
- JS截取后缀名,文件全名,非后缀名的方法---收藏(冷饭_)
<script language="javascript" type="text/javascript"> //取整个文件的路径并且把文件名赋给文件 ...
- Akka(二) - Future
1. future的所有方法都是非阻塞立即返回的 (1)future都要有TimeOut和ExecutionContextExecutor这2个隐士参数 (2)打印future object Hell ...