/**
* 編寫一個函數,其作用是將輸入的字符串反轉過來。輸入字符串以字符數組 char[] 的形式給出。
* <p>
* 不要給另外的數組分配額外的空間,你必須原地修改輸入數組、使用 O(1) 的額外空間解決這一問題。
* <p>
* 你可以假設數組中的所有字符都是 ASCII 碼錶中的可打印字符。
* <p>
* 來源:力扣(LeetCode)
* 鏈接:https://leetcode-cn.com/problems/reverse-string
* 著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。
*/
public class Solution {
public void reverseString(char[] s) {
if (s == null) {
return;
}
int length = s.length;
int midLength = s.length / 2;
for (int i = 0; i < midLength; i++) {
char char1 = s[i];
char char2 = s[length - i - 1];
s[i] = char2;
s[length - i - 1] = char1;
}
}
public static void main(String args[]) {
char[] test = "helloworld".toCharArray();
Solution solution = new Solution();
solution.reverseString(test);
System.out.println(test);
}
}