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. Keil MDK Code、RO-data、RW-data、ZI-data数据段

      Program Size: Code=10848 RO-data=780 RW-data=372 ZI-data=868   Code 表示程序代码指令部分 存放在Flash区 RO-data 表 ...

  2. 结构类模式(一):适配器(Adapter)

    定义 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 类适配器模式 使用继承的方式实现没有提供的接口从而达到适配到新系统的需求. ...

  3. 关于html和body高度 登录页面登录框永远保持在页面垂直水平居中位置

    HTML CSS 解说,水平居中就不用多说了,给div.login_wrap设置个宽度,然后margin:0 auto;就可以做到.但是这里这样做,这里使用了两层div 外层使用margin:0 au ...

  4. (剑指Offer)面试题29:数组中出现次数超过一半的数字

    题目: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...

  5. as [Frame]元标签

    [Frame(factoryClass="XXX_Class")] 利用Frame元标签.在主SWF类名上面添加 [Frame(factoryClass="加载类类名&q ...

  6. CloudStack4.2 更新全局参数API

    测试更新全局参数API http://192.168.153.34:8080/client/api?command=updateConfiguration&response=json& ...

  7. HTML要点(五)<iframe>标签

    浏览器支持:全部支持 定义和用法 iframe 元素会创建包含另外一个文档的内联框架(即行内框架). HTML 与 XHTML 之间的差异 在 HTML 4.1 Strict DTD 和 XHTML ...

  8. A debugger is already attached

    Today is the last day that all the laptops of winXP OS should be upgrade to WIN7. After updated. whe ...

  9. Why isn't there a SendThreadMessage function?

    Here's an interesting customer question: Windows has PostMessage and SendMessage. It also has PostTh ...

  10. 负margin使用权威指南

    自CSS2早在1998年,推荐表的使用已经慢慢褪色成背景和历史书中.正因为如此,CSS布局从那时起一直编码优雅的代名词. 的所有CSS概念设计师所使用,奖项可能需要给负margin的使用是最至少谈论的 ...