125 Valid Palindrome Easy
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.
思路:从两头向中间依次对应比较。
class Solution {
public boolean isPalindrome(String s) {
if(s == "") {
return true;
}
//所有字母变为小写
s = s.toLowerCase();
//删除字母数字外的其它字符
s = s.replaceAll("[^a-z0-9]","");
int n = s.length();
int i = 0, j = n-1;
while(i<j) {
if(s.charAt(i) != s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
}
680 Valid Palindrome II Easy
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.
Example 1:
Input: "aba"
Output: True
Example 2:
Input: "abca"
Output: True
Explanation: You could delete the character 'c'.
Note:
The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
思路:从两头开始比较,当遇到两个字符不相同时,会有两种情况:
将左边一个舍去,剩下的都对应相同;
将右边一个舍去,剩下的都对应相同。
这两种情况要分别尝试!不能在一个循环里同时试。
舍去的时候,看一下舍去后是否相同,如果是,继续,如果不是就说明舍去这个行不通。
为什么说舍左边/舍右边不能在一个循环里同时试呢?因为你在同一个循环里试的时候肯定有个先后,而存在一种情况,在当下,舍左&舍右下一步都是相同的,这时候你肯定就舍左了,因为舍左判断在前,结果到后面又发现行不通。 但其实舍右是行得通的,而程序里就忽略了这种情况!
例子:"mlcupuufxxfuupuculm"
class Solution {
public boolean validPalindrome(String s) {
int i = 0, j = s.length()-1;
boolean hasDeleted = false;
boolean leftFail = false;
while(i<j) {
if(s.charAt(i)!=s.charAt(j)) {
if(!hasDeleted) {
if(j-i == 1) {
return true;
}
if(s.charAt(i+1)==s.charAt(j)) {
i++;
hasDeleted = true;
} else {
leftFail = true;
break;
}
} else {
leftFail = true;
break;
}
}
i++;
j--;
}
if(!leftFail) {
return true;
} else {
hasDeleted = false;
i = 0;
j = s.length()-1;
while(i<j) {
if(s.charAt(i)!=s.charAt(j)) {
if(!hasDeleted) {
if(j-i == 1) {
return true;
}
if(s.charAt(i)==s.charAt(j-1)) {
j--;
hasDeleted = true;
} else {
return false;
}
} else {
return false;
}
}
i++;
j--;
}
return true;
}
}
}
67 Add Binary Easy
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
class Solution {
public String addBinary(String a, String b) {
int carry = 0;
int i = a.length() - 1;
int j = b.length() - 1;
String result = "";
while(i>=0 && j>=0) {
// char -> int
int A = a.charAt(i)-'0';
int B = b.charAt(j)-'0';
int temp = A+B+carry;
int res = temp%2; // k进制就用k
carry = temp/2;
result = res + result;
i--;
j--;
}
if(i>=0) {
while(i>=0) {
int A = a.charAt(i)-'0';
int temp = A+carry;
int res = temp%2;
carry = temp/2;
result = res + result;
i--;
}
} else if(j>=0) {
while(j>=0) {
int B = b.charAt(j)-'0';
int temp = B+carry;
int res = temp%2;
carry = temp/2;
result = res + result;
j--;
}
}
if(carry == 1) {
return "1"+result; //字符串拼接
} else {
return result;
}
}
}
49 Group Anagrams Medium
Given an array of strings, group anagrams together.
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
Note:
All inputs will be in lowercase.
The order of your output does not matter.
思路:用散列表,Key是按字母序升序的字符串,Value是String的ArrayList,放每一个原始字符串。遍历字符串数组,对每一个字符串,取得其排序后的字符串,然后将其插入散列表。
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String,ArrayList<String>> map = new HashMap<>();
for(int i=0; i<strs.length; i++) {
String sortedStr = sortStr(strs[i]);
if(map.containsKey(sortedStr)) {
map.get(sortedStr).add(strs[i]);
} else {
ArrayList<String> list = new ArrayList();
list.add(strs[i]);
map.put(sortedStr,list);
}
}
List<List<String>> result = new ArrayList<>();
map.forEach((key,value) -> {
result.add(value);
});
return result;
}
private String sortStr(String str) {
char[] charArray = str.toCharArray();
Arrays.sort(charArray);
return new String(charArray);
}
}