本帖最后由 Blüe-wǒlf 于 2014-6-5 13:22 编辑
- class Test
- {
- public static void main(String[] args)
- {
- String s="123456789";
- System.out.println(s);
- System.out.println(reverseString(s));
- System.out.println(reverseString(s,3,4));//这句为什么换不了。
- }
- public static String reverseString(String s,int start,int end)
- {
- char[] chr=s.toCharArray();
- reverse(chr,start,end);
- return new String(chr);
- }
- public static String reverseString(String s)
- {
- return reverseString(s,0,s.length());
- }
- public static void reverse(char[] arr,int x,int y)
- {
- for (int start=x,end=y-1;start<end;start++,end--)
- {
- swap(arr,start,end);
- }
- }
- public static void swap(char[] arr,int x,int y)
- {
- char temp=arr[x];
- arr[x]=arr[y];
- arr[y]=temp;
- }
- }
复制代码
|
|