【力扣】344. 反轉(zhuǎn)字符串
編寫一個函數(shù),其作用是將輸入的字符串反轉(zhuǎn)過來。輸入字符串以字符數(shù)組 s 的形式給出。不要給另外的數(shù)組分配額外的空間,你必須原地修改輸入數(shù)組、使用 O(1) 的額外空間解決這一問題。
示例 1:
輸入:s = [“h”,“e”,“l(fā)”,“l(fā)”,“o”]
輸出:[“o”,“l(fā)”,“l(fā)”,“e”,“h”]
示例 2:
輸入:s = [“H”,“a”,“n”,“n”,“a”,“h”]
輸出:[“h”,“a”,“n”,“n”,“a”,“H”]文章來源:http://www.zghlxwxcb.cn/news/detail-637096.html
提示:
1 <= s.length <=
1
0
5
10^5
105
s[i] 都是 ASCII 碼表中的可打印字符文章來源地址http://www.zghlxwxcb.cn/news/detail-637096.html
題解
class Solution {
public void reverseString(char[] s) {
int left = 0;
for (int right = s.length - 1; left <= right; left++, right--) {
char tmp = s[left];
s[left] = s[right];
s[right] = tmp;
}
}
}
到了這里,關于【力扣】344. 反轉(zhuǎn)字符串 <首尾指針>的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!