[LeetCode] 125 - Valid Palindrome
문제
- 링크: https://leetcode.com/problems/valid-palindrome
- 난이도: Easy
- 태그: 투포인터, 문자열
- 결과:
Time: 0 ms (100%), Space: 9.97 MB (53.50%)
풀이
class Solution {
public:
bool isPalindrome(string s) {
int len = s.size();
int l = 0;
int r = len - 1;
while (l <= r)
{
while (l <= r &&!isalnum(s[l]))
{
l++;
}
while (l <= r && !isalnum(s[r]))
{
r--;
}
if (l > r)
{
break;
}
if (tolower(s[l]) != tolower(s[r]))
{
return false;
}
l++;
r--;
}
return true;
}
};
alphanumeric한 글자인지 확인해야하는 거추장스러운 조건이 있지만, 난이도 자체가 여럽지는 않았다. 투 포인터로 해결할 수 있다.
댓글남기기