一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

(二)解题

题目大意:判断一个字符串是不是有效的回文字符串。忽略里面除了数字和字母的其他字符。

解题思路:两个指针i和j,i从前往后,j从后往前,碰到p[i]和p[j]都是字母或者数字就比较大小,如果相等就i++,j–反之则返回false

直到i>=j时,说明是有效回文,返回true。

  1. //isalnum()是判断该字符是不是字母或数字
  2. class Solution {
  3. public:
  4. bool isPalindrome(string s) {
  5. int len = s.length();
  6. int i = 0;
  7. int j = len-1;
  8. while(i<=j)
  9. {
  10. while(!isalnum(s[i])&&i<=j) i++;//直到s[i]为字母或数字为止
  11. while(!isalnum(s[j])&&i<=j) j--;//直到s[j]为字母或数字为止
  12. if(i<=j&&tolower(s[i])!=tolower(s[j])) return false;//如果不等就返回false
  13. i++;j--;//反之就继续比较
  14. }
  15. return true;
  16. }
  17. };

【一天一道LeetCode】#125. Valid Palindrome的更多相关文章

  1. [LeetCode] 125. Valid Palindrome 验证回文字符串

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

  2. leetcode 125. Valid Palindrome ----- java

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

  3. LeetCode 125. Valid Palindrome

    这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...

  4. Java [Leetcode 125]Valid Palindrome

    题目描述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  5. [leetcode]125. Valid Palindrome判断回文串

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

  6. LeetCode 125 Valid Palindrome(有效回文)(*)

    翻译 给定一个字符串.确定它是否是回文的,仅仅考虑当中的数字和字符并忽略其它. 比如. "A man, a plan, a canal: Panama" 是回文的. "r ...

  7. Leetcode 125 Valid Palindrome 字符串处理

    题意:判断字符串是否是回文字符串 先将所有的字母和数字字符保留,并将大写字母转化成小写字母,然后将字符串倒置,比较前后两个字符串是否相同. 该题最好的解法可以模仿 Leetcode 345 Rever ...

  8. Java for LeetCode 125 Valid Palindrome

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

  9. [LeetCode] 680. Valid Palindrome II 验证回文字符串 II

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  10. 125. Valid Palindrome【easy】

    125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...

随机推荐

  1. 电子凭证 : Java 生成 Pdf

    来源:蛙牛, my.oschina.net/lujianing/blog/894365 如有好文章投稿,请点击 → 这里了解详情 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中 ...

  2. CI表单验证

    =================Form辅助函数==================== 1.在视图文件中导入:$this->load->helper("form") ...

  3. PHP Misc. 函数

    PHP 杂项函数简介 我们把不属于其他类别的函数归纳到杂项函数类别. 安装 杂项函数是 PHP 核心的组成部分.无需安装即可使用这些函数. Runtime 配置 杂项函数的行为受 php.ini 文件 ...

  4. Nginx之(二)Nginx安装

    首先从官网上http://nginx.org/下载最新的stable version源码,当前最新版本为nginx-1.10.2.tar.gz. 2.1 configure 解压之后,会发现里面有一个 ...

  5. Antlr v4入门教程和实例

    1 重逢ANTLR 最早知道ANTLR是当年学习Apache Derby数据库源码时,在看到SQL解析那一层时,第一次看到编译原理在实际项目中的应用,惊叹之余也只能望而却步.之前也根据网上一些资料尝试 ...

  6. Dynamics CRM2016 Web Api之根据时间查询数据

    我的博文里已经有多次提到CRM中的时间处理问题了,本篇继续探讨在web api的场景下时间字段如何处理,本篇只涉及查询,针对2016中新增的时间行为"用户当地时间"和"无 ...

  7. Zookeeper的安装配置及基本开发

    一.简介 Zookeeper 是分布式服务框架,主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务.状态同步服务.集群管理.分布式应用配置项的管理等等. ZooKeeper的目标就 ...

  8. SQL Join各种内联外联说明

    Visual Representation of SQL Joins C.L. Moffatt, 3 Feb 2009 CPOL    4.96 (406 votes) Rate this: This ...

  9. 从1....n中随机输出m个不重复的数

    void knuth(int n, int m) { srand((unsigned) time( NULL)); for (int i = 0; i < n && m; i++ ...

  10. Core Python Programming一书中关于深浅拷贝的错误

    该书关于深浅拷贝的论述: 6.20. *Copying Python Objects and Shallow and Deep Copies "when shallow copies are ...