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;
}
}
```

?

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