当前位置:网站首页>58. 最后一个单词的长度
58. 最后一个单词的长度
2022-07-30 12:42:00 【拽拽就是我】
leetcode力扣刷题打卡
题目:58. 最后一个单词的长度
描述:给你一个字符串 s,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中 最后一个 单词的长度。
单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。
解题思路
1、从头遍历比较麻烦,直接倒着遍历。
原代码##
class Solution {
public:
int lengthOfLastWord(string s) {
int ans = 0;
for (int i = s.size() - 1; i >= 0; --i) {
if (s[i] == ' ') continue;
while (i >= 0 && s[i] != ' ') {
ans++;
--i;
}
break;
}
return ans;
}
};
边栏推荐
- 【23考研】408代码题参考模板——链表
- 自从外包干了四年,基本废了...
- no matching host key type found. Their offer: ssh-rsa
- Raja Koduri澄清Arc GPU跳票传闻 AXG年底前新推四条产品线
- 域名抢注“卷”到了表情包?ENS逆势上涨的新推力
- Markdown 3 - 流程图表
- Current and voltage acquisition module DAM-6160
- Tutorial on using the one-key upgrade function of the RTSP/Onvif video platform EasyNVR service
- 第42讲:Scala中泛型类、泛型函数、泛型在Spark中的广泛应用
- 干货分享:小技巧大用处之Bean管理类工厂多种实现方式
猜你喜欢
解码Redis最易被忽视的CPU和内存占用高问题
for循环的3个表达式执行顺序
Raja Koduri澄清Arc GPU跳票传闻 AXG年底前新推四条产品线
腾讯称电竞人才缺口200万;华为鸿蒙3.0正式发布;乐视推行每周工作4天半?...丨黑马头条...
【微信小程序】一文带你搞懂小程序的页面配置和网络数据请求
js 构造函数 return 非空对象,其实例化的对象在原型上的差异
私有化部署的即时通讯平台,为企业移动业务安全保驾护航
【Kaggle:UW-Madison GI Tract Image Segmentation】肠胃分割比赛:赛后复盘+数据再理解
DeFi 巨头进军 NFT 领域 用户怎么看?
Add the device library after Vivado installation
随机推荐
无人艇轨迹跟踪的预设性能抗扰控制研究
Mysql batch insert transaction unique key repeated processing
RTSP/Onvif协议视频平台EasyNVR服务一键升级功能的使用教程
dolphinscheduler单机化改造
EasyNVS cloud management platform function reconstruction: support for adding users, modifying information, etc.
Another blast!Ali's popular MySQL advanced collection is open source, reaching P7
展厅全息投影所具备的三大应用特点
datax enables hana support and dolphinscheduler enables datax tasks
[PostgreSQL] - explain SQL分析介绍
Tutorial on using the one-key upgrade function of the RTSP/Onvif video platform EasyNVR service
监控界的最强王者,没有之一!
[BJDCTF2020]Cookie is so stable-1|SSTI injection
自从外包干了四年,基本废了...
漫谈金丝雀部署(Canary Deployment)
Hand tearing read-write lock performance test
MySQL【多表查询】
腰部外骨骼机器人线性自抗扰控制器参数优化
no matching host key type found. Their offer: ssh-rsa
666666
Smart pointer implementation conjecture