Leetcode 1147. Longest Chunked Palindrome Decomposition. 【Hard】【Blue】
题意:
class Solution {
public int longestDecomposition(String text) {
int res = 0;
int left = 0, right = text.length()-1;
String lStr = "", rStr = "";
while(leftright && lStr.equals("")){ //特殊情况:"ABAB",导致 left>right && lStr是empty
return res;
} else {
return res+1;
}
}
}
分析:什么情况下返回res而不是res+1?Special Case: "camelcamel",或者"avav".
notEmpty; empty
left==right res+1 res+1
left>right res+1 res
Leetcode 最佳解法: Leetcode 937. Reorder Log Files. 【Easy】【Green】 Leetcode 1160. Find Words That Can Be Formed by Characters 【Easy】 Leetcode 771. Jewels and Stones. 【Easy】【】 Leetcode 49. Group Anagrams. 【Green】【Medium】 Leetcode 242. Valid Anagram. Leetcode 383. Ransom Note. Leetcode 387. First Unique Character in a String. Leetcode 22. Generate Parentheses. 【Green】【Medium】 Leetcode 380. Insert Delete GetRandom O(1). Leetcode 381. Insert Delete GetRandom O(1) - Duplicates allowed. 【】 Leetcode 28. Implement strStr(). 【Easy】 Leetcode 125. Valid Palindrome. Leetcode 344. Reverse String. Leetcode 811. Subdomain Visit Count. Leetcode 349. Intersection of Two Arrays. Leetcode 350. Intersection of Two Arrays II. 三个 Follow up: Leetcode 202. Happy Number. 最佳解法:参考Node的一道题,是否成环。 Leetcode 605. Can Place Flowers.
遍历1次,若equals只+1。避免了left public int longestDecomposition(String S) {
int res = 0, n = S.length();
String l = "", r = "";
for (int i = 0; i < n; ++i) {
l = l + S.charAt(i);
r = S.charAt(n - i - 1) + r;
if (l.equals(r)) {
++res;
l = "";
r = "";
}
}
return res;
}
class Solution {
//lexicographically: 字典的,按照字母排序的
public String[] reorderLogFiles(String[] logs) {
Comparator
class Solution {
public int countCharacters(String[] words, String chars) {
int[] alphabet = new int[26];
for(char c: chars.toCharArray()){
alphabet[c-'a']++;
}
int res = 0;
for(String word: words){
int[] copy = Arrays.copyOf(alphabet,alphabet.length);
//实际上是调用了System.arraycopy()方法,相当于执行了以下代码:
// int[] copy=new int[26];
// System.arraycopy(alphabet,0,copy,0,copy.length);
boolean legal =true;
int count=0;
for(char c: word.toCharArray()){
copy[c-'a']--;
count++;
if(copy[c-'a']<0) {
legal = false;
break;
}
}
if(legal) res+=count;
}
return res;
}
}
Tag:HashSet
题目简介:一个字符串代表珠宝,另一个字符串代表石头和珠宝的混合。求字符串2中含有多少个字符串1代表的珠宝。
解答过程:肯定是将字符串中的char存起来,然后搜索。HashMap的search需要O(n), 而HashSet的search需要O(1). 所以用HashSet来存储。class Solution {
//I used hash set and it's O(1) to check if it contains an element.
//So the total time complexity will be O(M+N), instead of O(MN)
public int numJewelsInStones(String J, String S) {
int res = 0;
Set jewels = new HashSet();
for(char j: J.toCharArray()) jewels.add(j);
for(char s: S.toCharArray()) res = (jewels.contains(s))? res+1:res;
return res;
}
}
Time: O(mn), strs个数字符串平均长度。或者说O(sum of all chars in strs).
Space: 同上,O(sum of all chars in strs)。class Solution {
//anagram:易位构词游戏,是将组成一个词或短句的字母重新排列顺序,
//原文中所有字母的每次出现都被使用一次,这样构造出另外一些新的词或短句。
//比如,listen变为silnet。
public List
> groupAnagrams(String[] strs) {
Map
class Solution {
public boolean isAnagram(String s, String t) {
int[] alphabet = new int[26];
if(s.length()!=t.length()) return false;
for(int i=0; i
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] alphabet = new int[26];
for(int i=0; i
class Solution {
public int firstUniqChar(String s) {
int[] alphabet = new int[26];
for(int i=0; i
class Solution {
public List
class RandomizedSet {
Map
class RandomizedCollection {
Map
class Solution {
public int strStr(String haystack, String needle) {
if(needle==null || needle.length()==0) return 0;
int h = haystack.length(), n = needle.length();
for(int i=0; i<=h-n; i++){
// for(int j=0; j
class Solution {
public boolean isPalindrome(String s) {
if(s==null || s.length()<=1) return true;
int start = 0, end=s.length()-1;
while(start
class Solution {
public void reverseString(char[] s) {
//若是传入的是String,需要用:
//1) char[] word = s.toCharArray();
//2) return new String(word);
int start = 0, end = s.length-1;
while(start
class Solution {
public List
class MinStack {
/** initialize your data structure here. */
Deque
1)What if the given array is already sorted? How would you optimize your algorithm?
2)What if nums1's size is small compared to nums2's size? Which algorithm is better?
3)What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
用HashSet做:(这不是最优的)class Solution {
public boolean isHappy(int n) {
HashSet
class Solution {
public boolean isHappy(int n) {
int x = n;
int y = x;
while(x!=1){
x = count(x); //x在原x基础上进一步
y = count(count(y)); //y在原y基础上进两步
if(x==1)return true;
if(y==1) return true;
if(x==y) return false;
}
return true;
}
public int count(int n){
int res = 0;
while(n>0){
int mod = n%10;
res += mod*mod;
n = n/10;
}
return res;
}
}
class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int add = 0;
for(int i=0; i