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的更多相关文章

  1. leetcode || 58、Length of Last Word

    problem: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ret ...

  2. leetcode 题解: Length of Last Word

    leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...

  3. LeetCode OJ:Length of Last Word(最后一个词的长度)

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  4. 【算法】LeetCode算法题-Length Of Last Word

    这是悦乐书的第155次更新,第157篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第14题(顺位题号是58).给定一个字符串,包含戴尔字母.小写字母和空格,返回最后一个单 ...

  5. [LeetCode] 58. Length of Last Word 求末尾单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  6. 【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 ' ' ...

  7. 【一天一道LeetCode】#58. Length of Last Word

    一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...

  8. 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 ...

  9. 【LeetCode】58. Length of Last Word 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...

随机推荐

  1. 组合数学的卡特兰数 TOJ 3551: Game of Connections

    这个就是卡特兰数的经典问题 直接用这个公式就好了,但是这个题涉及大数的处理h(n)=h(n-1)*(4*n-2)/(n+1) 其实见过好几次大数的处理了,有一次他存的恰好不多于30位,直接分成两部分l ...

  2. 【java基础 11】java集合框架学习

    导读:本篇博客主要是从整体上了解java的集合框架,然后主要介绍几个自己在项目中用到的结构,比如说:hashtable.hashmap.hashset.arraylist等! 一.宏观预览 从宏观上看 ...

  3. 九度oj 题目1024:畅通工程

    题目描述:     省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可能建设公路的若干条道 ...

  4. 【Luogu】P2051中国象棋(DP)

    题目链接 去看STDCALL的题解吧 #include<cstdio> #include<cctype> #define mod 9999973 inline long lon ...

  5. 北京集训TEST13——PA(Goodness)

    题目: Description 桌面上放有 n 张卡牌.对于每张卡牌,一面是绿色的,另一面是红色的.卡牌的每一面都标有一个整数.对于卡牌a和卡牌b,卡牌a对卡牌b的好感度为卡牌a绿色面的数与卡牌b红色 ...

  6. yum update 出错解决办法

    卸载掉yum和python 于是我卸载了python和yum,觉得自己重新安装python和yum. 步骤1:卸载python rpm -qa|grep python|xargs rpm -e --a ...

  7. 维修队列(bzoj 1500)

    Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一 ...

  8. HDU 4433 locker(12年天津,DP)

    4576 njczy2010 C Accepted 860 KB 140 ms G++ 2063 B 2014-10-16 09:51:19 哎,为啥1000*100*100的复杂度的dp就不敢敲了呢 ...

  9. Laravel5.1的控制器分组

    Laravel5.1的控制器分组 默认的控制器时直接在app\Http\Controllers下,创建分组名字,路由配置使用:分组名\控制器名@方法名.

  10. Android开发把项目打包成apk-(转)

    做完一个Android项目之后,如何才能把项目发布到Internet上供别人使用呢?我们需要将自己的程序打包成Android安装包文件--APK(Android Package),其后缀名为" ...