문제
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
int | long |
---|---|
4byte | 8byte |
32bit | 64bit |
if (stack.size() >= 11) {
return 0;
}
/***/
String str = actual.toString();
BigInteger test = new BigInteger(str);
if (test.compareTo(new BigInteger("-2147483648")) < 0 || test.compareTo(new BigInteger("2147483647")) > 0 ) {
return 0;
}
얼굴만 자란다... 🕺🏻 →(어떻게 알지?) → 🙎🏻 → (어떻게 알까?)→ 👩🏻
목표 | % | / |
---|---|---|
3 | 123 % 10 = 3 | 123/10 = 12 |
2 | 12 % 10 = 2 | 12/10 = 1 |
1 | 1 % 10 = 1 | 1/10 = 0 (종료) |
×10
@ParameterizedTest
@MethodSource("providerParam")
void test2(int x, int expect) {
int result = 0;
int digit = x;
while (x != 0) { // 양수,음수 고려
digit = x % 10;
x /= 10;
if (result > Integer.MAX_VALUE/10 || result < Integer.MIN_VALUE/10) {
result = 0;
break;
}
result = result * 10 + digit;
}
assertEquals(expect, result);
}