PS/LeetCode
[LeetCode] 9. Palindrome Number
판교토끼
2020. 4. 12. 16:24
728x90
https://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
팰린드롬 숫자, 앞뒤가 같은 숫자를 판별하는 문제이다. BOJ에서도 비슷한 문제를 풀었었다.
문제에서 음수는 팰린드롬으로 취급받지 않으므로 걸러낸 후에, string으로 변환하여 대칭을 비교하였다.
이번엔 C++이 아닌 C#으로 풀었다.
public class Solution {
public bool IsPalindrome(int x) {
if(x<0)
return false;
else if(x==0)
return true;
else {
var source = x.ToString();
if(source.Length==1)
return true;
for(int i=0;i<source.Length/2;i++) {
if(source[i]!=source[source.Length-1-i])
return false;
}
return true;
}
}
}
단순하게 풀었다. 하지만 시간과 공간이 상당히 하위권이었다.
728x90