編寫一個函數,其作用是將輸入的字符串反轉過來。輸入字符串以字符數組 s 的形式給出。
不要給另外的數組分配額外的空間,你必須原地修改輸入數組、使用 O(1) 的額外空間解決這一問題。
考察:字符串+雙指針文章來源:http://www.zghlxwxcb.cn/news/detail-642460.html
解答:文章來源地址http://www.zghlxwxcb.cn/news/detail-642460.html
class Solution {
public void reverseString(char[] s) {
int left = 0;
int right = s.length-1;
while(left < right){
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}
}
到了這里,關于【leetcode】344. 反轉字符串(easy)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!