My Solution (Flags)
1 | class Solution { |
Remarks:
Check the last digit of MAX value of
intboundary:result == Integer.MAX_VALUE / 10 && digit > (sign == 1 ? 7 : 8))Use
longto store the intermediateresult, and check if the result is out of bound at last:1
2
3if (result > Integer.MAX_VALUE) return Integer.MAX_VALUE;
if (result < Integer.MIN_VALUE) return Integer.MIN_VALUE;
return (int) result;
While Loops
1 | public class Solution { |
Remarks:
result * 10 + digit > Integer.MAX_VALUE->result > (Integer.MAX_VALUE - digit) / 10Use this to check out of bound