문제 풀이
제출한 코드
class Solution {
public int reverse(int x) {
int[] nums = new int[1000];
int index = 0;
boolean isNegativeNumber = false;
if (x >= Integer.MAX_VALUE || x <= Integer.MIN_VALUE) {
return 0;
}
if (x < 0) {
isNegativeNumber = true;
x = -x;
} else if (x == 0) {
return 0;
}
while(x != 0) {
int y = x % 10;
nums[index++] = y;
x = x / 10;
}
int result = 0;
for (int i = 0; i < index; i ++) {
if (!isNegativeNumber) {
if (result + nums[i] * Math.pow(10, index-i-1) > Integer.MAX_VALUE) {
return 0;
}
result += nums[i] * Math.pow(10, index-i-1);
} else {
if (result - nums[i] * Math.pow(10, index-i-1) < Integer.MIN_VALUE) {
return 0;
}
result -= nums[i] * Math.pow(10, index-i-1);
}
}
return result;
}
}
잘한점
개선할 점
- pow라는 함수를 외우지 못했었음
- Max / Min value에 대한 예외 처리를 제대로 하지 못했음