- 浏览: 19073 次
- 性别:
- 来自: 北京
文章分类
最新评论
题目描述
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.
题目出处:https://oj.leetcode.com/problems/valid-palindrome/
解题思路
本题判断字符串是否为回文,只考虑字母数字且不考虑大小写。
自己的代码1
这个代码超时了,需要寻找更加有效的方法。
自己的代码2
这段代码可以运行,只对字符串进行了一次遍历。
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.
题目出处:https://oj.leetcode.com/problems/valid-palindrome/
解题思路
本题判断字符串是否为回文,只考虑字母数字且不考虑大小写。
自己的代码1
这个代码超时了,需要寻找更加有效的方法。
package leetcode; public class ValidPalindrome { public boolean isPalindrome(String s) { if(s == null) return false; if(s == "") return true; s = s.toLowerCase();//System.out.println(s); String str = ""; for(int i = 0; i < s.length(); i++){ char c = s.charAt(i); if(c >= 'a' && c <= 'z') str += c; }//System.out.println(str); if(str.length() == 0) return true; if(str.length() == 1) return true; boolean palindrome = true; for(int i = 0; i < str.length()/2; i++){ if(str.charAt(i) != str.charAt(str.length()-1-i)){ palindrome = false; break; } } return palindrome; } public static void main(String[] args) { //String s = ""; //String s = " "; //String s = " , . "; //String s = "A man, a plan, a canal: Panama"; String s = "race a car"; ValidPalindrome vp = new ValidPalindrome(); System.out.println(vp.isPalindrome(s)); } }
自己的代码2
这段代码可以运行,只对字符串进行了一次遍历。
package leetcode; public class ValidPalindrome { public boolean isPalindrome(String s) { if(s == null) return false; if(s == "") return true; if(s.length() == 1) return true; /*int a = 'a';System.out.println(a); int z = 'z';System.out.println(z); int A = 'A';System.out.println(A); int Z = 'Z';System.out.println(Z); int A1 = 'A' - 32;System.out.println((char)A1); int zero = '0';System.out.println(zero); int nine = '9';System.out.println(nine);*/ int begin = 0; int end = s.length() - 1; boolean palindrome = true; while(true){ char cBegin = 0, cEnd = 0; while(begin < s.length()){ cBegin = s.charAt(begin); if((cBegin >= 'A' && cBegin <= 'Z') || (cBegin >= '0' && cBegin <= '9')) break; else if(cBegin >= 'a' && cBegin <= 'z') { cBegin = (char) (cBegin - 32); break; } else begin++; } while(end >= 0){ cEnd = s.charAt(end); if((cEnd >= 'A' && cEnd <= 'Z') || (cEnd >= '0' && cEnd <= '9')) break; else if(cEnd >= 'a' && cEnd <= 'z') { cEnd = (char) (cEnd - 32); break; } else end--; } if(begin >= end) break; else{ if(cBegin != cEnd) { palindrome = false; break; } else{ begin++; end--; } } } return palindrome; } public static void main(String[] args) { //String s = ""; //String s = " "; //String s = " , . "; //String s = "A man, a plan, a canal: Panama"; //String s = "race a car"; //String s = "aA"; //String s = "1a2"; String s = "ab2a"; ValidPalindrome vp = new ValidPalindrome(); System.out.println(vp.isPalindrome(s)); } }
发表评论
-
Java中String与StringBuffer的区别
2014-10-29 21:07 297String和StringBuffer的区别,网上资料可以说 ... -
String to Integer (atoi)
2014-10-29 17:13 398题目描述 Implement atoi to convert ... -
Implement strStr()
2014-10-28 15:17 284题目描述 Implement strStr(). Retu ... -
ZigZag Conversion
2014-10-22 19:51 340题目描述 The string "PAYPALIS ... -
Add Binary
2014-10-22 19:43 301题目描述 Given two binary strings, ... -
Longest Common Prefix
2014-10-22 19:44 327题目描述 Write a function to find t ... -
Count and Say
2014-10-22 19:44 346题目描述 The count-and-say sequence ... -
Valid Sudoku
2014-10-21 10:22 355题目描述 Determine if a Sudoku is v ... -
Valid Parentheses
2014-10-21 09:41 322题目描述 Given a string containing ... -
Palindrome Number
2014-10-21 09:41 344题目描述 Determine whether an integ ... -
Length of Last Word
2014-10-21 09:41 357题目描述 Given a string s consists ... -
Minimum Depth of Binary Tree
2014-10-21 09:41 307题目描述 Given a binary tree, find ... -
Remove Nth Node From End of List
2014-10-20 16:36 255题目描述 Given a linked list, remov ... -
Path Sum
2014-10-20 15:37 292题目描述 Given a binary tree and a ... -
Binary Tree Level Order Traversal II
2014-10-20 11:17 232题目描述 Given a binary tree, retur ... -
Binary Tree Level Order Traversal
2014-10-20 11:03 292题目描述 Given a binary tree, retur ... -
Pascal's Triangle II
2014-10-20 10:07 255题目描述 Given an index k, return t ... -
Pascal's Triangle
2014-10-19 12:24 317题目描述 Given numRows, generate th ... -
Plus One
2014-10-19 11:51 336题目描述 Given a non-negative numbe ... -
Merge Sorted Array
2014-10-18 10:45 397题目描述 Given two sorted integer a ...
相关推荐
https://www.lintcode.com/problem/valid-palindrome/description 给定一个字符串,只考虑字母和数字,字母忽略大小写区别,忽略所有其他字符,问该字符串是否回文。直接对撞双指针,左指针持续右移直到遇到字母或...
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 ...
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 ...
...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....|-----|---------------- | --------------- |...
Palindrome Implement strStr() String to Integer (atoi) addBinary longestPalindrome maximal rectangle :dp问题,较难 largestRectangleArea 求直方图的最大面积,左右两次扫面+剪枝优化 Valid Parentheses 用栈...
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 ...
Valid Palindrome 问题的解决方案。 首先,我将给定字符串中的所有字符都小写,这样就不会有任何违规行为。 然后我创建两个空数组。 在一个数组中,我将给定字符串的所有字母数字值附加到该数组中。 在第二个中,我...
Palindrome :star: 有效回文,小写字母转换 0005 Longest Palindromic Substring :star: :star: :star: 最长回文子串,Manacher算法 0010 RegularExpressionMatching :star: :star: :star: 正则表达式匹配,dp 0012 ...
Palindrome 给定一个字符串,判断该字符串是否是回文串。回文串是从前后方向阅读都相同的串。 同时从字符串正序、逆序遍历字符串,如果所有字符都相同,则是回文串,否则不是回文串。 8. String to Integer 给定一个...
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 分类 ...Palindrome II Easy 数据结构 二叉树 题目 难度 原题 解答 26. 树的子结构 中等 未分类 鸣谢 题目选择、分类参考了 关于 同步自我的博客:, 欢迎关注我的微信公众号「清风迅来」
我自己在新学erlang,在LeetCode OJ上找了题目练习,题目很适合新手熟悉语言,但是LeetCode OJ里面只有几门主流语言的... "valid_palindrome.erl" 个人认为dungeon_game这个题目解题逻辑很体现erlang的函数式的思维逻辑
leetcode Python 001 力码 简单的 # 问题 完成日期 语 001 001_Two_Sum ...125_Valid_Palindrome 2021 年 6 月 12 日 C# 009 412_FizzBuzz 2021 年 6 月 22 日 C# 中等的 # 问题 完成日期 语
gas station leetcode 在牛客网上的在线编程中的leetcode在线编程题解 代码中有详细题解 完成: 树 Minimum Depth of ...evaluate-reverse-polish-notation ...valid-palindrome 模拟 pascals-triangle 模拟 pasca
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题解 传送门 # 标题 解决方案 困难 笔记 1个 简单的 3 ... Valid_Palindrome Java 简单的 136 单号 Java 简单的 137 Single_NumberII Java 中等的 167 Two_Sum_II_Input_
Palindrome Number 回文数 11 Container With Most Water 盛最多水的容器 13 Roman to Integer 罗马数字转整数 14 Longest Common Prefix 最长公共前缀 20 Valid Parentheses 有效的括号 26 Remove Duplicates from ...
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...
Palindrome Number 简单 字串 Container With Most Water 中等 动态规划 重要 Integer to Roman 中等 重要 Roman to Integer 简单 重要 Longest Common Prefix 简单 字串 Valid Parentheses 简单 堆叠 重要 Merge ...
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 ...