zoukankan      html  css  js  c++  java
  • [Leetcode] Valid Palindrome

    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.

    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.

     1 class Solution {
     2 public:
     3     bool isPalindrome(string s) {
     4         int count = 0;
     5         for (int i = 0; i < s.length(); ++i) {
     6             if (s[i] >= 'A' && s[i] <= 'Z') {
     7                 s[i-count] = s[i] - 'A' + 'a';
     8             } else if (s[i] >= 'a' && s[i] <= 'z' || s[i] >= '0' && s[i] <= '9') {
     9                 s[i-count] = s[i];
    10             } else {
    11                 count++;
    12             }
    13         }
    14         s.resize(s.length() - count);
    15         int a = 0, b = s.length() - 1;
    16         while (a < b) {
    17             if (s[a] != s[b]) {
    18                 return false;
    19             }
    20             a++; b--;
    21         }
    22         return true;
    23     }
    24 };
  • 相关阅读:
    failed to push some refs to 'git@github.com:laniu/liuna.git'报错原因
    ECMAScript和JavaScript的关系
    js面试总结
    第16章 脚本化css
    代理模式
    SQL
    VS
    Js/Jquery获取iframe中的元素 在Iframe中获取父窗体的元素方法
    SQL
    C#
  • 原文地址:https://www.cnblogs.com/easonliu/p/3630611.html
Copyright © 2011-2022 走看看