L58: Length of Last Word

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開始,遇到’ ‘表示word结束,注意边界情况处理

优化:能够从字符串末尾開始处理

class Solution {
public:
int lengthOfLastWord(string s) {
int strLen = s.length();
if(strLen == 0)
return 0; int begin = 0;
int end = 0;
int pos = 0;
bool word_start = false;
while(pos < strLen)
{
if(s[pos] != ' ' && !word_start)
{
begin = pos;
word_start = true;
}
else if(s[pos] == ' ' && word_start)
{
end = pos;
word_start = false;
}
pos++;
}
if (word_start && pos == strLen)
end = strLen;
return end - begin;
}
};

Leetcode题解(5):L58/Length of Last Word的更多相关文章

  1. &lt;LeetCode OJ&gt; 58. Length of Last Word

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

  2. [leetcode.com]算法题目 - Length of Last Word

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

  3. LeetCode(59)Length of Last Word

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

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

  5. [LeetCode]题解(python):079 Word Search

    题目来源 https://leetcode.com/problems/word-search/ Given a 2D board and a word, find if the word exists ...

  6. 【leetcode❤python】 58. Length of Last Word

    #-*- coding: UTF-8 -*-#利用strip函数去掉字符串去除空格(其实是去除两边[左边和右边]空格)#利用split分离字符串成列表class Solution(object):   ...

  7. leetcode 题解: Length of Last Word

    leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...

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

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

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

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

随机推荐

  1. Listview多种布局的使用

    ListView中有两个可以用来让ListView可以在视图中显示多种布局的方法,分别是getItemType和getViewTypeCount 其中 getItemViewType返回的是有参数po ...

  2. 文档声明和HTML样式表

    文档声明 不是注释也不是元素,总是在HTML的第一行 书写格式:<!DOCTYPE HTML> 是用于通知浏览器目前文档正使用哪一个HTML版本(相关属性 lang) 若不写文档声明,浏览 ...

  3. Elasticsearch--扩展索引结构

    目录 索引树形数据 索引非扁平数据 索引关系型数据 使用嵌套对象 评分与嵌套查询 使用主从关系 索引树形数据 使用path_analyzer分析树形数据字段 索引非扁平数据 数据如下: { " ...

  4. MAMP中Python安装MySQLdb

    Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. MySQLdb 是用于Python链接Mysql数据库的接口,它实现了 Py ...

  5. 第一节:重写(new)、覆写(overwrite)、和重载(overload)

    一丶重写<NEW> 子类重写父类方法,方法里加new, eg: public new void CommonMethord1(string msg){} 子类继承父类中的普通方法,如果在子 ...

  6. JAVA基础——链表结构之单链表

    链表:一种数据存储结构.学链表首先要搞懂数组,按朋友的话说,数组和链表的关系就相当于QQ2008和QQ2009. 除非要通过索引频繁访问各个数据,不然大多数情况下都可以用链表代替数组. 链表部分主要要 ...

  7. css--小白入门篇6(终)

    一.相对定位 定位有三种,分别是相对定位.绝对定位.固定定位. 相对定位: 1 position:relative; 绝对定位: 1 position:absolute; 固定定位: 1 positi ...

  8. C++ 迭代器运算符 箭头运算符->

    所有标准库容器都支持迭代器,只有少数几种才支持下标运算 迭代器运算符 运算符 作用 *iter 返回迭代器iter所指元素的引用 iter -> mem 解引用iter,并获取元素名为mem的成 ...

  9. python文件读写及形式转化和CGI的简单应用

    一丶python文件读写学习笔记 open() 将会返回一个 file 对象,基本语法格式如下: open(filename, mode) filename:包含了你要访问的文件名称的字符串值. mo ...

  10. 2.6 访问 Shell 脚本的参数

        所谓的位置参数(positional parameters)指的也就是Shell脚本的命令行参数(command-line arguments).在Shell函数里,它们同时也可以是函数的参数 ...