[勇者闯LeetCode] 125. Valid Palindrome
来源:互联网 发布:信鸽分类信息软件 编辑:程序博客网 时间:2024/05/14 14:13
[勇者闯LeetCode] 125. Valid Palindrome
Description
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.
Information
- Tags: Tow Pointers | String
- Difficulty: Easy
Solution
使用两个指针分别从字符串的首尾开始扫描,若两个指针所指向的字母不相等则返回False,否则分别寻找下一个字母进行比较,当两个指针相遇时返回True。
Python Code
class Solution(object): def isPalindrome(self, s): """ :type s: str :rtype: bool """ left, right = 0, len(s)-1 while left < right: while left < right and not s[left].isalnum(): left += 1 while left < right and not s[right].isalnum(): right -= 1 if s[left].lower() != s[right].lower(): return False left += 1 right -= 1 return True
0 0
- [勇者闯LeetCode] 125. Valid Palindrome
- [勇者闯LeetCode] 9. Palindrome Number
- [勇者闯LeetCode] 20. Valid Parentheses
- [LeetCode]125.Valid Palindrome
- LeetCode 125. Valid Palindrome
- [Leetcode] 125. Valid Palindrome
- 【LeetCode】125.Valid Palindrome
- [leetcode] 125.Valid Palindrome
- [leetcode] 125.Valid Palindrome
- Leetcode 125. Valid Palindrome
- 125. Valid Palindrome LeetCode
- 【LeetCode】125. Valid Palindrome
- Leetcode 125. Valid Palindrome
- LeetCode 125. Valid Palindrome
- LeetCode 125. Valid Palindrome
- LeetCode *** 125. Valid Palindrome
- 【leetcode】125. Valid Palindrome
- [leetcode] 125. Valid Palindrome
- 替换空格
- 操作系统的 (program)loader(程序加载器)
- 10分钟理解代理——JDK动态代理
- MySQL基础知识
- webrtc之signal机制
- [勇者闯LeetCode] 125. Valid Palindrome
- 算法系列—计算阶乘
- 基础头文件
- 内存对齐时为什么结构体的最终大小需要是其最大成员大小的整数倍
- 445端口,修不完的BUG
- H5新标签 定义动画
- OpenCV3.2.0+VS2017环境配置与常见问题(巨细坑爹版)
- 从paxous到zookeeper
- java反射(对象拷贝)