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. C#基础语法实例荟萃

    匿名类 action = new UploadHandler(context, new UploadConfig() { AllowExtensions = Config.GetStringList( ...

  2. 【架构】How To Use HAProxy to Set Up MySQL Load Balancing

    How To Use HAProxy to Set Up MySQL Load Balancing Dec  2, 2013 MySQL, Scaling, Server Optimization U ...

  3. sharepoint2010匿名访问

    怎样在SharePoint 2010网站中启用匿名访问 SharePoint 2010的改动比较大,尤其是相对SharePoint Portal Server 2003来说.本文介绍在SharePoi ...

  4. vim python设置

    http://www.cnblogs.com/Leo-Forest/archive/2012/04/06/2435237.html http://linux-wiki.cn/wiki/zh-hans/ ...

  5. Python模块之optparse

    参考: http://www.cnblogs.com/captain_jack/archive/2011/01/11/1933366.html https://docs.python.org/2/li ...

  6. mysql性能优化学习笔记-存储引擎

    mysql体系架构 客户端(java.php.python等) mysql服务层(连接管理器.查询解析器.查询优化器.查询缓存) mysql存储引擎(innodb.myisam等) 存储引擎针对表而言 ...

  7. 利用ssh-copy-id无需密码登录远程服务器

    本地机器生成公钥和私钥 ssh-keygen -t rsa 一路回车,最后会在~/.ssh目录下生成id_rsa和id_rsa.pub这两个文件. 与远程服务器建立信任机制 ssh-copy-id - ...

  8. socket端口重复占用问题

    1.一个服务端进程在主动释放端口后(调用close)端口状态为TIME_WAIT,这时再去监听同样的端口,不论是否设置SO_REUSEADDR,都能监听成功,也能接收到客户端的连接,但是无法收到数据. ...

  9. 多线程BackroundWorker 使用

    参考文章:http://www.cnblogs.com/inforasc/archive/2009/10/12/1582110.html using System; using System.Coll ...

  10. Python 之 【re模块的正则表达式学习】

    摘要: re模块包括操作正则表达式的函数,一些工作中都需要用到,现在说明下使用方法. 使用说明: 一,re模块下的函数:            函数             描述 compile(pa ...