当前位置:网站首页>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 .
边栏推荐
- LeetCode 1184. 公交站间的距离 ---vector顺逆时针
- Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
- 科研漫画 | 联系到被试后还需要做什么?
- MP3是如何诞生的?
- numpy笔记
- Why do domestic mobile phone users choose iPhone when changing a mobile phone?
- Redis的4种缓存模式分享
- Width and alignment
- 近一亿美元失窃,Horizon跨链桥被攻击事件分析
- 5g TV cannot become a competitive advantage, and video resources become the last weapon of China's Radio and television
猜你喜欢

C1 certification learning notes 3 -- Web Foundation

Five minutes of machine learning every day: why do we need to normalize the characteristics of numerical types?

LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04

Guitar Pro 8win10 latest guitar learning / score / creation

Helix Swarm中文包发布,Perforce进一步提升中国用户体验

Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture

Ffprobe common commands

MP3是如何诞生的?
![[local differential privacy and random response code implementation] differential privacy code implementation series (13)](/img/fe/f6a13dcf31ac67633ee5a59d95149d.jpg)
[local differential privacy and random response code implementation] differential privacy code implementation series (13)

Quick introduction to automatic control principle + understanding
随机推荐
MySQL组合索引(多列索引)使用与优化案例详解
Redis的4种缓存模式分享
Redis 发布和订阅
Luo Gu - some interesting questions 2
They are all talking about Devops. Do you really understand it?
中国主要城市人均存款出炉,你达标了吗?
小数,指数
Introduction to modern control theory + understanding
怎么判断外盘期货平台正规,资金安全?
Halcon knowledge: NCC_ Model template matching
Temperature control system based on max31865
夜天之书 #53 Apache 开源社群的“石头汤”
Width accuracy
Deep learning network regularization
华为云数据库DDS产品深度赋能
暑期复习,一定要避免踩这些坑!
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
hexadecimal
干货 | fMRI标准报告指南新鲜出炉啦,快来涨知识吧
Five minutes per day machine learning: use gradient descent to complete the fitting of multi feature linear regression model