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

因为edge case 很多,考虑一不周全就是错的,比如 "", " ", " a", " a", "a     b     " 这几个case。

最后我AC的思路是用 i 来记录当前的词长,如果遇到空格则说明此词完毕,把 j = i; i = 0 然后继续直到串尾巴'\0'

最后判断 i 是否为0,如果是则返回j,否则返回i

 int lengthOfLastWord(const char *s) {
int i=, j=;
while (*s != '\0') {
if (*s == ' ') {
if (i != ){
j = i;
i = ;
}
}
else {
i++;
}
s++;
}
if (i == ) return j;
return i;
}

[LeetCode] Length of Last Word的更多相关文章

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

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

  2. LeetCode——Length of Last Word

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

  3. [leetcode]Length of Last Word @ Python

    原题地址:https://oj.leetcode.com/problems/length-of-last-word/ 题意: Given a string s consists of upper/lo ...

  4. Leetcode: Length of Last Word in python

    Length of Last Word Total Accepted: 47690 Total Submissions: 168587     Given a string s consists of ...

  5. [Leetcode] Length of last word 最后一个单词的长度

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

  6. [LeetCode] Length of Last Word 字符串查找

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

  7. LeetCode Length of Last Word 最后一个字的长度

    class Solution { public: int lengthOfLastWord(const char *s) { ; string snew=s; ,len=strlen(s); ]; ) ...

  8. leetcode 题解: Length of Last Word

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

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

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

随机推荐

  1. httpclient+Jsoup总结

    Jsoup.parse解析HTML字符串,如Jsoup.parse("<html><head><title>Firstparse</title> ...

  2. centos rpmforge repo

    在这里找到合适的rpm包:http://pkgs.repoforge.org/rpmforge-release/ 下载 wget http://pkgs.repoforge.org/rpmforge- ...

  3. pycharm3.4 下svn 项目checkout&配置

    pycharm 社区版: 3.4 1. checkout 项目 注意,之前配置好:设置里面的一些配置:(以下勾勾不要勾上) 2. checkout 项目之后,做以下操作: vcs ->enabl ...

  4. Linux查看文件编码格式及文件编码转换

    Linux查看文件编码格式及文件编码转换   如果你需要在Linux 中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中默认的文件格式是GBK(gb2312),而L ...

  5. NPOI教程

    NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. NPOI 官方网站:http://npoi.codeplex.com/( ...

  6. leetcode Add and Search Word - Data structure design

    我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...

  7. C#之串口

    1.字符发送 string strSend = "00 01 02 03"; serialPort1.Write(strSend); 2.字符接收 ReadDataFromSeri ...

  8. POJ 1917

    http://poj.org/problem?id=1917 poj的字符串的一道水题. 题意么无关紧要, 反正输出的第一行就是把那个<>去掉,s1<s2>s3<s4&g ...

  9. 2014北大研究生推免机试(校内)-复杂的整数划分(DP进阶)

    这是一道典型的整数划分题目,适合正在研究动态规划的同学练练手,但是和上一个随笔一样,我是在Coursera中评测通过的,没有找到适合的OJ有这一道题(找到的ACMer拜托告诉一声~),这道题考察得较全 ...

  10. Xcode无法启动ios模拟器的问题

    一.问题描述 开发过程需要来回切换ios模拟器调试程序,开始在iPhone 4s下调试,然后切换到iPhone 6s Plus,再切换回iPhone 4s,遇到无法启动ios模拟器.错误提示如下: 二 ...