zoukankan      html  css  js  c++  java
  • LeetCode125 Valid Palindrome

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

    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         if (s.size() == 0) {
     5             return true;
     6         }
     7         for (int i = 0; i < s.size(); ++i) {
     8             if (s[i] >= 'A' && s[i] <= 'Z') {
     9                 s[i] += ('a' - 'A');
    10             }
    11         }
    12         int i = 0, j = s.size() - 1;
    13         while (i <= j) {
    14             if ( !( (s[i] >= 'a' && s[i] <= 'z') || (s[i] >= '0' && s[i] <= '9')) ) {
    15                 i++;
    16                 continue;
    17             }
    18             if (!( (s[j] >= 'a' && s[j] <= 'z') || (s[j] >= '0' && s[j] <= '9') )) {
    19                 j--;
    20                 continue;
    21             }
    22             if (s[i] != s[j] ) {
    23                 return false;
    24             }
    25             i++;
    26             j--;
    27         }
    28         return true;
    29     }
    30 };
  • 相关阅读:
    MFC深入浅出读书笔记第一部分
    给对话框添加背景
    相对路径与绝对路径之比较
    线程同步的四种方式以及串口通信
    DirectShow简单入门程序
    两个对话框之间的通信
    websphere部署war包
    Struts2的基础知识
    iBatis基础知识
    Struts1的基础知识
  • 原文地址:https://www.cnblogs.com/wangxiaobao/p/6139306.html
Copyright © 2011-2022 走看看