Leetcode
2020.04.28 06:21

7. Reverse Integer

조회 수 100 추천 수 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
번호 분류 제목 글쓴이 날짜 조회 수
37 Leetcode 415. Add Strings hooni 2020.04.28 117
» Leetcode 7. Reverse Integer hooni 2020.04.28 100
35 Leetcode 206. Reverse Linked List hooni 2020.04.28 110
34 Leetcode 53. Maximum Subarray hooni 2020.04.28 106
33 Leetcode 581. Shortest Unsorted Continuous Subarray hooni 2020.04.28 107
32 Leetcode 852. Peak Index in a Mountain Array hooni 2020.04.28 101
31 Leetcode 350. Intersection of Two Arrays II hooni 2020.04.28 102
30 Leetcode 20. Valid Parentheses hooni 2020.04.25 98
29 Leetcode 38. Count and Say hooni 2020.04.25 103
28 Leetcode 937. Reorder Data in Log Files hooni 2020.04.25 107
27 Leetcode 443. String Compression hooni 2020.04.25 103
26 Leetcode 437. Path Sum III hooni 2020.04.24 140
25 Leetcode 54. Spiral Matrix hooni 2020.04.20 122
24 Leetcode 380. Insert Delete GetRandom O(1) hooni 2020.04.18 124
23 Leetcode 542. 01 Matrix hooni 2020.04.18 123
22 Leetcode 139. Word Break hooni 2020.04.18 129
21 Leetcode 126. Word Ladder II hooni 2020.04.17 126
20 Leetcode 238. Product of Array Except Self hooni 2020.04.17 125
19 Leetcode 763. Partition Labels hooni 2020.04.17 159
18 Leetcode 121. Best Time to Buy and Sell Stock hooni 2020.04.17 125
Board Pagination Prev 1 2 3 4 Next
/ 4