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.

解题思路:

建立两个index,同时从前从后遍历字符串。遇到非字符需要跳过,遇到大写字母将其转为小写字母。比较两个index指向的字母,如果一致则继续遍历下一组,如果不相同则返回false,最后返回true;

注意:

1、非字母不需要比较,要跳过;

2、注意统一大小写;

 class Solution {
public:
bool isPalindrome(string s) {
int len = s.length();
int front = ;
int behind = len - ; if (!len)
return true; while (front <= behind) { if (!isAlphanumeric(&s[front])) {
front++;
continue;
} if (!isAlphanumeric(&s[behind])) {
behind--;
continue;
} if (s[front] != s[behind]) {
return false;
} else {
front ++;
behind --;
} } return true;
} bool isAlphanumeric(char *s) {
if ((*s>='a' && *s<='z') || (*s>='' && *s<=''))
return true;
if (*s>='A' && *s<='Z') {
*s += ;
return true;
}
return false;
} };

另,有些程序直接使用了C++中isalnum()和tolower()函数,思路是一样的:

 class Solution {
public:
bool isPalindrome(string s) {
int len = s.length();
int front = ;
int behind = len - ; if (!len)
return true; while (front <= behind) {
if (!isalnum(s[front])) {
front++;
continue;
} if (!isalnum(s[behind])) {
behind--;
continue;
} if (tolower(s[front]) != tolower(s[behind])) {
return false;
} else {
front ++;
behind --;
}
} return true;
}
};

附录:

常用字符ASCII值

【Leetcode】【Easy】Valid Palindrome的更多相关文章

  1. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  2. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. 【leetcode刷题笔记】Valid Palindrome

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

  5. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  6. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  7. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  8. 【LeetCode题意分析&解答】36. Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  9. 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  10. 【LeetCode每天一题】Valid Parentheses(有效的括弧)

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

随机推荐

  1. Luogu P1273 有线电视网 树形DP

    又重构了一下...当然当初的题一看就看懂了QAQ 设f[i][j]表示以i为根的子树,有j个客户的最大收益 方程:f[u][j+k]=max(f[u][j+k],f[u][j]+f[v][k]-w(u ...

  2. Qt随笔 - QSettings

    QSettings类提供了持久的跨平台应用程序设置. 嗯,一句话概括QSettings-- 创建 来看一下原型: QSettings::QSettings(const QString &org ...

  3. node.js知识点提取

    javascript是脚本语言,脚本语言都需要一个解析器才能运行.

  4. drf序列化器的实例

    应用目录结构: views.py from django.shortcuts import render # Create your views here. from django.views imp ...

  5. kvm 虚拟网络命令操作

    2018-11-06 ```使用brctl命令创建网桥br1```# brctl addbr br1``` 删除网桥br1```# brctl delbr br1``` 将eth0端口加入网桥br1  ...

  6. Linux acpi off学习的必要

    ACPI是Intel(i386,x86_64,IA64)平台的标准固件规范,绝大部分OS需要从BIOS得到的信息都可以从ACPI得到,并且现在的趋势是未来的任何新的特性相关的信息都只能从ACPI得到. ...

  7. 如何写一个简单的webserver(一):最简实现

    本文主要讲述如何用C/C++在Linux环境下写一个简单的支持并发的web服务器,并不考虑服务器的健壮性.安全性.性能等一系列因素. 在本文中,该服务器仅支持GET请求. 项目地址:https://g ...

  8. 安装Newton版Glance

    Image Service 本文介绍在controller节点上安装.配置Image服务 glance,镜像存储在本地文件系统 安装准备 controller 节点 ip:192.168.81.11 ...

  9. Docker_network相关指令

    docker network create创建docker网卡docker network create -d overlay --ip-range=192.168.2.0/24 --gateway= ...

  10. 牛客网Java刷题知识点之equals和hashcode()

    不多说,直接上干货! 福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号:   大数据躺过的坑      Java从入门到架构师      人工智能躺过的坑          ...