11. 盛最多水的容器 - 力扣(LeetCode)
双指针两头向里移动,短的先动;
class Solution {
public:
int maxArea(vector<int>& height) {
int l = 0, r = height.size() - 1;
int ans = 0;
while(l < r)
{
int area = min(height[l], height[r]) * (r - l);
ans = max(ans, area);
if(height[r] < height[l])
{
r--;
}
else
{
l++;
}
}
return ans;
}
};
392. 判断子序列 - 力扣(LeetCode)
双指针,他子序列的元素顺序是和父亲的顺序一样;
class Solution {
public:
bool isSubsequence(string s, string t) {
int n = s.size();
int m = t.size();
int i = 0;
int j = 0;
while(i < n && j < m)
{
if(s[i] == t[j])
{
i++;
}
j++;
}
return i == n;
}
};