Topic description

Given a string s , you need to reverse the character order of each word in the string, while still preserving the initial order of spaces and words.

Example 1:

 输入:s = "Let's take LeetCode contest"
输出:"s'teL ekat edoCteeL tsetnoc"

Example 2:

 输入: s = "God Ding"
输出:"doG gniD"
Leetcode original title address: https://leetcode.cn/problems/reverse-words-in-a-string-iii/

Thought analysis

Solution: Convert a string to an array, reverse it and then convert it to a string

This method is readable, that is, first convert the string into an array divided by spaces, and then the map loop converts each item (string) in the array, and then converts it into a small array, inverting it and turning it back. Can. code show as below:

 let s = "abc fgg kks"
var reverseWords = function (s) {
    let arr = s.split(' ') // 此时arr为: ['abc', 'fgg', 'kks']
    arr = arr.map((item) => {
        return item.split('').reverse().join('') // 把每一项字符串转成数组,在反转颠倒,再转回字符串
    }) // map循环加工以后的arr为: ['cba', 'ggf', 'skk']
    return arr.join(' ') // 最终变成:'cba ggf skk'
};

Solution 2 Flashback traverse the header of the heap array and append

Ideas:

For example: let s = "abc fgg kks" , when we traverse in flashback, we will get 'k','k','s',' ','g','g','f',' ','c','b','a' At this time, we find that the reversal is indeed reversed, but the position is also reversed.

So, we have such an idea: append the header of the array to the heap. In this case, we divide the string into three heaps with spaces as the boundary, which are: abc a pile, fgg --pile, kks fgg . When we traverse in reverse, we will get three inverted piles skk , ggf , abc , get a pile and put this pile Append the header unshift to the array, so that the order of each heap remains the same. Then convert it to a string. code show as below:

 let s = "abc fgg kks"
var reverseWords = function (ss) {
    // 1. 因为是以空格为区分,见到空格说明这一堆字符串完毕,才会装填到数组中去,所以这里手动再在原有字符串头部添加一个空格,用于区分一堆一堆的结束
    let s = ' ' + ss
    let arr = [] // 2. 创建一个数组用于装填一堆一堆的字符串
    let tempStr = '' // 3. 由于遍历得到的是一个一个的字符串,所以再创建一个空字符串用于拼接'堆'
    for (let i = s.length - 1; i >= 0; i--) {
        // 4. 倒叙第一个肯定不是空格的,所以看else语句中的代码
        if (s[i] === ' ') { // 5. 当遇到空格的时候,说明这一堆结束啦,一堆结束,那就一堆装填呗
            arr.unshift(tempStr)  // 6. 把这一堆数据头部追加到数组中,这样位置就不会变化咯
            arr.unshift(' ') // 7. 注意这里需要手动再头部追加一个空格,因为原来的字符串也有空格哦,空格不能漏掉哦
            tempStr = '' // 8. 然后把用于拼接堆的字符串重置,以便继续重新拼接堆
        } else {
            tempStr = tempStr + s[i] // 5.1 拼接归拢一堆,注意因为是反转,所以是tempStr + s[i]拼接,不要写反了哦
        }
    }
    console.log('数组', arr); // 9. 打印看看 [' cba', ' ggf', ' skk']
    return arr.join('').trimStart() // 10. 转成字符串以后,别忘了把左侧的空格给去掉哦
};
console.log(  reverseWords(s)  );

Solution 3 Flashback traverse the header of the heaped string and append

This is similar to the above, which is to splicing a bunch of strings using the string header (the original is to use the array header to append, and then convert the string), the code is as follows:

 var reverseWords = function (ss) {
    let s = ' ' + ss // 1. 加空格做区分一堆一堆的字符串
    let resultStr = '' // 2. 结果字符串
    let tempStr = '' // 3. 拼接堆字符串
    for (let i = s.length - 1; i >= 0; i--) {
        if (s[i] === ' ') { // 4. 倒叙第一个肯定不是空格
            resultStr = ' ' + tempStr + resultStr // 6. 把拼接好的一堆字符串加到结果字符串上
            tempStr = '' // 7. 然后把拼接字符串重置为原来的状态
        } else {
            tempStr = tempStr + s[i] // 5. 一堆字符串拼接
        }
    }
    return resultStr.trimStart() // 8. 返回结果的时候,也要注意去除左侧之前手动添加的空格哦
};
Good memory is not as good as bad writing, record it ^_^

水冗水孚
1.1k 声望585 粉丝

每一个不曾起舞的日子,都是对生命的辜负