2021/8/29

笔试一场

两题leetcode

学习QUIC协议

GCC工具的调式

class Solution {
public:
    int longestValidParentheses(string s) {
        int dp[30100] = {0};int ans = 0;
        for(int i = 0; i < s.size(); i ++){
            if(s[i] == '(') dp[i] = 0;
            else {
                if(i - 1 >= 0 && s[i - 1] == '(') {
                    if(i - 2 >= 0)
                    dp[i] = dp[i - 2] + 2;
                    else dp[i] = 2;
                } else if(i - 1 >= 0 && i - 1 - dp[i - 1] >= 0 && s[i - 1 - dp[i - 1]] == '(' && s[i - 1] == ')'){
                    dp[i] = dp[i - 1] + 2;
                    if(i - 1 - dp[i - 1] > 0) dp[i] += dp[i - 1 - dp[i - 1] - 1];
                }

            }
            ans = max(ans,dp[i]);
        }
        return ans;
    }
};

  

class Solution {
public:
    int dp[100100];
    int candy(vector<int>& ratings) {
        int L = 0,R = 1,ans = 0,pre = 0;
        pre = 1;ans += 1;
        for(int i = 1; i < ratings.size(); i ++){
            if(ratings[i] >= ratings[i - 1]){
                L = 0; 
                if(ratings[i] == ratings[i - 1]) {
                    pre = 1; 
                } else pre += 1;
                ans += pre;
                R = pre;
            } else {
                L ++;
                if(L == R){
                    L ++;
                }
                ans += L;
                pre = 1;
            }
        }
        return ans;
    }
};

  

 

上一篇:Java 面试题(三)


下一篇:C语言程序设计100例之(49):序列变换