用栈的思路最简单,普通字符弹入,#字符弹出。
class Solution {
public:
bool backspaceCompare(string s, string t) {
return build(s)==build(t);
}
string build(string k){
string res;
for(char ch:k){
if(ch!='#'){
res.push_back(ch);
}else if(!res.empty()){
res.pop_back();
}
}
return res;
}
};
主要是用比较区间末端大小来筛选有用区间
class Solution {
public:
vector<vector<int>> intervalIntersection(vector<vector<int>>& A, vector<vector<int>>& B) {
int i = 0, j = 0;
vector<vector<int>> res;
while (i < A.size() && j < B.size()) {
int l = max(A[i][0], B[j][0]);
int r = min(A[i][1], B[j][1]);
if (l <= r) {
res.push_back({l, r});
}
// 谁的尾部小 移动谁
if (A[i][1] < B[j][1])
i++;
else
j++;
}
return res;
}
};
双指针法,两边开始移动,谁小移谁
class Solution {
public:
int maxArea(vector<int>& height) {
int l=0,r=height.size()-1;
int m=0;
while(l<r){
m=max(m,min(height[l],height[r])*(r-l));
if(height[l]<height[r]){
l++;
}
else{
r--;
}
}
return m;
}
};