【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 ...
随机推荐
- 2018南京icpc现场赛心得
第一次参加icpc的比赛,也是第一块奖牌,虽然只是铜,但其实打的已经很好了,稍微差一点就可以摸银了. 之前参加省赛,成为那次比赛我校唯一一个没拿奖的队伍,其实还是一直都有一些心结的,而这段时间和新的队 ...
- webpack 打包css报错 Error: Chunk.entrypoints: Use Chunks.groupsIterable and filter by instanceof Entrypoint instead
是webpack4和extract-text-webpack-plugin的兼容性问题 执行命令:npm install extract-text-webpack-plugin@next --save ...
- HDU - 3038 带权并查集
这道题我拖了有8个月... 今天放假拉出来研究一下带权的正确性,还有半开半闭的处理还有ab指向的一系列细节问题 #include<iostream> #include<algorit ...
- 采用MQTT协议实现android消息推送(2)MQTT服务端与客户端软件对比、android客户端示列表
1.服务端软件对比 https://github.com/mqtt/mqtt.github.io/wiki/servers 名称(点名进官网) 特性 简介 收费 支持的客户端语言 IBM MQ 完整的 ...
- SQL手工注入学习 一
sql注入: (基于DVWA环境的sql注入) 流程: 1.判断是否有SQL注入漏洞 2.判断操作系统.数据库和web应用的类型 3.获取数据库信息看,包括管理员信息(拖库) ...
- twitter storm学习 - 安装部署问题汇总
已经碰到的或者将来碰到的关于安装部署方面的问题以及解决方法,先挖个坑 1.提交的topology在admin界面上看emitted始终都是0,查看日志发现有如下错误: worker [ERROR] E ...
- bootstrap的datepicker使用(1.将默认的英文设置为中文2.选择日月年的时候记录之前的操作)
参考网页 bootstrap datepicker 属性设置 以及方法和事件 1.如何将bootstrap的datepicker默认的英文设置为中文 第一步,新建一个js文件(bootstrap ...
- lua-遍历集合-ipairs和pairs的区别
--ipairs和pairs的区别arr = {1,3,[5]=5,name="kaikai",age=12, 89}--arr[4]= 23--ipairs--ipairs仅仅遍 ...
- JS代码格式化排版工具,web文本编辑器
js格式化代码工具:http://www.cnblogs.com/blodfox777/archive/2008/10/09/1307462.html web文本编辑器 :http://www.div ...
- CF 303C——Minimum Modular——————【剪枝】
Minimum Modular time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...