7 questions, integer reversal

7 questions, integer reversal

topic

Gives a 32-bit signed integer, you need this integer number on each inverted.

Example 1:
Input: 123
Output: 321

Example 2:
Input: -123
Output: -321

Example 3:
Input: 120
Output: 21

Note:
Suppose we have an environment can store the 32-bit signed integer, its value is in the range [-2 31 is 2 31 is - 1] Please According to this hypothesis, if integer overflow after reverse it returns 0.

Thinking

In addition to taking more than 10 get the value of the last one, the new number is added to the end (before the need to take the new number 10), in addition to the original number of 10 minus one, and so forth, until the original number of full circle again.

Note judgment of data overflow, because the calculation of the time, if the final value of the overflow, is not being given, will still get data, but the data is wrong, judgment should determine before calculating the overflow, which is the first judgment the next calculation will overflow if not, then re-calculation, an overflow return 0.

About personal view overflow

When the computer is in accordance with the calculated complement in the calculated value is calculated according to the following complement overflow through the overflow after rounding value.
The other is the value of the machine may be understood to mean a change -2 31 is next is two 31 is -1, reciprocating cycle.
The final step is the initialization value, if a value is spilled will complain, but it does not overflow error occurred during the calculation must be processed manually.

Code

public class T007 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println( reverse( 102 ) );


	}
	//我的最优解
	public static int reverse(int x) {

		int output = x%10;
		int next = x/10;
		
		//当剩余的数为0结束循环
		while( next != 0 ) {
			
			//判断下一次计算的值是否会溢出,即下一次计算后除最高位外的值大于等于147483647且最高位大于等于2(正向溢出)
			//或者下一次计算后除最高位外的值小于等于-147483648且最高位大于等于2(负向溢出)
			if( ( (output%100000000 * 10 + next%10) >= 147483647 && output/100000000 >= 2) || 
					( (output%100000000 * 10 + next%10) <= -147483648 && output/100000000 <= -2) )
				    return 0;
			
			//计算
			output = output*10+next%10;
			next = next/10;
		}
		
		return output;
	}
	//另一种看似简单,实际上复杂的一匹的算法
	//验证我直接拿题8的用了
    public static int reverse1(int x) {
    	
    	String input = (x+"");
    	String output = "";
    	
    	for( int i = input.length()-1; i >= 0; i-- ) {
    		if( input.charAt(i) == '-' ) {
    			output = input.charAt(i) + output;
    		}else if( input.charAt(i) == '+' ) {
    			;
    		}else {
    			output += input.charAt(i);
    		}
    	}

    	//判断是否越界了
    	//当字符串长度大于11的时候无论是正还是负,均已经越界,等于11的时候正数越界,负数(因为有负号,实际数字长度为10)再进行下一步判断
    	if( output.length() >= 11 ) {
    		//判断是正还是负,正就直接返回越界的信息,负再进行下一步判断
    		if( output.charAt(0) == '-' ) {
    			//判断字符串长度是否等于11是就进行下一步判断,否则返回越界信息
    			if( output.length() == 11 ) {
    				//当第一位数字大于2的时候,必定越界,等于2再判断,等于1不越界
        			if( output.charAt(1) > 50 ) {
        				return 0;
        			}else if( output.charAt(1) == 50 ) {
        				//等于2的时候再对接下来的几位进行判断,这个时候可以去掉一位可以直接转换为整型数判断大小
        				//大于147483648越界,否则不越界
        				if( Integer.parseInt( output.substring(2)) > 147483648 )
        					return 0;
        			}
    			}else
    				return 0;
    		}else
    			return 0;
    	//长度为10的时候负数必定不越界,正数进行判断,方法同上
    	}else if( output.length() == 10 ) {
    		if( output.charAt(0) != '-' ) {
    			if( output.charAt(0) > 50 ) {
    				return 0;
    			}else if( output.charAt(0) == 50 ) {
    				if( Integer.parseInt( output.substring(1)) >= 147483647 )
    					return 0;
    			}
    		}
    	}
    	
		return Integer.parseInt(output); 
    }

}
Published 25 original articles · won praise 0 · Views 124

Guess you like

Origin blog.csdn.net/weixin_45980031/article/details/103465949