本篇参照大佬博客练习题顺序,代码都是自打的,有些可能不是最优解。
目录
455. 分发饼干
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(),g.end());
sort(s.begin(),s.end());
int r=g.size()-1;
int ans=0;
if(r==-1)
return 0;
for(int i=s.size()-1;i>=0;i--)
{
while(s[i]<g[r])
{
r--;
if(r==-1)
break;
}
if(r==-1)
break;
if(s[i]>=g[r])
ans++,r--;
if(r==-1)
break;
}
return ans;
}
};
435. 无重叠区间
class Solution {
public:
struct node{
int l,r;
};
vector<node>a;
static bool cmp(node p,node q)
{
if(p.r!=q.r)
return p.r<q.r;
else
return p.l<q.l;
}
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
if(intervals.size()<=1)
return 0;
for(int i=0;i<intervals.size();i++)
{
a.push_back({intervals[i][0],intervals[i][1]});
}
sort(a.begin(),a.end(),cmp);
int s=0,k=a[0].r;
for(int i=1;i<a.size();i++)
{
if(a[i].l<k)
{
s++;
}
else
{
k=a[i].r;
}
}
return s;
}
};
452. 用最少数量的箭引爆气球
class Solution {
public:
struct node{
int l,r;
};
vector<node>a;
static bool cmp (node p,node q)
{
if(p.r!=q.r)
return p.r<q.r;
else
return p.l<q.l;
}
int findMinArrowShots(vector<vector<int>>& points) {
for(int i=0;i<points.size();i++)
{
a.push_back({points[i][0],points[i][1]});
}
int s=1;
sort(a.begin(),a.end(),cmp);
if(a.size()==0)
return 0;
int k=a[0].r;
for(int i=1;i<a.size();i++)
{
if(a[i].l>k)
{
s++;
k=a[i].r;
}
}
return s;
}
};
406. 根据身高重建队列
class Solution {
public:
struct node{
int h;
int n;
int id;
};
vector<node>a;
static bool cmp(node p,node q)
{
if(p.h!=q.h)
return p.h>q.h;
else
return p.n<q.n;
}
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
vector<vector<int>>ans;
for(int i=0;i<people.size();i++)
{
a.push_back({people[i][0],people[i][1]});
}
sort(a.begin(),a.end(),cmp);
vector<int>u,v;
u.push_back(0);
u.push_back(0);
v.push_back(0);
v.push_back(0);
for(int i=0;i<a.size();i++)
{
u[0]=a[i].h;
u[1]=a[i].n;
int s=0,flag=0;
for(int j=0;j<ans.size();j++)
{
if(flag==1)
{
u[0]=ans[j][0];
u[1]=ans[j][1];
ans[j][0]=v[0];
ans[j][1]=v[1];
v[0]=u[0];
v[1]=u[1];
continue;
}
v[0]=ans[j][0];
v[1]=ans[j][1];
if(v[0]>=u[0])
s++;
if(s>u[1])
{
flag=1;
ans[j][0]=u[0];
ans[j][1]=u[1];
}
}
if(flag==1)
{
ans.push_back(v);
}
else
{
ans.push_back(u);
}
}
return ans;
}
};
121. 买卖股票的最佳时机
class Solution {
public:
int maxProfit(vector<int>& prices) {
stack<int>ans;
ans.push(prices[0]);
int x=0,min1=prices[0];
for(int i=1;i<prices.size();i++)
{
while(ans.top()>=prices[i])
{
ans.pop();
if(ans.size()==0)
break;
}
ans.push(prices[i]);
if(ans.size()==1)
{
min1=min(min1,prices[i]);
}
x=max(x,prices[i]-min1);
}
return x;
}
};
122. 买卖股票的最佳时机 II
class Solution {
public:
int maxProfit(vector<int>& prices) {
int p=prices[0],s=0;
for(int i=1;i<prices.size();i++)
{
if(prices[i]<prices[i-1])
{
s+=prices[i-1]-p;
p=prices[i];
}
}
s+=prices[prices.size()-1]-p;
return s;
}
};
605. 种花问题
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
if(n==0)
return true;
for(int i=0;i<flowerbed.size();i++)
{
if(flowerbed[i]==1)
{
i++;
continue;
}
else if(i==flowerbed.size()-1&&i==0)
{
n--;
flowerbed[i]=1;
if(n==0)
return true;
}
else if(i==flowerbed.size()-1&&flowerbed[i-1]==0)
{
n--;
flowerbed[i]=1;
if(n==0)
return true;
}
else if(i==0&&flowerbed[i+1]==0)
{
n--;
flowerbed[i]=1;
if(n==0)
return true;
}
else if(i!=0&&i!=flowerbed.size()-1&&flowerbed[i-1]==0&&flowerbed[i+1]==0)
{
n--;
flowerbed[i]=1;
if(n==0)
{
return true;
}
}
}
return false;
}
};
392. 判断子序列
class Solution {
public:
bool isSubsequence(string s, string t) {
int i=0,j=0;
while(i<s.size()&&j<t.size())
{
if(s[i]==t[j])
{
i++;
}
j++;
}
if(i==s.size())
return true;
else
return false;
}
};
665. 非递减数列
class Solution {
public:
bool checkPossibility(vector<int>& nums) {
int flag=0;
int p=0;
for(int i=1;i<nums.size();i++)
{
if(nums[i-1]<=nums[i])
{
p=nums[i-1];
}
else
{
if(flag==0)
{
if(p<=nums[i])
{
flag=1;
}
else
{
flag=-1;
break;
}
}
else
{
flag=-1;
break;
}
}
}
if(flag==1||flag==0)
return true;
else
{
flag=0;
for(int i=1;i<nums.size();i++)
{
if(nums[i-1]<=nums[i])
{
continue;
}
else
{
if(flag==0)
{
nums[i]=nums[i-1];
flag=1;
}
else
{
flag=-1;
break;
}
}
}
if(flag==-1)
return false;
else
return true;
}
}
};
53. 最大子序和
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n=nums.size();
int dp[n+1][2];
if(n==0)
return 0;
dp[0][0]=0;
dp[0][1]=nums[0];
int max1=nums[0];
for(int i=1;i<n;i++)
{
dp[i][0]=0;
dp[i][1]=max(dp[i-1][0],dp[i-1][1])+nums[i];
max1=max(dp[i][1],max1);
}
return max1;
}
};
763. 划分字母区间
class Solution {
public:
vector<int> partitionLabels(string S) {
unordered_map<char,int>m;
for(int i=0;i<S.size();i++)
{
m[S[i]]=max(m[S[i]],i);
}
int max1=0,l=0;
vector<int>ans;
for(int i=0;i<=max1;i++)
{
max1=max(max1,m[S[i]]);
if(max1==i)
{
ans.push_back(i-l+1);
l=i+1;
max1=i+1;
}
if(max1>=S.size())
break;
}
return ans;
}
};