yanil3500
12/1/2017 - 5:56 PM

Reverse an Integer

Reverse an Integer

class ReverseInteger {

	public static int reverseInteger(int num){
		long reversedNum = 0L;
		while(num != 0){
			//Calculate the remainder
			reversedNum = (num % 10) + (reversedNum * 10);
			//Recalculate num to remove digit from tens place
			num = num / 10;
			if (doesOverflow(reversedNum) || doesUnderflow(reversedNum)) {
				return 0;
			}
		}
		return (int)reversedNum;
	}

	public static boolean doesUnderflow(long num){
		return num < -2_147_483_648;
	}

	public static boolean doesOverflow(long num){
		return num > 2_147_483_647;
	}
}