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 --
}
}
有疑问加站长微信联系(非本文作者)