-
455.分发饼干
-
class Solution { public: int findContentChildren(vector<int>& g, vector<int>& s) { int count = 0; sort(g.begin(), g.end(), greater<int>()); sort(s.begin(), s.end(), greater<int>()); for (int i = 0, k = 0; i < g.size() && k < s.size(); i++) { if (g[i] <= s[k]) { count++; k++; } } return count; } } ;
-
376. 摆动序列
-
class Solution { public: int wiggleMaxLength(vector<int>& nums) { int count = 1; int flag = 0; if (nums.size() < 2) return 1; for (int i = 1; i < nums.size(); i++) { if (nums[i] == nums[i - 1]) continue; if (nums[i] > nums[i - 1]) { if (flag == 1) continue; count++; flag = 1; } if (nums[i] < nums[i - 1]) { if (flag == -1) continue; count++; flag = -1; } } return count; } };
-
53. 最大子序和
-
class Solution { public: int maxSubArray(vector<int>& nums) { int ret = -0x3f3f3f3f; int slow = 1; for (int i = 0, sum = 0; i < nums.size(); i++) { if (sum < 0) sum = 0; sum += nums[i]; if (sum > ret) ret = sum; } return ret; } };