๋ฌธ์
์ฝ๋
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์ผ๋ก ๋ณํ์ํจ ๋ค ๋ฌธ์์ด์ ์๊ณผ ๋ค๋ฅผ ๋์์ ๊ฒ์ฌํ์ฌ ํ์์ต๋๋ค.
'Algorithm ๐ง๐ปโ๐ป > Leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Leetcode,c++] Climbing Stairs (0) | 2021.11.09 |
---|---|
[Leetcode,c++] Maximum Subarray (0) | 2021.11.09 |
[Leetcode,c++] Valid Parentheses (0) | 2021.11.09 |
[Leetcode,c++] Search Insert Position (0) | 2021.11.09 |
[Leetcode,c++] Find the City With the Smallest Number of Neighbors at a Threshold Distance (0) | 2021.11.07 |
[Leetcode,c++] Combination Sum (0) | 2021.11.07 |
๋๊ธ