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: ...
随机推荐
- CSU-1803: 2016 ,同余定理!
int main() { int n,m; while(~scanf("%d%d",&n,&m)) { int r1=n/2 ...
- hdu6071[最短路+解不等式] 2017多校4
求出所有,从2走到x所需的花费在对 t = 2*min(d1,2, d2,3) 模运算下, 所有剩余系的最短路即可(把一个点拆成 t 个点, 每个点代表一种剩余系), 知道了所有剩余系就可以凑出答案 ...
- Opencv学习笔记——视频进度条的随动
1. CvCapture结构体: CvCapture是一个结构体,用来保存图像捕获的信息,就像一种数据类型(如int,char等)只是存放的内容不一样,在OpenCv中,它最大的作用就是处理视频时(程 ...
- BZOJ2561 最小生成树 【最小割】
题目 给定一个边带正权的连通无向图G=(V,E),其中N=|V|,M=|E|,N个点从1到N依次编号,给定三个正整数u,v,和L (u≠v),假设现在加入一条边权为L的边(u,v),那么需要删掉最少多 ...
- 【2018.4.5】Shoi2017题集
这三道题分别对应bzoj4868~4870,pdf没法往这放,因此放弃了. T1: 方法1(正解):三分法 考虑暴力枚举最晚公布的时间x,关注到2操作是没有负面影响的1操作,所以如果A大于B,那么只需 ...
- Vmware error:无法获得 VMCI 驱动程序的版本: 句柄无效。
error:无法获得 VMCI 驱动程序的版本: 句柄无效.驱动程序“vmci.sys”的版本不正确.请尝试重新安装 VMware Workstation.开启模块 DevicePowerOn 的操作 ...
- hdu 1189 并查集
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Objective-C单例模式的常用实现
oc中单例模式可以使用以下方法来实现 + (YourClass *)sharedInstance { static dispatch_once_t once; static YourClass *sh ...
- Redis命令行之String
一.Redis之String简介 1. String是redis最基本的数据类型,一个key对应一个value. 2. String是二进制安全的,可以包含任何数据,例如图片或序列化的对象. 3. S ...
- android apk程序升级
1 .设置apk版本号 Androidmanifest.xml <manifest xmlns:android="http://schemas.android.com/apk/res/ ...