Leetcode(58)题解:Length of Last Word
https://leetcode.com/problems/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.
思路:
主要是确定最后一个单词的边界。注意特殊情况。
AC代码:
class Solution {
public:
int lengthOfLastWord(string s) {
int begin=-,end=-,j,n=s.size();
for(j=n-;j>=;j--)
if(s[j]!=' '){
begin=j;
break;
}
if(begin==-)
return ;
for(j=begin;j>=;j--){
if(s[j]==' '){
end=j+;
break;
}
}
if(end==-)
end=;
return begin-end+;
}
};
Leetcode(58)题解:Length of Last Word的更多相关文章
- leetcode || 58、Length of Last Word
problem: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ret ...
- leetcode 题解: Length of Last Word
leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...
- LeetCode OJ:Length of Last Word(最后一个词的长度)
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【算法】LeetCode算法题-Length Of Last Word
这是悦乐书的第155次更新,第157篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第14题(顺位题号是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/66】Length of Last Word/Plus One
LeetCode第58题: Given a string s consists of upper/lower-case alphabets and empty space characters ' ' ...
- 【一天一道LeetCode】#58. Length of Last Word
一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...
- 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...
随机推荐
- wordpress 使用jquery需要主要的问题
wordpress 使用jquery时,不能直接使用$, 而是用jQuery 代替$, 而且wordpress默认调用jquery
- [adb 连接不上的原因] 汇总
http://www.cnblogs.com/dazhao/p/6534128.html 查看android studio 的sdk路径( 点击工具栏 ?号旁边的类似下载按钮) SDK_Manager ...
- SQL注入与xss
1. 什么是SQL注入 所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.通过递交参数构造巧妙的SQL语句,从而成功获取 ...
- Python模块安装路径初探
在调用MySQL第三方工具mysqlrplsync是报错 mysqlrplsync --master=checksum:checksum@master_ip:3306 --slave=checksum ...
- BZOJ 2005 [Noi2010]能量采集 ——Dirichlet积
[题目分析] 卷积一卷. 然后分块去一段一段的求. O(n)即可. [代码] #include <cstdio> #include <cstring> #include < ...
- SPOJ GSS7 Can you answer these queries VII ——树链剖分 线段树
[题目分析] 问题放到了树上,直接链剖+线段树搞一搞. 调了300行+. (还是码力不够) [代码] #include <cstdio> #include <cstring> ...
- 【双向bfs】2017多校训练十 HDU 6171 Admiral
[题意] 现在给出一个三角矩阵,如果0编号的在点(x,y)的话,可以和(x+1,y),(x-1,y),(x+1,y+1),(x-1,y-1)这些点进行交换. 我们每一次只能对0点和其他点进行交换.问最 ...
- hdu 2100
#include<stdio.h> #include<string.h> char a[300],b[300]; int a1[300],b1[300]; int main() ...
- 乘法运算(codevs 3254)
题目描述 Description 编制一个乘法运算的程序. 从键盘读入2个100以内的正整数,进行乘法运算并以竖式输出. 输入描述 Input Description 输入只有一行,是两个用空格隔开的 ...
- [bzoj1110][POI2007]砝码Odw_贪心
bzoj-1110 POI-2007 砝码Odw 参考博客:http://hzwer.com/4761.html 题目大意:在byteotian公司搬家的时候,他们发现他们的大量的精密砝码的搬运是一件 ...