๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/Leetcode

[Leetcode,c++] Palindrome Number

by dkswnkk 2021. 11. 9.

๋ฌธ์ œ

 

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

 

์ฝ”๋“œ

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0) return false;

        string s = to_string(x);

        int start = 0, end = s.length()-1;

        while(start<=end){
            if(s[start]!=s[end]) return false;
            start++;
            end--;
        }
        return true;
    }
};

 

ํ’€์ด

์ˆซ์ž๋ฅผ ๋’ค์ง‘์–ด๋„ ๊ฐ™์€์ง€ ํ™•์ธํ•˜๋Š” ํŒฐ๋ฆฐ๋“œ๋กญ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ˆ˜๊ฐ€ ์Œ์ˆ˜ ์ผ ๊ฒฝ์šฐ ๋’ค์ง‘์—ˆ์„ ๋•Œ ์ ˆ๋Œ€ ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์Œ์ˆ˜๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ๋ฌด์กฐ๊ฑด false๋ฅผ return ํ•ด์คฌ๊ณ ,  ์ž…๋ ฅ์ด ๋“ค์–ด์˜ค๋Š” ์ˆ˜์˜ ๋ฒ”์œ„๊ฐ€ 32๋น„ํŠธ == 4๋ฐ”์ดํŠธ์ธ Int๋ฒ”์œ„์ด๊ธฐ์— stoi ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ string์œผ๋กœ ๋ณ€ํ™˜์‹œํ‚จ ๋’ค ๋ฌธ์ž์—ด์˜ ์•ž๊ณผ ๋’ค๋ฅผ ๋™์‹œ์— ๊ฒ€์‚ฌํ•˜์—ฌ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

๋Œ“๊ธ€