【Leetcode】【Easy】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.
解题思路:
建立两个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的更多相关文章
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【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 ...
- 【leetcode刷题笔记】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【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 ...
- 【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 ...
- 【LeetCode题意分析&解答】36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【LeetCode每天一题】Valid Parentheses(有效的括弧)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- RPC 定义 和 原理
一.RPC 1. RPC是什么 RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议. ...
- 2019.03.26 读书笔记 关于 invoke与begininvoke
invoke与begininvoke是同步委托和异步委托,但是有两种使用情况: 1. control中的invoke.begininvoke. 2. delegrate中的invoke.beginin ...
- GreenPlum 大数据平台--运维(一)
.最后分析或真空或创建表或等... Select * from pg_stat_operations where schemaname='SCHEMA NAME ' and actionname in ...
- Java基础26-对象初始化过程
/* 1.因为new Test1()用到了Test1类,所以会把它从硬盘上加载进入内存 2.如果有static静态代码块就会随着类的加载而执行,还有静态成员和普通方法也会随着类的加载而被加载 3.在堆 ...
- 遍历方式 && 数组方法 && 算法
遍历方式 一般,我们常用for in遍历对象,使用for (var i = 0; i < len; i++) {}的方式来遍历数组,这是最常用的两种方式,但是优缺点呢? 1.for (var i ...
- Rabbitmq~对Vhost的配置
rabbitmq里有一些概念我们要清楚,如vhost,channel,exchange,queue等,而前段时间在部署rabbitmq环境时启用了虚拟主机vhost,感觉他主要是起到了消息隔离的作用, ...
- MsysGit下GUI乱码问题解决
在Windows下安装Git-preview-1.7.4后,使用中发现许多的乱码问题,感觉甚是不便.这是因为Git是在linux下开发的管理软件,而linux的编码方式是基于UTF-8的,所以移植到W ...
- sqlserver 限制用户只能访问指定的视图
项目中有一个需求,要求给其它单位提供数据,我们用到了视图,并要求不能让他们看到数据库中的其它数据,我们为其创建了单独的账号,并只能看到指定视图 一.创建视图 CREATE VIEW [dbo].[v_ ...
- jquery的$.getScript在IE下的缓存问题
jquery的$.getScript在IE下的缓存问题
- 安装 VS 2015 Update 2 + Windows SDK Tools 1.3.1 + Windows SDK 10586.212 后提示找不到 10586.0 SDK 问题的解决方法
将 Visual Studio 2015 升级到 Update 2,并安装 Windows SDK Tools 1.3.1 和 Windows SDK 10586.212 后,有可能造成原本已安装的 ...