[LeetCode]-Valid Palindrome (correct retrieval), leetcodepalindrome
[Problem:]
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Returns a string to determine whether it is a forward slash (only letters and numbers are counted. ignore other characters ).
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.
[Solution 1:]
Prepare valid letters and numbers, traverse the target string, and put valid characters into the buffer.
Then compare the strings in the buffer with the reversed strings. If they are the same, they are the same as the background strings. Otherwise, they are not the background strings.
Comment: This solution can be used by the system accept, but too many APIs are called, and the performance is average.
public class Solution {public boolean isPalindrome(String s) {StringBuilder buffer = new StringBuilder();String tempStr = "abcdefghijklmnopqrstuvwxyz0123456789"; char[] cArr = s.toCharArray();for (int i = 0; i < cArr.length; i++) { if (tempStr.contains(String.valueOf(cArr[i]).toLowerCase())) {buffer.append(String.valueOf(cArr[i]).toLowerCase());}}String currentStr = buffer.toString();String reverseStr = buffer.reverse().toString();if (currentStr.equals(reverseStr)) {return true;}return false;}}
[Solution 2:]
Using the bipartite method, a pointer is traversed from the left, and a pointer is traversed from the right. Non-letters and non-numbers are skipped. When the traversal is still the same to the midpoint, the system returns the text.
Comments: The code is clear and the performance is good.
Public class Solution {/*** determines whether it is a return *** @ param String str * @ return boolean true (is palindrome)/false (is not palindrome) */public boolean isPalindrome (String s) {if (s = null) {return false;} int I = 0; int j = s. length ()-1; while (I <j) {if (! IsAlphanumeric (s. charAt (I) {I ++; continue;} if (! IsAlphanumeric (s. charAt (j) {j --; continue;} if (Character. toLowerCase (s. charAt (I) = Character. toLowerCase (s. charAt (j) {I ++; j --; continue;} return false;} return true ;} /*** determine whether it is a letter or number ** @ param char character * @ return boolean true (is alphanumeric)/false (is not alphanumeric) */public boolean isAlphanumeric (char c) {if (c> = 'A' & c <= 'Z') | (c> = 'A' & c <= 'Z ') | (c> = '0' & c <= '9') {return true;} return false ;}}