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. makefile中 $@, $^, $<, $?含义

    $@ 表示目标文件 $^ 表示所有的依赖文件 $< 表示第一个依赖文件 $? 表示比目标还要新的依赖文件列表 例子 root_num.exe: root_num.o my_root.o gcc ...

  2. C++中多态中构造函数与析构函数的调用

    做个实验,看一下成员变量的构造析构,父类子类的构造析构,以及虚函数对调用的影响. #include <iostream> using namespace std; class Member ...

  3. TFrame bug

    delphi 10.1.2 工程里有很多fram 正确的工程文件dproj中fram的定义是 <DCCReference Include="Unit15frame.pas"& ...

  4. 机器学习-文本数据-文本的相关性矩阵 1.cosing_similarity(用于计算两两特征之间的相关性)

    函数说明: 1. cosing_similarity(array)   输入的样本为array格式,为经过词袋模型编码以后的向量化特征,用于计算两两样本之间的相关性 当我们使用词频或者TFidf构造出 ...

  5. Python conda 入门

    https://blog.csdn.net/yimingsilence/article/details/79388205 查看版本 conda --version 列出所有的Python环境 cond ...

  6. mysqldump: Got error: 1066: Not unique table/alias

    mysqldump: Got error: 1066: Not unique table/alias myql 导出时提示如下: [root@localhost mysql]# mysqldump  ...

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

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

  8. 【Noip模拟 20161005】公约数

    问题描述 小ww最近仔细研究了公约数,他想到了以下问题:现有nn个正整数,从中选k(2≤k≤n)k(2≤k≤n) 个,设这kk个数的最大公约数为gg,则这kk个数的价值为k×gk×g.求这个价值的最大 ...

  9. Delphi TQuery 的Locate用法

    Help里的解释 function Locate(const KeyFields: String; const KeyValues: Variant; Options: TLocateOptions) ...

  10. python语言中的数据类型之集合

    数据类型 集合类型    set 用途:1.关系运算        2.去重 定义方式:在{}内用逗号分隔开多个元素,但元素的特点是 1.集合内元素必须是不可变类型 2.集合内元素无序 集合内元素不能 ...