344. 反转字符串
作者:
Andysys | 来源:发表于
2019-12-30 22:26 被阅读0次 //迭代
public void reverseString(char[] s) {
if (s == null || s.length < 2) {
return;
}
int left = -1;
int right = s.length;
char temp;
while (++left < --right) {
temp = s[left];
s[left] = s[right];
s[right] = temp;
}
}
// 尾递归
public void reverseString2(char[] s) {
helper(s, 0, s.length - 1);
}
public void helper(char[] s, int start, int end) {
if (start >= end) {
return;
}
char temp = s[start];
s[start] = s[end];
s[end] = temp;
helper(s, start + 1, end - 1);
}
本文标题:344. 反转字符串
本文链接:https://www.haomeiwen.com/subject/rtieoctx.html
网友评论