Reverse Integer

其中 1000的结果应为 1! java

1. 采用常规方式,如123->321 其过程为((3)*10+2)*10+1 code

private int reve(int x) {
		// TODO Auto-generated method stub
		
		int result = 0 ;
		int temp = x;
		while(temp > 0){
			result = result * 10 + temp % 10;
			temp /= 10;
		}
		return result;
	}



2. 采用字符串方式,后转为数字

private int reve(String x){
		char[] chs = x.toCharArray();
		
		int begin = 0;
		int end = chs.length - 1;
		while(begin < end){
		    
		    if(chs[begin] != chs[end]){
		       char temp = chs[begin];
			    chs[begin] = chs[end];
		    	chs[end] = temp; 
		    }
			
			++begin;
			--end;
		}
		
		int result = Integer.parseInt(new String(chs));
		return result;
		
	}
相关文章
相关标签/搜索