[Leetcode] 9. Palindrome Number
[Leetcode] 9. Palindrome Number

[Leetcode] 9. Palindrome Number

์–ธ์–ด
Python
C
๋‚œ์ด๋„
๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ
๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ ํ˜•
์ž‘์„ฑ์ž
๋ฐ•์šฉ์„ฑ๋ฐ•์šฉ์„ฑ
์ƒ์„ฑ ์ผ์‹œ
2024๋…„ 09์›” 02์ผ
floatFirstTOC: right

๐Ÿ–ฅ๏ธย ์‹œ์ž‘ํ•˜๋ฉฐ

์‰ฌ์šด ๋ฌธ์ œ๋‹ค.
Example 2:
๋ฌธ์ œ ์กฐ๊ฑด์— ์ด์™€ ๊ฐ™์ด ๋ถ€ํ˜ธ๋„ ํ•œ๊บผ๋ฒˆ์— ํšŒ๋ฌธ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋‹ˆ ์šฐ์„  ์Œ์ˆ˜๋Š” ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜์ง€ ์•Š์€ ๊ฒƒ์œผ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค. ๊ทธ ํ›„ ์ˆซ์ž๋ฅผ ๋’ค์ง‘์–ด์ฃผ๋ฉด ๋œ๋‹ค.
ย 

โš™๏ธย Python

class Solution: def isPalindrome(self, x: int) -> bool: if x < 0: return False else: x = str(x) return x == x[::-1] if __name__ == "__main__": sol = Solution() print(sol.isPalindrome(10)) # True
ย 

โš™๏ธย C++

#include <cctype> #include <iostream> #include <limits> #include <string> class Solution { public: bool isPalindrome(int x) { if (x < 0) { return false; } int reversed = 0; int original = x; while (x != 0) { int digit = x % 10; x /= 10; if (reversed > (std::numeric_limits<int>::max() - digit) / 10) { return false; } reversed = reversed * 10 + digit; } return original == reversed; } }; int main() { Solution sol; std::cout << sol.isPalindrome(-42) << std::endl; // -42 return 0; }
ย 

๐Ÿ“Œย ์†Œ๊ฐ

ย 

๋Œ“๊ธ€

guest