反转字符串
解法一:数组遍历
public class LeetCode_344 {
public static void reverseString(char[] s) {
for (int i = 0; i < s.length / 2; i++) {
// 第i项和第s.length-1-i项进行交换
char temp = s[i];
s[i] = s[s.length - 1 - i];
s[s.length - 1 - i] = temp;
}
}
public static void main(String[] args) {
char[] s = new char[]{'h', 'e', 'l', 'l', 'o'};
System.out.println("-----反转之前-----");
for (char c : s) {
System.out.print(c + " ");
}
System.out.println();
reverseString(s);
System.out.println("-----反转之后-----");
for (char c : s) {
System.out.print(c + " ");
}
}
}