leetcode刷题18天
125. Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
Input: “A man, a plan, a canal: Panama”
Output: true
Example 2:
Input: “race a car”
Output: false
代码:
class Solution {
public boolean isPalindrome(String s) {
if(s.length()<=0)
return true;
String tempStr = s.replaceAll("[^A-Za-z0-9]", "").toLowerCase();
return new StringBuffer(tempStr).reverse().toString().equals(tempStr);
}
}
- Single Number
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
代码:
class Solution {
public int singleNumber(int[] nums) {
int res=0 ;
for(int i = 0;i<nums.length;i++) {
res = res^nums[i];
}
return res;
}
}