题目

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.

分析

题目要求得出所给字符串最后一个单词的长度。

这其实是一个很简单的题目,主要有几个需要注意的点:

  • 空字符串,自然是返回0
  • 只有一个单词的字符串,返回长度即可
  • 若是输入字符串为“hello “也就是说,最后是多个空字符时,返回的长度要求是最后非空字符组成的最后一个单词而不是0;

AC代码

class Solution {
public:
int lengthOfLastWord(string s) {
int len = strlen(s.c_str()); //如果是空字符串或者是单字符,则直接返回长度
if (len == 0)
return len; int i = len-1 , j = 0;
//从后向前找到非空字符
while (i>=0 && s[i] == ' ')
--i; for (j = i; j>=0 && s[j] != ' '; --j)
; return i - j;
}
};

GitHub测试程序源码

LeetCode(59)Length of Last Word的更多相关文章

  1. Leetcode(59)-Count Primes

    题目: Description: Count the number of prime numbers less than a non-negative number, n. 思路: 题意:求小于给定非 ...

  2. LeetCode(59):螺旋矩阵 II

    Medium! 题目描述: 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, ...

  3. LeetCode(59)SPiral Matrix II

    题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...

  4. Leetcode(5)最长回文子串

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...

  5. Qt 学习之路 2(59):使用流处理 XML

    Qt 学习之路 2(59):使用流处理 XML 豆子 2013年7月25日 Qt 学习之路 2 18条评论 本章开始我们将了解到如何使用 Qt 处理 XML 格式的文档. XML(eXtensible ...

  6. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  7. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  8. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  9. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

随机推荐

  1. hdu3938 Portal 离线+并查集

    #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int ...

  2. iOS [CIContext initWithOptions:]: unrecognized selector sent to instance 模拟器 iOS 8.4

    在模拟器(iPhone 4s,iOS 8.4)中运行应用时, 应用crash在了使用CIContext(options:nil) 这个API的一个纯Swift第三方库. StackOverFlow的解 ...

  3. Jquery | 基础 | 属性过滤选择器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. inline-block元素水平居中问题

    今天做项目的时候碰到了不固定元素个数,需要水平居中的问题,原来的确定宽度下margin:0 auto等方法木有用了.想起来之前看过display:inline-block的文章, 果断用这个. 之前很 ...

  5. python之url编码

    import urllib.parsempp='besttest 自动化测试'print(urllib.parse.quote_plus(mpp)) #url编码print(urllib.parse. ...

  6. [已读]基于MVC的Javascript Web 富应用开发

    这本书是12年出版,我买的时间应该是13年,书架上唯一一本盗版→ → 但是看完是在今年. 因为刚拿到的时候,读起来很是磕磕绊绊,就搁置了蛮久.到第二次拿起来的时候,发现已经有部分内容过时,但我还是觉得 ...

  7. B. Connecting Universities DFS,无向树

    http://codeforces.com/problemset/problem/700/B 题意是,在一颗树中,有k个大学,要求两两匹配,他们之间的距离作为贡献,使得距离总和最大. 一开始的时候无从 ...

  8. win应用只允许单个实例运行,并将已运行实例窗口置顶

    关键词:windows,c++,桌面应用,单个实例,窗口置顶 目标:1.判断本程序是否已有一个实例在运行.2.若有,则激活已在运行的实例(将其窗口置顶),并退出当前运行. 1.使用semaphore来 ...

  9. SQLServer · 最佳实践 · SQL Server 2012 使用OFFSET分页遇到的问题

    1. 背景 最近有一个客户遇到一个奇怪的问题,以前使用ROW_NUMBER来分页结果是正确的,但是替换为SQL SERVER 2012的OFFSET...FETCH NEXT来分页出现了问题,因此,这 ...

  10. phpstorm 格式化代码

    MAC 安装phpcs.phpcbf composer global require 'squizlabs/php_codesniffer=*' Changed current directory t ...