Created
February 26, 2019 00:34
-
-
Save qiaoxu123/2307222cc07b063ce2e1fa3236b2cc45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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