-
今日学习的文章链接,或者视频链接
-
自己看到题目的第一想法
-
看完代码随想录之后的想法
455:
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(),g.end());
sort(s.begin(),s.end());
int g_size = g.size();
int s_size = s.size();
int result = 0;
for(int i = 0,j = 0;i<g_size&&j<s_size;j++){
if(g[i]<=s[j]){
result++;
i++;
}
}
return result;
}
};
376
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
int n = nums.size();
if (n==1) return 1;
int prediff = 0;
int curdiff = 0;
int result = 1;
for(int i =0;i<n-1;i++)
{
curdiff = nums[i+1]-nums[i];
if(prediff>=0&&curdiff<0||prediff<=0&&curdiff>0){
result++;
prediff = curdiff;
}
}
return result;
}
};
53
动态规划:
class Solution {
public:
int maxSubArray(vector<int>& nums) {
//以 nums[i] 为结尾的「最大子数组和」为 dp[i]
int n = nums.size();
if(n==0) return 0;
vector<int> dp(n);
dp[0] = nums[0];
int result = dp[0];
for(int i=1;i<n;i++){
dp[i] = max(nums[i],dp[i-1]+nums[i]);
result = max(result,dp[i]);
}
return result;
}
};
贪心:
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n = nums.size();
if(n==0) return 0;
int result = nums[0];
int cur = 0;
for (int i=0;i<n;i++){
cur+=nums[i];
if(cur>result){
result = cur;
}
if(cur<0){
cur = 0;
}
}
return result;
}
};
-
自己实现过程中遇到哪些困难
-
今日收获,记录一下自己的学习时长