文档讲解:代码随想录 (programmercarl.com)
视频讲解:代码随想录的个人空间-代码随想录个人主页-哔哩哔哩视频 (bilibili.com)
代码如下:
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(),g.end());
sort(s.begin(),s.end());
int count=0;
for(int i=0;i<s.size();i++){
if(count<g.size()&&s[i]>=g[count])count++;
}
return count;
}
};
代码如下:
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
if (nums.size() <= 1) return nums.size();
int res=1;
int prediff=0;
int curdiff=0;
for(int i=0;i<nums.size()-1;i++){
curdiff=nums[i+1]-nums[i];
if((prediff<=0&&curdiff>0)||(prediff>=0&&curdiff<0)){
res++;
prediff=curdiff;
}
}
return res;
}
};
题目链接:53. 最大子数组和 - 力扣(LeetCode)
解题思路:太巧妙了
解题代码如下:
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int res=INT32_MIN;
int count=0;
for(int i=0;i<nums.size();i++){
count+=nums[i];
if(count>res)res=count;
if(count<=0)count=0;
}
return res;
}
};