`
hcx2013
  • 浏览: 82108 次
社区版块
存档分类
最新评论

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.

 

public class Solution {
    public boolean isPalindrome(String s) {
        if (s.length() == 0) {
        	return true;
        }
        s = s.toUpperCase();
        int first = 0;
        int end = s.length()-1;
        while (first < end) {
        	if ((s.charAt(first)<'A' || s.charAt(first)>'Z') && (s.charAt(first)>'9' || s.charAt(first)<'0' )) {
        		first++;
        		continue;
        	}
        	if ((s.charAt(end)<'A' || s.charAt(end)>'Z') && (s.charAt(end)>'9' || s.charAt(end)<'0' )) {
        		end--;
        		continue;
        	}
        	if (s.charAt(first) == s.charAt(end)) {
        		first++;
        		end--;
        	} else {
        		return false;
        	}
        }
        return true;
    }
}

 

分享到:
评论

相关推荐

    【Lintcode】415. Valid Palindrome

    https://www.lintcode.com/problem/valid-palindrome/description 给定一个字符串,只考虑字母和数字,字母忽略大小写区别,忽略所有其他字符,问该字符串是否回文。直接对撞双指针,左指针持续右移直到遇到字母或...

    cpp-算法精粹

    Valid Palindrome Implement strStr() String to Integer (atoi) Add Binary Longest Palindromic Substring Regular Expression Matching Wildcard Matching Longest Common Prefix Valid Number Integer to Roman ...

    leetcode浇花-LCSolutions:我的力扣解决方案

    Palindrome Number #0035 - Search Insert Position #0058 - Length of Last Word #0066 - Plus One #0083 - Remove Duplicates from Sorted List #0118 - Pascal's Triangle #0121 - Best Time to Buy and Sell ...

    LeetCode最全代码

    ...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...

    leetcode中国-leetcode:leetcode刷题

    Palindrome Implement strStr() String to Integer (atoi) addBinary longestPalindrome maximal rectangle :dp问题,较难 largestRectangleArea 求直方图的最大面积,左右两次扫面+剪枝优化 Valid Parentheses 用栈...

    javalruleetcode-LeetCode:LeetCode算法问题

    Palindrome LeetCode 167 Two Sum II - Input array is sorted LeetCode 344 Reverse String LeetCode 345 Reverse Vowels of a String 2 字符串 编号 题目 LeetCode 3 Longest Substring Without Repeating ...

    leetcode不会-ValidPalindrome:这是我在Python中针对LeetCode上的ValidPalindrome问题的解决方

    Valid Palindrome 问题的解决方案。 首先,我将给定字符串中的所有字符都小写,这样就不会有任何违规行为。 然后我创建两个空数组。 在一个数组中,我将给定字符串的所有字母数字值附加到该数组中。 在第二个中,我...

    leetcode卡-LeetCode:LeetCode题解

    Palindrome :star: 有效回文,小写字母转换 0005 Longest Palindromic Substring :star: :star: :star: 最长回文子串,Manacher算法 0010 RegularExpressionMatching :star: :star: :star: 正则表达式匹配,dp 0012 ...

    leetcode环形数组取值-Crack-Interview::ghost:为什么人们一次又一次地这样做?

    Palindrome 给定一个字符串,判断该字符串是否是回文串。回文串是从前后方向阅读都相同的串。 同时从字符串正序、逆序遍历字符串,如果所有字符都相同,则是回文串,否则不是回文串。 8. String to Integer 给定一个...

    leetcode516-Lcode:代码

    leetcode 516 8/13 - 8/18 周:leetcode#: ...Valid Palindrome 28. Implement strStr() 5. Longest Palindromic Substring option: 516. Longest Palindromic Subsequence string replacement strStr II 链接:

    leetcode分类-leetcode-practice:LeetCode题解

    leetcode 分类 ...Palindrome II Easy 数据结构 二叉树 题目 难度 原题 解答 26. 树的子结构 中等 未分类 鸣谢 题目选择、分类参考了 关于 同步自我的博客:, 欢迎关注我的微信公众号「清风迅来」

    erlang入门级练习:LeetCode OJ问题的部分erlang 源码

    我自己在新学erlang,在LeetCode OJ上找了题目练习,题目很适合新手熟悉语言,但是LeetCode OJ里面只有几门主流语言的... "valid_palindrome.erl" 个人认为dungeon_game这个题目解题逻辑很体现erlang的函数式的思维逻辑

    leetcodepython001-LeetCode:力码

    leetcode Python 001 力码 简单的 # 问题 完成日期 语 001 001_Two_Sum ...125_Valid_Palindrome 2021 年 6 月 12 日 C# 009 412_FizzBu​​zz 2021 年 6 月 22 日 C# 中等的 # 问题 完成日期 语

    gasstationleetcode-leetcode-in-niuke:在牛客网上的在线编程中的leetcode在线编程题解

    gas station leetcode 在牛客网上的在线编程中的leetcode在线编程题解 代码中有详细题解 完成: 树 Minimum Depth of ...evaluate-reverse-polish-notation ...valid-palindrome 模拟 pascals-triangle 模拟 pasca

    程序员面试宝典LeetCode刷题手册

    9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17. Letter Combinations of a Phone Number 18. 4Sum 19. Remove Nth Node From End of List 20. Valid ...

    LeetCode:LeetCode题解

    LeetCode LeetCode题解 传送门 # 标题 解决方案 困难 笔记 1个 简单的 3 ... Valid_Palindrome Java 简单的 136 单号 Java 简单的 137 Single_NumberII Java 中等的 167 Two_Sum_II_Input_

    颜色分类leetcode-leetcode-[removed]我对Leetcode问题的解决方案

    Palindrome Number 回文数 11 Container With Most Water 盛最多水的容器 13 Roman to Integer 罗马数字转整数 14 Longest Common Prefix 最长公共前缀 20 Valid Parentheses 有效的括号 26 Remove Duplicates from ...

    leetcode530-algorithm:算法

    Palindrome Number 010 Regular Expression Matching 011 Container With Most Water 012 Integer to Roman 013 Roman to Integer 014 Longest Common Prefix 015 3Sum 016 3Sum Closest 017 Letter Combinations of...

    lrucacheleetcode-leetcode:leetcode

    Palindrome Number 11. Container With Most Water 12. Integer to Roman 13. Roman to Integer 14. Longest Common Prefix 15. 3Sum 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses ...

    leetcode双人赛-LeetCode:力扣笔记

    Palindrome Number 简单 字串 Container With Most Water 中等 动态规划 重要 Integer to Roman 中等 重要 Roman to Integer 简单 重要 Longest Common Prefix 简单 字串 Valid Parentheses 简单 堆叠 重要 Merge ...

Global site tag (gtag.js) - Google Analytics