코딩테스트/Leetcode

Leetcode - [Easy]9. Palindrome Number

aiemag 2021. 3. 4. 23:21
반응형

leetcode.com/problems/palindrome-number/

 

Palindrome Number - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
bool isPalindrome(int x) {
 
    if (x < 0return false;
 
    int c = 1;
    int t = x;
 
    while (1) {
        t /= 10;
        if (t == 0break;
        c *= 10;
    }
 
    int a, b;
    while (x) {
        a = x / c;
        b = x % 10;
        if (a != b) return false;
 
 
        x = (x - a * c - b)/10;
        c /= 100;
 
        if (c == 1return true;
    }
 
    return true;
}
cs
 

 

 

반응형