반응형

[LeetCode, 리트코드] Palindrome Number

반응형


1. Problem


Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.


입력된 정수가 팰린드롬인지 판명하라. 앞뒤로 읽어도 똑같이 읽힐 때 팰린드롬이라 한다.


Example 1:

Input: 121
Output: true

Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.


2. Solution


기본적인 팰린드롬 문제




3. What I solved

class Solution {
public:
    bool isPalindrome(int x) {
        if(x < 0) 
            return false;
        string x_s = to_string(x);
        string reverse_s = x_s;
        
        reverse(reverse_s.begin(), reverse_s.end());
        
        return x_s == reverse_s ? true : false;
    }
};


반응형

이 글을 공유하기

댓글

Designed by JB FACTORY