1. 具體題目數組
編寫一個函數,其做用是將輸入的字符串反轉過來。輸入字符串以字符數組 char[] 的形式給出。不要給另外的數組分配額外的空間,你必須原地修改輸入數組、使用 O(1) 的額外空間解決這一問題。你能夠假設數組中的全部字符都是 ASCII 碼錶中的可打印字符。函數
示例 1:輸入:["h","e","l","l","o"] 輸出:["o","l","l","e","h"]spa
示例 2:輸入:["H","a","n","n","a","h"] 輸出:["h","a","n","n","a","H"]指針
2. 思路分析code
雙指針,首尾交換blog
3. 代碼字符串
1 public void reverseString(char[] s) { 2 int left = 0; 3 int right = s.length - 1; 4 while(left < right){ 5 char temp = s[left]; 6 s[left] = s[right]; 7 s[right] = temp; 8 left++; 9 right--; 10 } 11 }