首页 > JS写leetcode的题目,time limit exceeded

JS写leetcode的题目,time limit exceeded

Write a function that takes a string as input and returns the string reversed.

Example:
Given s = "hello", return "olleh".

Subscribe to see which companies asked this question

var reverseString = function(s) {

s=s.split("");
var arr=[];
for(i=0;i<s.length;i++){
    arr.splice(0,0,s[i]);
    
    
}
arr=arr.join('');
return arr;

};
怎样才能减少计算时间呢?


哪有这么写的……一边循环数组一边更改数组是大忌。

数组原生就有reverse()方法:

var reverseString = function(s) {
    return s.split('').reverse().join('');
}
【热门文章】
【热门文章】