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. [ CodeForces 1063 A ] Oh Those Palindromes

    \(\\\) \(Description\) 给出 \(N\) 个小写字母,将他们排成一个字符串,使得这个字符串里包含的回文串最多. \(N\le 10^5\) \(\\\) \(Solution\) ...

  2. 2559. [NOIP2016]组合数问题

    [题目描述] [输入格式] 从文件中读入数据. 第一行有两个整数t, k,其中t代表该测试点总共有多少组测试数据,k的意义见[问题描述]. 接下来t行每行两个整数n, m,其中n, m的意义见[问题描 ...

  3. js技巧(三)

    1.检测浏览器,search的用法 if(window.navigator.userAgent.search(/firefox/i)!=-1){ alert('ff'); } else if(wind ...

  4. 对比hive和mysql 复杂逻辑流处理

      1.Mysql中可用存储过程和函数来实现复杂逻辑处理,两者的对比如下:存储过程作为可执行文件,编译一次放在数据库中,函数又返回值.可设定使用权限. 存储过程中可使用游标,声明变量.用call调用. ...

  5. interface与抽象类

    抽象类: 定义:在 class 前加了 abstract 关键字且存在抽象方法(在类方法 function 关键字前加了 abstract 关键字)的类 抽象类不能被实例化. 抽象类被继承之后,子类必 ...

  6. (转)淘淘商城系列——商品搜索功能Service实现

    http://blog.csdn.net/column/details/15737.html 首先我们在taotao-search-interface工程中新建一个SearchService接口,并在 ...

  7. CAD在一个点构造选择集(网页版)

    主要用到函数说明: IMxDrawSelectionSet::SelectAtPoint 在一个点构造选择集.详细说明如下: 参数 说明 [in] IMxDrawPoint* point 点坐标 [i ...

  8. 40条常见的移动端Web页面问题解决方案

    1.安卓浏览器看背景图片,有些设备会模糊.想让图片在手机里显示更为清晰,必须使用2x的背景图来代替img标签(一般情况都是用2倍).例如一个div的宽高是100100,背景图必须得200200,然后b ...

  9. C/C++格式化输入,输出

    C/C++格式化输入,输出 1.C语言 1. 语言函数 scanf(); printf(); sscanf() --> 不安全 sscanf_s() ---> 安全 sprintf() - ...

  10. apacheAB测试指标

    在进行性能测试过程中有几个指标比较重要: 1.吞吐率(Requests per second) 服务器并发处理能力的量化描述,单位是reqs/s,指的是在某个并发用户数下单位时间内处理的请求数.某个并 ...