【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 ...
随机推荐
- 洛谷 P3233 [HNOI2014]世界树(虚树+dp)
题面 luogu 题解 数据范围已经告诉我们是虚树了,考虑如何在虚树上面\(dp\) 以下摘自hzwer博客: 构建虚树以后两遍dp处理出虚树上每个点最近的议事处 然后枚举虚树上每一条边,考虑其对两端 ...
- python time,random,os,sys,序列化模块
一.time模块 表示时间的三种方式 在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串: (1)时间戳(timestamp) :通常来说,时间戳 ...
- PostgreSQL精简命令:
dos命令行连接PostgreSQL: . 接入PostgreSQL数据库: psql -h IP地址 -p 端口 -U 用户名 -d 数据库名 . 输入数据库密码 C:\Users\admin\De ...
- vue自定义指令clickoutside使用以及扩展用法
vue自定义指令clickoutside使用以及扩展用法 产品使用vue+element作为前端框架.在功能开发过程中,难免遇到使用element的组件没办法满足特殊的业务需要,需要对其进行定制,例如 ...
- python 获取子目录下的所有文件的路径
import os pathss=[] for root, dirs, files in os.walk(tarpath): path = [os.path.join(root, name) for ...
- 关于跨域登录中获取COOKIES解析BUG
FormsAuthentication.Decrypt 报错 Length of the data to decrypt is invalid. 关于同域名不同服务器之间的登录,加密配置说明 ...
- 数据库~大叔通过脚本生成poco实体
今天在做开发时,需要把表映射成实体,又没有EF这种工具,就从网上下了一个工具,但使用时觉得太重了,所以就自己写了一个,基于mysql的. 功能:输入表名,得到这个表的poco实体 SELECT COL ...
- console命令详解:(转载学习)
Console命令详解,让调试js代码变得更简单 Firebug是网页开发的利器,能够极大地提升工作效率. 但是,它不太容易上手.我曾经翻译过一篇<Firebug入门指南>,介绍了一些 ...
- 修改OPENSUSE 桌面快速搜索快捷键
- mongodb连接失败原因排查
安装了mongodb,添加了管理员root和test数据库的用户rex,并且开启的用户认证. 按照说明文档连接mongodb数据库:$mongo = new Mongo("mongodb:/ ...