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.

class Solution {
public:
int lengthOfLastWord(string s) {
if(s.empty()) return ; int len = s.length();
int i = len - ;
int lastLen = ; // 去掉空格
while(s[i] == ' ' && i>=){
--i;
}//while
while(i >= && s[i] != ' '){
++lastLen;
--i;
}//while
return lastLen;
}
};

58. Length of Last Word (String)的更多相关文章

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

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

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

  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】58 - Length of Last Word

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

  5. 58. Length of Last Word

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

  6. Java [Leetcode 58]Length of Last Word

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

  7. 58. Length of Last Word【leetcode】

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

  8. <LeetCode OJ> 58. Length of Last Word

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

  9. 58. Length of Last Word(easy, 字符串问题)

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

随机推荐

  1. Eclipse集成SonarLint

    https://docs.sonarqube.org/display/PLUG/Writing+Custom+Java+Rules+101

  2. 解决Java Web项目中Word、Excel等二进制文件编译后无法打开的问题

    今天写新项目的时候遇到一个问题,在resources目录下存储的.xlsx文件,编译过后会增大几kb,无法打开. Google了一番之后,发现问题源自于maven-resources-plugin这个 ...

  3. UI5-学习篇-10-本地UI5应用发布到SAP前端服务器

    1.本地UI5应用发布 点击项目名,右键Deploy,Deploy to Sapui5 ABAP Repository 选择SAP系统连接名,发布或是更新应用 注意上图中,SAPUI5应用版本与选择的 ...

  4. 尚硅谷springboot学习8-yaml基本语法

    1.基本语法 k:(空格)v:表示一对键值对(空格必须有): 以空格的缩进来控制层级关系:只要是左对齐的一列数据,都是同一个层级的 server: port: 8081 path: /hello 属性 ...

  5. 一个未完成的2.6.32-220内核踩内存crash分析记录

    遇到一个crash,log如下: BUG: unable to handle kernel NULL pointer dereference at (null) IP: [<ffffffff81 ...

  6. 【358】GitHub 上面文件夹下载方法

    参考:https://www.bilibili.com/read/cv210500/ 参考:https://www.jianshu.com/p/743ecc20ffb2 软件下载:Downloads ...

  7. Android中使用Lambda表达式开发

    参考文章:ImportNew 要在Android开发中使用lambda表达式,首先需要在 Module 的build.gradle中加入: compileOptions { targetCompati ...

  8. 30种提高mysql处理速度的方法

    1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉 ...

  9. python字典dict的成对运算

    dict = {'age': 18, 'name': 'jin', 'sex': 'male', }# for k,v in dict.items():# print(k,v)# v1 = dict[ ...

  10. Linux初学时的一些常用命令(4)

    1. 磁盘 查看当前磁盘使用情况 df -h 查看某个文件大小 du -sh 文件名 如果不输入文件名,默认是当前目录的所有文件之和,即当前目录大小 2. 系统内存 free 参数详解:https:/ ...