LeetCode-Left Rotate String

Title description

Problem-solving ideas

  1. First convert the string to a character array.
  2. Define two temporary arrays, one to store the array before the split, and one to store the array after the split.
  3. Traverse the character array in turn, if the index subscript +1 is less than or equal to n, add it to the array before the division, and vice versa.
  4. Use extended operations to merge two arrays.
  5. Use a for of loop to merge all the elements in the array into a string and return.

Implementation code

var reverseLeftWords = function (s, n) {
    
    
    let temp = [];
    for(let v of s) {
    
    
        temp.push(v);
    }
    let temp2 = [];
    let temp3 = [];
    temp.some((value,index) => {
    
    
        if ((index + 1) <= n) {
    
    
            temp2.push(value);
        } else {
    
    
            temp3.push(value);
        }
    });
    const merge = [...temp3,...temp2];
    let str = '';
    for(let v of merge) {
    
    
        str = str + v;
    }
    return str;
};

Guess you like

Origin blog.csdn.net/sinat_41696687/article/details/114728106