Title description
Problem-solving ideas
- First convert the string to a character array.
- Define two temporary arrays, one to store the array before the split, and one to store the array after the split.
- 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.
- Use extended operations to merge two arrays.
- 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;
};