455.分发饼干
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
int i=0,j=0;
int num = 0;
sort(g.begin(),g.end());
sort(s.begin(),s.end());
while(i<g.size()&&j<s.size()){
if(g[i]<=s[j]){
i++;
j++;
num++;
}else{
j++;
}
}
return num;
}
};
376. 摆动序列
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
if (nums.size() <= 1) return nums.size();
int curDiff = 0; // 当前一对差值
int preDiff = 0; // 前一对差值
int result = 1; // 记录峰值个数,序列默认序列最右边有一个峰值
for (int i = 0; i < nums.size() - 1; i++) {
curDiff = nums[i + 1] - nums[i];
// 出现峰值
if ((preDiff <= 0 && curDiff > 0) || (preDiff >= 0 && curDiff < 0)) {
result++;
preDiff = curDiff; // 注意这里,只在摆动变化的时候更新prediff
}
}
return result;
}
};
53. 最大子序和
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int result = INT32_MIN;
int count = 0;
for(int i=0;i<nums.size();i++){
count+=nums[i];
if(count>result)
result = count;
if(count<=0) count = 0;
}
return result;
}
};