java算法:
![image.png](https://static.studygolang.com/200520/c59d0107418a7908b470ee3dd58f9e97.png)
public void reverseString(char[] s) {
// 左右双指针
int left = 0;
int right = s.length - 1;
// 交换元素的临时变量 temp
char temp;
while (left < right){
temp = s[left];
s[left++] = s[right];
s[right--] = temp;
}
}
go算法:
![image.png](https://static.studygolang.com/200520/83d68f1b6ae6f793130a2ffbb678f83c.png)
func reverseString(s []byte) {
left := 0
right := len(s) -1
for left < right{
s[left],s[right] = s[right],s[left]
left ++
right --
}
}
楼主跑跑看这个代码
class Solution {
public void reverseString(char[] s) {
// 左右双指针
int left = 0;
int right = s.length - 1;
// 交换元素的临时变量 temp
char temp;
while (left < right){
temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}
}
#3
更多评论
这个图一乐就好。一样的算法,C跑出来效果比go都慢。
![2020-05-20 15-26-25屏幕截图.png](https://static.studygolang.com/200520/dd77a0ec58633663d0c88ac8184f04fb.png)
#1