javascript


  • 0
    B

    //330ms 或许你可以改进下
    var len = s.length;
    var arr = [];
    if(num == 1) return s;
    for(var i = 0; i < s.length; i++){
    arr[i] = new Array();
    }

      	 	i = 0,j=0;
      	 	var count = 0;
      	 	   while(count < len){
      	 	      for(var x = 0; x < num && count < len ; x ++){
      	 	      	 arr[x][i] = s[count++];
      	 	      	 if(x == num-1) count--;
      	 	      }
      	 	      for(x = num-1; x>=1 && count < len; x --){
      	 	      	arr[x][i] = s[count++];
      	 	      	i++;
      	 	      }
      	 	   }
      	 	  var y = "";
      	 	  for(i = 0; i < arr.length; i++){
      	 	  	  y+=arr[i].join("");
      	 	  }
      	 	return y.replace(/\s+/g,"");

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.