Skip to content

Instantly share code, notes, and snippets.

@qiaoxu123
Created February 26, 2019 00:34
Show Gist options
  • Save qiaoxu123/2307222cc07b063ce2e1fa3236b2cc45 to your computer and use it in GitHub Desktop.
Save qiaoxu123/2307222cc07b063ce2e1fa3236b2cc45 to your computer and use it in GitHub Desktop.
//Runtime: 8 ms, faster than 99.85%
//Memory Usage: 9.1 MB, less than 77.05%
class Solution {
public:
bool isPalindrome(string s) {
for (int i = 0, j = s.size() - 1; i < j; i++, j--) { // Move 2 pointers from each end until they collide
while (isalnum(s[i]) == false && i < j) i++; // Increment left pointer if not alphanumeric
while (isalnum(s[j]) == false && i < j) j--; // Decrement right pointer if no alphanumeric
if (toupper(s[i]) != toupper(s[j])) return false; // Exit and return error if not match
}
return true;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment