当前位置:网站首页>LeetCode 58. Length of the last word
LeetCode 58. Length of the last word
2022-07-04 15:13:00 【TianChao lobster】
LeetCode 58. Length of last word
Topic link : https://leetcode.cn/problems/length-of-last-word/
Give you a string s, It consists of several words , Words are separated by some space characters . Return string the last one The length of the word .
word It's just letters 、 The largest substring that does not contain any space characters .
Example 1:
Input :s = “Hello World”
Output :5
explain : The last word is “World”, The length is 5.
Problem solving :
class Solution {
public:
int lengthOfLastWord(string s) {
if(s.empty()) return 0;
const int size = s.size();
int count{
};
for (size_t i=size; i>0; --i){
// Minus one means to remove the last character of the string .
if(s[i-1] == ' '){
if(count > 0) break;
count = 0;
}else{
count ++;
}
}
return count;
}
};
Topic notes :
Reverse traversal , Cumulative length . But it should be noted that there is a hidden character at the end of the string , Therefore need -1. The essence '\0’ Is the end of string flag , Not included in string length , But take up memory space . Here and in a binary picture , Finding a row of consecutive pixels is similar . That is, traverse a row of pixels , How many consecutive pixels are satisfied , Returns the start pixel , End pixel , There are corresponding lines .
边栏推荐
- 基于MAX31865的温度控制系统
- Implementation of macro instruction of first-order RC low-pass filter in signal processing (easy touch screen)
- Openresty current limiting
- 暑期复习,一定要避免踩这些坑!
- 近一亿美元失窃,Horizon跨链桥被攻击事件分析
- .Net之延迟队列
- go-zero微服务实战系列(九、极致优化秒杀性能)
- EventBridge 在 SaaS 企业集成领域的探索与实践
- 每周招聘|高级DBA年薪49+,机会越多,成功越近!
- Introduction to asynchronous task capability of function calculation - task trigger de duplication
猜你喜欢
Deep learning neural network case (handwritten digit recognition)
LeetCode 1200 最小绝对差[排序] HERODING的LeetCode之路
UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...
压力、焦虑还是抑郁? 正确诊断再治疗
Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding
Leecode learning notes - Joseph problem
TechSmith Camtasia studio 2022.0.2 screen recording software
干货 | fMRI标准报告指南新鲜出炉啦,快来涨知识吧
.Net 应用考虑x64生成
Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
随机推荐
hexadecimal
Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
selenium 浏览器(2)
数据库函数的用法「建议收藏」
LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04
Align left and right!
Width accuracy
Openresty current limiting
Deep learning network regularization
What are the concepts of union, intersection, difference and complement?
信号处理之一阶RC低通滤波器宏指令实现(繁易触摸屏)
lnx 高效搜索引擎、FastDeploy 推理部署工具箱、AI前沿论文 | ShowMeAI资讯日报 #07.04
宽度与对齐
Guitar Pro 8win10 latest guitar learning / score / creation
UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...
[local differential privacy and random response code implementation] differential privacy code implementation series (13)
Is BigDecimal safe to calculate the amount? Look at these five pits~~
LeetCode 58. 最后一个单词的长度
Implementation of web chat room
selenium 元素交互