博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
58. Length of Last Word
阅读量:4553 次
发布时间:2019-06-08

本文共 646 字,大约阅读时间需要 2 分钟。

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) {        int r = s.size() - 1;        while (s[r] == ' ') r --;        r ++;        int len = r;        while (r > 0) {            if (s[-- r] == ' ') return len - r - 1;          }        return len - r;    }};

 

转载于:https://www.cnblogs.com/pk28/p/7210421.html

你可能感兴趣的文章
CSS定位深入理解 完全掌握CSS定位 相对定位和绝对定位
查看>>
网络体系结构
查看>>
练习4.13、4.14、4.15、4.16
查看>>
SAP库龄表
查看>>
PhantomJS 基础及示例 (转)
查看>>
20175316盛茂淞 2018-2019-2 《Java程序设计》第3周学习总结
查看>>
zookeeper安装
查看>>
js清空页面控件值
查看>>
Appium使用Python运行appium测试的实例
查看>>
django request bug
查看>>
二叉树_非递归先中后序_递归非递归求深度
查看>>
20181227 新的目标
查看>>
HDFS写流程
查看>>
生产环境服务器环境搭建+ 项目发布
查看>>
js按条件分类json数组,并合计同组数据(一维转换为二维)
查看>>
Exp6 信息搜集与漏洞扫描
查看>>
redis4安装
查看>>
随机生成双色球号码
查看>>
使用命令wsimport构建WebService客户端[转]
查看>>
第八遍:链接详解
查看>>