Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.02.13 - [LeetCode] 7. Reverse Integer #14

Closed
suhyunsim opened this issue Feb 14, 2021 · 0 comments
Closed

21.02.13 - [LeetCode] 7. Reverse Integer #14

suhyunsim opened this issue Feb 14, 2021 · 0 comments
Assignees
Labels
easy LeetCode, Codility - easy 수학 수학 실패 시도했지만 맞지 못한 문제

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Feb 14, 2021

문제

핵심 아이디어

  • 단순히 입력받은 숫자를 역으로 출력하는 문제
  • 몇 가지 예외 처리를 놓치면 안됨
    • 음수 처리
    • 9자리일 때 x가 0이면 10자리 수가 아님
    • 9자리일 때 x가 0이 아니면 10자리 수

어려운 점, 실수

  • 처음에 reverse 로직을 간단하게 완성했는데 테스트 케이스 몇 가지를 계속 통과하지 못했다.
    -> 9자리일 때를 고려하지 못함

풀이

package main.java.com.poogle.LeetCode.Q7;

public class Solution {
    public int reverse(int x) {
        if (x > Integer.MAX_VALUE || x < Integer.MIN_VALUE)
            return 0;

        int result = 0;
        int cnt = 0;

        while (x != 0) {
            cnt++;
            result = result * 10 + x % 10;
            x /= 10;

            if (cnt == 9 && x != 0) {
                int cmp = Integer.MAX_VALUE / 10;
                if (result > 0) {
                    if ((result - cmp) > 0) {
                        result = 0;
                        break;
                    }
                } else {
                    if ((result + cmp) < 0) {
                        result = 0;
                        break;
                    }
                }
            }
        }
        return result;
    }
}
@suhyunsim suhyunsim self-assigned this Feb 14, 2021
@suhyunsim suhyunsim added 수학 수학 실패 시도했지만 맞지 못한 문제 labels Feb 14, 2021
@suhyunsim suhyunsim added the easy LeetCode, Codility - easy label Feb 14, 2021
@suhyunsim suhyunsim added this to the 2월 2주 차 milestone Feb 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy LeetCode, Codility - easy 수학 수학 실패 시도했지만 맞지 못한 문제
Projects
None yet
Development

No branches or pull requests

1 participant