classSolution{public:int n, m;
vector<vector<int>> f, w;int dx[4]={-1,0,1,0}, dy[4]={0,1,0,-1};intdp(int x,int y){auto& v = f[x][y];if(v !=-1)return v;
v =1;for(int i =0; i <4; i ++){int a = x + dx[i], b = y + dy[i];if(a >=0&& a < n && b >=0&& b < m && w[x][y]< w[a][b])
v =max(v,dp(a, b)+1);}return v;}intlongestIncreasingPath(vector<vector<int>>& matrix){if(matrix.empty()|| matrix[0].empty())return0;
w = matrix;
n = w.size(), m = w[0].size();
f = vector<vector<int>>(n, vector<int>(m,-1));int res =0;for(int i =0; i < n; i ++)for(int j =0; j < m; j ++)
res =max(res,dp(i, j));return res;}};
classSolution{public:intminPatches(vector<int>& nums,int n){longlong x =1;int i =0, res =0;while(x <= n){if(i < nums.size()&& nums[i]<= x) x += nums[i ++];else{
x += x;
res ++;}}return res;}};
classSolution{publicintminPatches(int[] nums,int n){int patches =0;long x =1;int length = nums.length, index =0;while(x <= n){if(index < length && nums[index]<= x){
x += nums[index];
index++;}else{
x *=2;
patches++;}}return patches;}}
classSolution:defminPatches(self, nums: List[int], n:int)->int:
patches, x =0,1
length, index =len(nums),0while x <= n:if index < length and nums[index]<= x:
x += nums[index]
index +=1else:
x <<=1
patches +=1return patches
funcminPatches(nums []int, n int)(patches int){for i, x :=0,1; x <= n;{if i <len(nums)&& nums[i]<= x {
x += nums[i]
i++}else{
x *=2
patches++}}return}
第三百三十一题:
classSolution{public:int k;
string s;boolisValidSerialization(string _s){
k =0;
s = _s +',';if(!dfs())returnfalse;return k == s.size();}booldfs(){if(k == s.size())returnfalse;if(s[k]=='#')return k +=2,true;while(s[k]!=',') k ++;
k ++;returndfs()&&dfs();}};
classSolution{publicbooleanisValidSerialization(String preorder){// number of available slotsint slots =1;int n = preorder.length();for(int i =0; i < n;++i){if(preorder.charAt(i)==','){// one node takes one slot--slots;// no more slots availableif(slots <0)returnfalse;// non-empty node creates two children slotsif(preorder.charAt(i -1)!='#') slots +=2;}}// the last node
slots =(preorder.charAt(n -1)=='#')? slots -1: slots +1;// all slots should be used upreturn slots ==0;}}
classSolution:defisValidSerialization(self, preorder:str)->bool:
num=1
preorder=preorder.split(',')for i in preorder:if num==0:returnFalseif i =='#':
num-=1if num<0:returnFalseelse:
num+=1if num==0:returnTrueelse:returnFalse
funcfindItinerary(tickets [][]string)[]string{var(
m =map[string][]string{}
res []string)for_, ticket :=range tickets {
src, dst := ticket[0], ticket[1]
m[src]=append(m[src], dst)}for key :=range m {
sort.Strings(m[key])}var dfs func(curr string)
dfs =func(curr string){for{if v, ok := m[curr];!ok ||len(v)==0{break}
tmp := m[curr][0]
m[curr]= m[curr][1:]dfs(tmp)}
res =append(res, curr)}dfs("JFK")for i :=0; i <len(res)/2; i++{
res[i], res[len(res)-1- i]= res[len(res)-1- i], res[i]}return res
}
第三百三十四题:
classSolution{public:boolincreasingTriplet(vector<int>& nums){
vector<int>q(2, INT_MAX);for(auto a: nums){int k =2;while(k >0&& q[k -1]>= a) k --;if(k ==2)returntrue;
q[k]= a;}returnfalse;}};
classSolution{publicbooleanincreasingTriplet(int[] nums){int one = Integer.MAX_VALUE,two = Integer.MAX_VALUE;for(int three : nums){if(three > two)returntrue;elseif(three <= one) one = three;else two = three;// if(three > one && three < two) two = three;}returnfalse;}}
classSolution:defincreasingTriplet(self, nums: List[int])->bool:
r1, r2 = sys.maxsize, sys.maxsize
for n in nums :if n <= r1 : r1 = n
elif n <= r2 : r2 = n
else:returnTruereturnFalse
funcincreasingTriplet(nums []int)bool{
m1, m2 := math.MaxInt64, math.MaxInt64
for i :=0; i <len(nums); i++{if m1 >= nums[i]{
m1 = nums[i]}elseif m2 >= nums[i]{
m2 = nums[i]}else{returntrue}}returnfalse}
classSolution:defisSelfCrossing(self, x)->bool:iflen(x)<4:returnFalseiflen(x)==4:returnTrueif x[3]>= x[1]and x[2]<= x[0]elseFalsefor i inrange(3,len(x)):if x[i]>= x[i-2]and x[i-1]<= x[i-3]:returnTrueif i >3and x[i-1]== x[i-3]and x[i-4]+ x[i]>= x[i-2]:returnTrueif i >4and x[i-3]-x[i-5]<= x[i-1]<= x[i-3]and x[i-2]-x[i-4]<= x[i]<= x[i-2]and x[i-2]> x[i-4]:returnTruereturnFalse
第三百三十六题:
classSolution{public:boolcheck(string& s){for(int i =0, j = s.size()-1; i < j; i ++, j --)if(s[i]!= s[j])returnfalse;returntrue;}
vector<vector<int>>palindromePairs(vector<string>& words){
unordered_map<string,int> hash;for(int i =0; i < words.size(); i ++){auto w = words[i];reverse(w.begin(), w.end());
hash[w]= i;}
vector<vector<int>> res;for(int i =0; i < words.size(); i ++){auto w = words[i];for(int j =0; j <= w.size(); j ++){auto left = w.substr(0, j), right = w.substr(j);if(check(right)&& hash.count(left)&& hash[left]!= i) res.push_back({i, hash[left]});if(check(left)&& hash.count(right)&& hash[right]!= i && w.size()!= words[hash[right]].size())
res.push_back({hash[right], i});}}return res;}};
classSolution{
List<String> wordsRev =newArrayList<String>();
Map<String, Integer> indices =newHashMap<String, Integer>();public List<List<Integer>>palindromePairs(String[] words){int n = words.length;for(String word: words){
wordsRev.add(newStringBuffer(word).reverse().toString());}for(int i =0; i < n;++i){
indices.put(wordsRev.get(i), i);}
List<List<Integer>> ret =newArrayList<List<Integer>>();for(int i =0; i < n; i++){
String word = words[i];int m = words[i].length();if(m ==0){continue;}for(int j =0; j <= m; j++){if(isPalindrome(word, j, m -1)){int leftId =findWord(word,0, j -1);if(leftId !=-1&& leftId != i){
ret.add(Arrays.asList(i, leftId));}}if(j !=0&&isPalindrome(word,0, j -1)){int rightId =findWord(word, j, m -1);if(rightId !=-1&& rightId != i){
ret.add(Arrays.asList(rightId, i));}}}}return ret;}publicbooleanisPalindrome(String s,int left,int right){int len = right - left +1;for(int i =0; i < len /2; i++){if(s.charAt(left + i)!= s.charAt(right - i)){returnfalse;}}returntrue;}publicintfindWord(String s,int left,int right){return indices.getOrDefault(s.substring(left, right +1),-1);}}
classSolution:defpalindromePairs(self, words: List[str])-> List[List[int]]:deffindWord(s:str, left:int, right:int)->int:return indices.get(s[left:right+1],-1)defisPalindrome(s:str, left:int, right:int)->bool:return(sub := s[left:right+1])== sub[::-1]
n =len(words)
indices ={word[::-1]: i for i, word inenumerate(words)}
ret =list()for i, word inenumerate(words):
m =len(word)for j inrange(m +1):if isPalindrome(word, j, m -1):
leftId = findWord(word,0, j -1)if leftId !=-1and leftId != i:
ret.append([i, leftId])if j and isPalindrome(word,0, j -1):
rightId = findWord(word, j, m -1)if rightId !=-1and rightId != i:
ret.append([rightId, i])return ret
funcpalindromePairs(words []string)[][]int{
wordsRev :=[]string{}
indices :=map[string]int{}
n :=len(words)for_, word :=range words {
wordsRev =append(wordsRev,reverse(word))}for i :=0; i < n; i++{
indices[wordsRev[i]]= i
}
ret :=[][]int{}for i :=0; i < n; i++{
word := words[i]
m :=len(word)if m ==0{continue}for j :=0; j <= m; j++{ifisPalindrome(word, j, m -1){
leftId :=findWord(word,0, j -1, indices)if leftId !=-1&& leftId != i {
ret =append(ret,[]int{i, leftId})}}if j !=0&&isPalindrome(word,0, j -1){
rightId :=findWord(word, j, m -1, indices)if rightId !=-1&& rightId != i {
ret =append(ret,[]int{rightId, i})}}}}return ret
}funcfindWord(s string, left, right int, indices map[string]int)int{if v, ok := indices[s[left:right+1]]; ok {return v
}return-1}funcisPalindrome(s string, left, right int)bool{for i :=0; i <(right - left +1)/2; i++{if s[left + i]!= s[right - i]{returnfalse}}returntrue}funcreverse(s string)string{
n :=len(s)
b :=[]byte(s)for i :=0; i < n/2; i++{
b[i], b[n-i-1]= b[n-i-1], b[i]}returnstring(b)}