Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit series activities – click on the task to see the details of the activities.

The title

You are given a string s, consisting of several words separated by space characters. Returns the length of the last word in the string.

A word is the largest substring that consists only of letters and does not contain any space characters.

Source: LeetCode link: leetcode-cn.com/problems/le… Copyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.

The sample1: Enter: s ="Hello World"Output:5Explanation: The last word is "World"5. The sample2: Enter: s =" fly me to the moon "Output:4Explanation: The last word is "moon" and the length is4. The sample3: Enter: s ="luffy is still joyboy"Output:6Explanation: The last word is of length6"Joyboy". Source: LeetCode//leetcode-cn.com/problems/length-of-last-wordCopyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.Copy the code

Thought analysis

Idea 1

  • First remove the space trim() before and after the character
  • Then start cutting the middle character with a space
  • Get the cut array and get the length
  • Take the length minus 1, take the last string of the array
  • Cut the string to get its length

Idea 2

  • Trim (), trim(), trim(), trim(), trim()

Idea 3

Reverse traversal

  • First, remove the Spaces before and after the characters
  • Because we get the value by subscript, it’s a character length of -1
  • Once you’ve got the length, start removing the space before and after the character,
  • Now let’s start at the end and compare, one counter,
  • The loop starts from behind, if the current value is greater than 0 and the current value is not null, the counter is stacked, and the value is returned if the condition is not met

code

Code 1

let lengthOfLastWord = function(s) {
  /** * first trim trim() * then trim the middle characters with Spaces * take the trim array and get the length * subtract the length by 1 * take the last string in the array * trim the string and get the length ** */ 
  let arr = s.trim().split(' ')
  let lastIndex = arr.length - 1
  return arr[lastIndex].split(' ').length
}
Copy the code

Code 2

let arr = s.trimEnd().split(' ')
Copy the code

Code 3

/ * * * or remove the character Spaces before and after the first * because we are through the subscript value, so is the character length after obtaining length - 1 *, eliminate characters before and after the space length, * now starting from the comparison, a counter, * start from behind cycle, if the current value greater than 0, and the current value is not equal to null, Counter stack, return value * */ if the condition is not met 
  let index = s.length - 1
  console.log(index)
  while (s[index] === ' ') {
    console.log(index)
      index--
  }
  let wordLength = 0
  while (index >= 0&& s[index] ! = =' ') {
      wordLength++
      index--
  }
  return wordLength
Copy the code

The test results

Test result Trim ()

Test results trimEnd()

Test results – Reverse traversal