【Easy】125. Valid Palindrome

    xiaoxiao2022-06-24  20

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

    For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome.

    中心思想:

    从两端开始数。由于无视大小写和其它符号,先把干净的小写字符串整理出来。 注意容易错的地方: alphanumeric是指字母和数字。还有,转换成全小写字符串tmp之后,无论是字符串长度还是提取char都在tmp上处理

    class Solution { public: bool isPalindrome(string s) { <span style="white-space:pre"> </span> string tmp = ""; for (int i = 0; i < n; i++){ if ((s[i] >= 'a' && s[i] <= 'z') || s[i] >= '0' && s[i] <= '9' ){ tmp += s[i]; } else if (s[i] >= 'A' && s[i] <= 'Z'){ tmp += (s[i] + 32); } } if (tmp.size() == 0 || tmp.size() == 1) return true; int left = 0; int right = tmp.size()-1; while (left < right){ if (tmp[left] != tmp[right]) return false; left++; right--; } return true; } };

    转载请注明原文地址: https://ju.6miu.com/read-1123621.html

    最新回复(0)