344. Reverse String

Total Accepted: 71392
Total Submissions: 188974
Difficulty: Easy

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

Example:

Given s = “hello”, return “olleh”.

js字符串是引用类型,要转为值类型来处理。

JS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* @param {string} s
* @return {string}
*/
var reverseString = function(s) {
var arr = s.split('');
var len = arr.length;
for(var i = 0;i < len / 2; i++){
var temp = arr[i];
arr[i] = arr[len - 1 - i];
arr[len - i - 1] = temp;
}
return arr.join('');
};

打个小广告

欢迎加入我的小专栏「基你太美」一起学习。