Likou brush question day6 (replace spaces + left-rotate characters)

replace spaces

topic

insert image description here

the code

var replaceSpace = function(s) {
    
    
     return s.replace(/ /g, "%20");
};
var replaceSpace = function(s) {
    
    
    return s.split(' ').join('%20');
};

left rotate string

topic

insert image description here

train of thought

  • First repeat the string once, splicing at the end
  • calculaten = k % len
  • Then nintercept from the double string, lenjust intercept

the code

var reverseLeftWords = function(s, n) {
    
    
    let len=s.length;
    let num=n%len;
    let str=`${
      
      s}${
      
      s}`
    return str.slice(num,num+len)
};

Supongo que te gusta

Origin blog.csdn.net/weixin_51610980/article/details/128401228
Recomendado
Clasificación