[Likou] 58. Is the reverse traversal of the length of the last word the optimal solution?

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

The 30th article on the 22nd day of the spring recruit punch card.

Diligent study is like a seedling that springs up, it does not increase, but it grows every day; dropping out of school is like a stone for sharpening a knife, it does not see its loss, but it loses every day.

There are so many activities for the Nuggets. This month, I decided to use go to brush questions every day, on the one hand to improve the algorithm level, on the other hand to precipitate the learning of the go language.

Let's GO!

Topic description

You are given a string s consisting of several words separated by some space characters before and after them. Returns the length of the last word in the string.

A word is the largest substring consisting of only letters and not containing any space characters.

Example

Example 1:

Input: s = "Hello World"

output: 5

Explanation: The last word is "World" and has a length of 5.

Example 2:

输入:s = " fly me to the moon "

output: 4

Explanation: The last word is "moon" and has a length of 4.

Example 3:

Input: s = "luffy is still joyboy"

Output: 6

Explanation: The last word is "joyboy" of length 6.  

hint:

1 <= s.length <= 1 0 4 10^{4}

s only consists of English letters and spaces ' '

there is at least one word in s

topic analysis

  1. After reading the title description, the first reaction is to split the string into arrays based on spaces
  2. Getting the length of the last word in the array is the length we need
  3. But the above method is not the optimal solution. Since we want to find the length of the last word, we can use 反向遍历the idea

Explanation of ideas

  1. According to the meaning of the question, we can know that there is at least one word in the string, so there must be letters in the string.
  2. First find the last letter in the string, which is the last letter of the last word.
  3. Continue traversing the string in reverse, starting at the last letter, until a space is encountered or the beginning of the string is reached.
  4. Each letter traversed is a letter in the last word, so the number of letters traversed is the length of the last word.

AC code

func lengthOfLastWord(s string) (ans int) {
    //倒序遍历 获得最大索引位置
    index := len(s) - 1
    for s[index] == ' ' {
        index--
    }
    //倒序索引未跑完并且索引对应的值不是空字符串时
    for index >= 0 && s[index] != ' ' {
        ans++
        index--
    }
    return
}
复制代码

operation result

image.png

Summarize

Complexity Analysis:

Time Complexity: O(n), where n is the length of the string.

Space complexity: O(1).

Source description

Source: LeetCode

Link: leetcode-cn.com/problems/le…

The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.

finally

Thank you for reading, and welcome everyone: like, favorite,coin(focus on)! ! !

8e95dac1fd0b2b1ff51c08757667c47a.gif

Guess you like

Origin juejin.im/post/7079210376222474271