添加 StringReverse.java
This commit is contained in:
parent
72141441cb
commit
10de7e7ec1
17
StringReverse.java
Normal file
17
StringReverse.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
public class StringReverse {
|
||||||
|
// 字符串反转 数组双指针解法
|
||||||
|
// String无法直接操作内部的char数组,避免不了2倍内存开销
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String str = "1234567";
|
||||||
|
char[] chars = str.toCharArray();
|
||||||
|
int len = chars.length;
|
||||||
|
char x, y;
|
||||||
|
for (int index = 0, mid = len >> 1, end = len - 1; index < mid; index++, end--) {
|
||||||
|
x = chars[index];
|
||||||
|
y = chars[end];
|
||||||
|
chars[index] = y;
|
||||||
|
chars[end] = x;
|
||||||
|
}
|
||||||
|
System.out.println(new String(chars));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user