leetcode:

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.

说明:给定一个字符串,返回最后一个word的长度

1)字符串后面可以有空格,所以要去尾部空格

2)可能不含word,所以要判断字符串是否为空、或者是否只含有空格

 class Solution {
public:
int lengthOfLastWord(const char *s) {
int len=strlen(s);
while(s[len-]==' ') len--; //去尾部空格
if(len<) return ; //去空格后是否为空字符串
int i=;
for(;s[len-]&&s[len-]!=' ';i++,len--); //求最后word长度
return i;
}
};

leetcode 题解: Length of Last Word的更多相关文章

  1. Leetcode(58)题解:Length of Last Word

    https://leetcode.com/problems/length-of-last-word/ 题目: Given a string s consists of upper/lower-case ...

  2. [leetcode] 18. Length of Last Word

    这个题目很简单,给一个字符串,然后返回最后一个单词的长度就行.题目如下: Given a string s consists of upper/lower-case alphabets and emp ...

  3. [LeetCode] 58. 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

    题目简述 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...

  5. Java for LeetCode 058 Length of Last Word

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

  6. LeetCode 58. Length of Last Word

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

  7. Java [Leetcode 58]Length of Last Word

    题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...

  8. LeetCode(48)-Length of Last Word

    题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...

  9. [leetcode]58. Length of Last Word最后一个词的长度

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

随机推荐

  1. ems server

    自DELPHI XE7始提供企业移动服务,方便移动等设备通过REST+JSON访问中间件提供的服务. EMS (Enterprise Mobility Services企业移动服务) 服务器给EMS客 ...

  2. 关于 mobile sui a外链 老是出现加载失败的解决办法

    mobile sui 框架里面的a本身都绑了了一个ajax方法,ajax只能处理同域,跨域就会出现问题 ,所以mobile sui 中的a如果是外链的话就会出现加载失败的提示,这种明显的bug,让用户 ...

  3. Linux下查看文件和文件夹大小的df和du命令

        转自:http://www.yayu.org/look.php?id=162 当磁盘大小超过标准时会有报警提示,这时如果掌握df和du命令是非常明智的选择. df可以查看一级文件夹大小.使用比 ...

  4. InnoDB存储引擎

    [InnoDB和MyISAM区别][ http://jeck2046.blog.51cto.com/184478/90499] InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型, ...

  5. oracle:自定义多行合并聚合函数

    原始表 COUNTRY CITY            -------------------- -------------- 中国 台北              中国 香港             ...

  6. 使用Office-Word的博客发布功能(测试博文)

    本人打算在博客园开博,但平时收集和整理资料都在OneNote中,又不想在写博客时还要进行复制粘贴操作,于是就想到了Microsoft Office自带的博客发布功能.在此做了一下测试,发布了此博文. ...

  7. perl学习笔记(4)——动态加载

    在写perl的时候,如果要应用到各种平台的话,比如linux 和windows,会遇到各种问题,有时就是要根据系统类型来加载各种库,之前写的就是这样的, if($^O eq 'linux'){ use ...

  8. ClassRequestHandler or VendorRequestHandler wIndex must be less than NumIFs

    P1_ro:20000EEA ClassRequestHandler ; CODE XREF: USB__HandleSetup+38j P1_ro:20000EEA LDRB R0, [R4,#4] ...

  9. MySQL中进行树状所有子节点的查询

    在Oracle 中我们知道有一个 Hierarchical Queries 通过CONNECT BY 我们可以方便的查了所有当前节点下的所有子节点.但很遗憾,在MySQL的目前版本中还没有对应的功能. ...

  10. Mac OS增删环境变量

    一.首先查看shell版本 使用命令echo $SHELL 如果输出的是:csh或者是tcsh,就是C Shell.如果输出的是:bash,sh,zsh,就是Bourne Shell的一个变种. Ma ...