Leetcode
2020.04.28 06:21

7. Reverse Integer

조회 수 106 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.


class Solution {
    public int reverse(int x) {
        int result = 0;
        
        while (x != 0) {
            int last = x % 10;
            x /= 10;
            
            if(result > Integer.MAX_VALUE/10){
                return 0;
            }
            
            if(result == Integer.MAX_VALUE/10 && last > 7){
                return 0;
            }
            
            if (result < Integer.MIN_VALUE/10){
                return 0;
            }
            
            if(result == Integer.MIN_VALUE/10 && last < -8){
                return 0;
            }
            
            result = result * 10 + last;
        }
        return result;
    }
}


[문제] https://leetcode.com/problems/reverse-integer/



?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
77 Programmers hashmap secret hooni 2020.04.30 0
76 Programmers fib secret hooni 2020.04.30 0
75 Programmers team game secret hooni 2020.04.30 0
74 Leetcode 692. Top K Frequent Words hooni 2020.04.15 93
73 Leetcode 2. Add Two Numbers hooni 2020.04.14 96
72 Leetcode 731. My Calendar II hooni 2020.04.15 100
71 Leetcode 75. Sort Colors hooni 2020.04.14 101
70 Leetcode 347. Top K Frequent Elements hooni 2020.04.14 102
69 Leetcode 430. Flatten a Multilevel Doubly Linked List file hooni 2020.04.06 104
68 Leetcode 23. Merge k Sorted Lists hooni 2020.04.14 104
67 Leetcode 729. My Calendar I hooni 2020.04.15 104
66 Leetcode 253. Meeting Rooms II hooni 2020.04.15 104
65 Leetcode 20. Valid Parentheses hooni 2020.04.25 104
64 Leetcode 62. Unique Paths file hooni 2020.04.09 105
63 Leetcode 852. Peak Index in a Mountain Array hooni 2020.04.28 106
» Leetcode 7. Reverse Integer hooni 2020.04.28 106
61 Leetcode 946. Validate Stack Sequences hooni 2020.04.08 107
60 Leetcode 973. K Closest Points to Origin hooni 2020.04.15 107
59 Leetcode 350. Intersection of Two Arrays II hooni 2020.04.28 108
58 Leetcode 443. String Compression hooni 2020.04.25 109
Board Pagination Prev 1 2 3 4 Next
/ 4