(leetcode)Valid Palindrome

Question

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

Example

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

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

Solution

First solution

First I figure out an complex solution, the core idea in this algorithm is that the index of characters from both sides of the string should add up to a certain figure, which is the size of that string plus one.

class Solution {
public:
    static bool isPalindrome(string s) {
        int i = 0;
        vector<char> v1(0);
        unsigned int sum[62] = { 0 };        //該數(shù)組0~25對(duì)應(yīng)a~z,26~51對(duì)應(yīng)A~Z,52~61對(duì)應(yīng)'0'~'9'
        for (; i<s.length(); i++) {
            if ((s[i] >= 'a'&&s[i] <= 'z') || (s[i] >= 'A'&&s[i] <= 'Z')||(s[i] >= '0'&&s[i] <= '9')) {    //去掉各種cases存入容器中
                v1.push_back(s[i]);
            }
        }
        if ((int)v1.size() % 2 == 1) {
            v1.erase(v1.begin() + (v1.size() - 1) / 2);  //若字符串為奇數(shù),去掉中間那個(gè)
        }
        for (i = 0; i<v1.size(); i++) {                //對(duì)字符串中字符出現(xiàn)的位置索引進(jìn)行累加,累加的和存入數(shù)組
            if(v1[i]>='a'&&v1[i]<='z')
            sum[v1[i] - 'a'] += i + 1;                
            else if(v1[i] >= 'A'&&v1[i] <= 'Z') sum[v1[i] - 'A' + 26] += i + 1;
            else sum[v1[i] - '0' + 52] += i + 1;
        }
        for (i = 0; i<26; i++) {                        //每個(gè)字符(此處大小寫(xiě)合并)的索引的和必然是(容器大小加一)的整數(shù)倍
            if ((sum[i] + sum[i + 26]) % (v1.size() + 1) != 0) return false;
        }
        for (i = 52; i < 62; i++) {
            if (sum[i] % (v1.size() + 1) != 0) return false;
        }
        return true;
    }
};

Second solution

when I look up the handbook, it provide a much simpler solution.
O(n) runtime, O(1) space:
The idea is simple, have two pointers – one at the head while the other one at the tail.
Move them towards each other until they meet while skipping non-alphanumeric characters.
I implement it in C++(I really should have thought of this method =_=)

class Solution {
public:
    static bool isPalindrome(string s) {
        int i=0,j=s.length()-1;
        while(i<j){
            while(i<j&&!isdigit(s[i])&&!isalpha(s[i])) i++;
            while(i<j&&!isdigit(s[j])&&!isalpha(s[j])) j--;
            if(tolower(s[i])!=tolower(s[j])) return false;
            i++;
            j--;
        }
        return true;
    }
};

原文地址:我的主頁(yè)文章

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,775評(píng)論 0 33
  • 運(yùn)行時(shí)是iOS中一個(gè)很重要的概念,iOS運(yùn)行過(guò)程中都會(huì)被轉(zhuǎn)化為runtime的C代碼執(zhí)行。例如[target do...
    蘿卜醬紫閱讀 431評(píng)論 0 3
  • 文/茱莉 9月第一次開(kāi)始嘗試主題閱讀,這月讀完了六本書(shū),都是關(guān)于日式生活方式的圖書(shū)。 1、《日和手帖1:我們終究都...
    茱莉Julia閱讀 347評(píng)論 0 3
  • Vue實(shí)例基礎(chǔ)一 數(shù)據(jù)的雙向綁定 v-model 綁定表單的相應(yīng)事件,和數(shù)據(jù)實(shí)現(xiàn)動(dòng)態(tài)的雙向綁定,需要在Vue實(shí)例中...
    sunny519111閱讀 449評(píng)論 0 0