标签:style blog color io os java ar strong for
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
1 public class Solution { 2 public int reverse(int x) { 3 String str=x+""; 4 boolean isNeg=false; 5 if(str.charAt(0)==‘-‘) 6 { 7 str=str.substring(1); 8 isNeg=true; 9 } 10 11 //System.out.println(str); 12 char temp=0; 13 char[] arr=new char[str.length()]; 14 15 for(int i=0;i<str.length();i++) 16 { 17 arr[i]=str.charAt(i); 18 } 19 //System.out.println(Arrays.toString(arr)); 20 for(int i=0,j=arr.length-1;i<arr.length;i++,j--) 21 { 22 if(i<j) 23 { 24 temp=arr[i]; 25 arr[i]=arr[j]; 26 arr[j]=temp; 27 } 28 } 29 //System.out.println(Arrays.toString(arr)); 30 31 //System.out.println(Integer.parseInt(String.valueOf(arr))); 32 return isNeg==false?Integer.parseInt(String.valueOf(arr)):-Integer.parseInt(String.valueOf(arr)); 33 } 34 }
解法2充分说明了我是个多么蹩脚的程序员啊。。。哭。。。
1 public int reverse(int x) { 2 int res=0; 3 while(x!=0) 4 { 5 res=res*10+x%10; 6 x/=10; 7 } 8 return res; 9 }
标签:style blog color io os java ar strong for
原文地址:http://www.cnblogs.com/sweetculiji/p/3987729.html