当前位置:网站首页>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 .
边栏推荐
猜你喜欢
Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
函数计算异步任务能力介绍 - 任务触发去重
MySQL组合索引(多列索引)使用与优化案例详解
深度学习 神经网络案例(手写数字识别)
Preliminary exploration of flask: WSGI
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
【大连理工大学】考研初试复试资料分享
5g TV cannot become a competitive advantage, and video resources become the last weapon of China's Radio and television
UFO:微软学者提出视觉语言表征学习的统一Transformer,在多个多模态任务上达到SOTA性能!...
随机推荐
干货 | fMRI标准报告指南新鲜出炉啦,快来涨知识吧
函数计算异步任务能力介绍 - 任务触发去重
LeetCode 58. 最后一个单词的长度
unity update 协程_Unity 协程的原理
对话龙智高级咨询顾问、Atlassian认证专家叶燕秀:Atlassian产品进入后Server时代,中国用户应当何去何从?
Helix Swarm中文包发布,Perforce进一步提升中国用户体验
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
【学习笔记】拟阵
压力、焦虑还是抑郁? 正确诊断再治疗
Helix swarm Chinese package is released, and perforce further improves the user experience in China
Introduction to asynchronous task capability of function calculation - task trigger de duplication
Weibo and Huya advance into interest communities: different paths for peers
Ffprobe common commands
中国主要城市人均存款出炉,你达标了吗?
宽度与对齐
Deep learning network regularization
十六进制
hexadecimal
怎么判断外盘期货平台正规,资金安全?
Redis publish and subscribe