Math
def reverse(self, x: int) -> int:
limit = pow(2, 31) - 1
neg = x < 0
res = 0
x = abs(x)
while x > 0:
if res > limit / 10 and int(x % 10) != 0:
return 0
res = res * 10 + int(x % 10)
x = x // 10
if neg:
return res * -1
return resint[] arr = { 1,4,5,9,10,40,50,90,100,400, 500, 900, 1000};
String[] symbol = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
public String intToRoman(int num) {
StringBuilder sb = new StringBuilder();
for (int i = arr.length - 1; i >= 0; i--) {
while (num >= arr[i]) {
sb.append(symbol[i]);
num -= arr[i];
}
}
return sb.toString();
}1131. Maximum of Absolute Value Expression


Last updated