In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to solve the problem of the length of the last word in leetcode, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
Topic link
Https://leetcode-cn.com/problems/length-of-last-word/
Topic description
Given a string that contains only uppercase and lowercase letters and spaces'', returns the length of its last word.
If the last word does not exist, return 0.
Description: a word is a string that consists of letters but does not contain any spaces.
Example:
Enter: "Hello World"
Output: 5
The idea of solving the problem
Tags: string traversal
Traversing forward from the end of the string, there are two main cases
In the first case, take the string "Hello World" as an example, traversing back and forward until it traverses to the end or encounters a space, which is the length of the last word "World" 5.
In the second case, take the string "Hello World" as an example, you need to filter out the space at the end, and then do the operation in the first case, that is, the last word is considered as "World" with a length of 5.
So the whole process is to filter out the spaces from behind to find the tail of the word, and then iterate forward from the tail to find the head of the word, and then subtract the two, that is, the length of the word.
Time complexity: O (n), n is the ending space and the overall length of the ending word
Code
Java version
Class Solution {
Public int lengthOfLastWord (String s) {
Int end = s.length ()-1
While (end > = 0 & & s.charAt (end) = ='') end--
If (end
< 0) return 0; int start = end; while(start >= 0 & & s.charAt (start)! ='') start--
Return end-start
}
}
JavaScript version
/ * *
* @ param {string} s
* @ return {number}
, /
Var lengthOfLastWord = function (s) {
Let end = s.length-1
While (end > = 0 & & s [end] = =') end--
If (end
< 0) return 0; let start = end; while(start >= 0 & s [start]! ='') start--
Return end-start
}
Drawing and interpretation
The above is all the content of the article "how to solve the length of the last word in leetcode". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.