
[Easy] 58. Length of Last Word
发布日期:2021-05-07 18:21:07
浏览次数:9
分类:原创文章
本文共 713 字,大约阅读时间需要 2 分钟。
58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ’ ', return the length of last word (last word means the last appearing word if we loop from left to right) in the string.
If the last word does not exist, return 0.
Note: A word is defined as a maximal substring consisting of non-space characters only.
Example:
Input: "Hello World"Output: 5
Solution
Brute Force
4 ms 6.5 MB
class Solution { public: int lengthOfLastWord(string s) { int len = 0, tail = s.length() - 1; while (tail >= 0 && s[tail] == ' ') tail--; while (tail >= 0 && s[tail] != ' ') { len++; tail--; } return len; }};
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年04月11日 03时44分59秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Tomcat启动过程连接部分-(下)
2019-03-04
JVM篇-结合源码分析垃圾收集器的类型
2019-03-04
STM32F407单片机ADC采样定时器触发事件分布
2019-03-04
LWIP协议socket通信设置发送接收超时等待时间
2019-03-04
RT -Thread Studio开发环境下使用W5500做TCP客户端
2019-03-04
Warning: The core is locked up的解决办法
2019-03-04
奔涌吧 后浪!!! 哔哩哔哩 何冰
2019-03-04
【JVM系列】JDK 内置工具
2019-03-04
JAVA 基础与进阶系列索引 -- JDK 源码学习系列 -- 并发
2019-03-04
网络编程系列索引 -- Linux 网络编程索引
2019-03-04
网络编程系列索引 -- JAVA 网络编程系列
2019-03-04
【JDK源码分析系列】ArrayBlockingQueue源码分析
2019-03-04
【网络通信 -- 直播】音视频常见封装格式 -- MEPG2 TS
2019-03-04
【网络通信 -- 直播】音视频常见封装格式 -- FLV
2019-03-04
【C/C++基础进阶系列】C/C++ 对象模型 -- 类基础知识总结(三)
2019-03-04
【C/C++基础进阶系列】C/C++ 对象模型 -- 对象语义
2019-03-04
基于FPGA的HDMI信号采样原理
2019-03-04
Spring 与使用STOMP消息
2019-03-04
AngularJS ng-class、ng-style
2019-03-04