【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 ...
随机推荐
- 119th LeetCode Weekly Contest Subarray Sums Divisible by K
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...
- 关于XML的验证(DTD与XSD)一点实践
[转自] http://blog.chinaunix.net/uid-276853-id-366491.html 关于XML的验证一点实践 1)此方法是在XML文档中绑定对应的DTD文件来进行的 // ...
- javascript格式化时间(几秒钟前,几分钟前,几小时前,几天前...)
beautify_time: function(timestamp) { var mistiming = Math.round(new Date() / 1000) - timestamp; var ...
- JS中String()与.toString()的区别
1..toString()可以将所有的的数据都转换为字符串,但是要排除null 和 undefined 例如将false转为字符串类型 <script> var str = false.t ...
- UI设计师与VI设计师的区别
企业视觉形象(CorporateVisualImage)与企业视觉形象识别(VI)并不是一个概念.前者是企业与生俱来的客观存在要素,也就是说一个企业无论是否制定了它的VI,也无论其所制定的VI是否成功 ...
- Open Closed Principle(OCP)开闭原则
面向对象的最基本原则 Software entites like classes,modules and functions should be open for extension but cloa ...
- querySelectorAll与childNodes
NodeList 对象是一个节点的集合,是由 Node.childNodes 和 document.querySelectorAll 返回的. html代码: <ul id="pare ...
- Java开发环境搭建——IntelliJ Idea开发环境
IntelliJ Idea版本选择由于公司使用JDK7,所以我选择安装Version 2016.1.4(手动安装试验出来的,最新版的2016.1.4启动时提示需要安装JDK8)下载 前面说明有误,其实 ...
- IDEA 导入cordova3.5工程目录注意事项
IDEA 导入cordova3.5工程目录注意事项 1 eclipse很不稳定,有很多小问题.平时我自己用idea,但是当用cordova3.5创建好工程目录是,用eclipse导入时没有问题的.但是 ...
- 设置ibus为默认输入法
/etc/profile 文件中编辑 export INPUT_METHOD="ibus"