头像

相得益彰




在线 


最近来访(12)
用户头像
是这样的捏
用户头像
陌上花开Charlie
用户头像
SIFLY
用户头像
辛白林
用户头像
真睡不醒
用户头像
三岁不吃糖
用户头像
nosmile

活动打卡代码 LeetCode 15. 三数之和

相得益彰
28分钟前
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> res;
        sort(nums.begin(), nums.end());
        for (int i = 0; i < nums.size(); i ++ ) {
            if (i && nums[i] == nums[i - 1]) continue;
            for (int j = i + 1, k = nums.size() - 1; j < k; j ++ ) {
                if (j > i + 1 && nums[j] == nums[j - 1]) continue;
                while (j < k - 1 && nums[i] + nums[j] + nums[k - 1] >= 0) k -- ;
                if (nums[i] + nums[j] + nums[k] == 0) {
                    res.push_back({nums[i], nums[j], nums[k]});
                }
            }
        }

        return res;
    }
};


活动打卡代码 AcWing 3311. 最长算术

相得益彰
6小时前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 200010;
int n;
int w[N];

int main()
{
    int T;
    scanf("%d", &T);
    for(int cases = 1; cases <= T; cases ++)
    {
        scanf("%d", &n);
        for(int i = 0; i < n; i ++) scanf("%d",&w[i]);
        int res = 0;

        for(int i = 0; i < n; i ++)
        {
            int j = i + 2;
            while(j < n && w[j] - w[j - 1] == w[j - 1] - w[j - 2]) j ++;
            res = max(res, j - i);
            i = j -2;
        }
        printf("Case #%d: %d\n", cases, res);
    }
    return 0;
}


活动打卡代码 AcWing 691. 立方体IV

相得益彰
6小时前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1010;

int n;
int g[N][N], f[N][N];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

int dp(int x, int y)
{
    int& v = f[x][y];
    if(v != -1) return v;

    v = 1;
    for(int i = 0; i < 4; i ++)
    {
        int a = x + dx[i], b = y + dy[i];
        if(a >= 0 && a < n && b >= 0 && b < n && g[a][b] == g[x][y] + 1)
            v = max(v, dp(a, b) + 1);
    }
    return v;
}
int main()
{
    int T;
    scanf("%d", &T);
    for(int cases = 1; cases <= T; cases ++)
    {
        scanf("%d", &n);
        for(int i = 0; i < n; i ++)
            for(int j = 0; j < n; j ++)
                scanf("%d", &g[i][j]);
        memset(f, -1, sizeof f);

        int id, cnt = 0;
        for(int i = 0; i < n; i ++)
            for(int j = 0; j < n; j ++)
            {
                int t = dp(i, j);
                if(t > cnt || t == cnt && id > g[i][j])
                {
                    id = g[i][j];
                    cnt = t;
                }
            }
        printf("Case #%d: %d %d\n", cases, id, cnt);
    }
    return 0;
}


活动打卡代码 LeetCode 1282. 用户分组

相得益彰
7小时前
class Solution {
public:
    vector<vector<int>> groupThePeople(vector<int>& g) {
        map<int, vector<int>> vt;
        vector<vector<int>> res;
        for(int i = 0; i < g.size(); i ++)
            vt[g[i]].push_back(i);

        for(auto& [x, t] : vt)
        {
            int v = t.size(), i = 0;
            while(i != t.size())
            {
                vector<int> temp;

                for(int k = 0; k < x; k ++)
                    temp.push_back(t[i ++]);
                res.push_back(temp);
            }
        }
        return res;
    }
};


活动打卡代码 LeetCode 14. 最长公共前缀

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        string res;
        if(strs.empty()) return res;

        for(int i = 0; ; i ++)
        {

            if(i >= strs[0].size()) return res;
            char c = strs[0][i];
            for(auto& str : strs)
                if(str.size() <= i || str[i] != c)
                    return res;
            res += c;
        }
        return res;
    }
};



class Solution {
public:
    int romanToInt(string s) {
        unordered_map<char, int> hash;
        hash['I'] = 1, hash['V'] = 5;
        hash['X'] = 10, hash['L'] = 50;
        hash['C'] = 100, hash['D'] = 500;
        hash['M'] = 1000;

        int res = 0;
        for(int i = 0; i < s.size(); i ++)
        {
            if(i + 1 < s.size() && hash[s[i]] < hash[s[i + 1]])
                res -= hash[s[i]];
            else
                res += hash[s[i]];
        }
        return res;
    }
};



class Solution {
public:
    string intToRoman(int num) {
        int values[] = {
            1000,
            900, 500, 400, 100,
            90, 50, 40, 10,
            9, 5, 4, 1,
        };
        string resps[] = {
            "M",
            "CM", "D", "CD", "C",
            "XC", "L", "XL", "X",
            "IX", "V", "IV", "I",

        };
        string res;
        for(int i = 0; i < 13; i ++)
        {
            while(num >= values[i])
            {
                num -= values[i];
                res += resps[i];
            }
        }
        return res;
    }
};



class Solution {
public:
    int maxArea(vector<int>& height) {
        int res = 0;
        for(int i = 0, j = height.size() - 1; i < j;)
        {
            res = max(res, min(height[i], height[j] )* (j - i));
            if(height[i] > height[j]) j --;
            else i ++;
        }
        return res;
    }
};



活动打卡代码 AcWing 2058. 笨拙的手指

#include<iostream>
#include<cstring>
#include<algorithm>
#include <unordered_set>

using namespace std;

int get(string s, int b)
{
    int res = 0;
    for(auto c : s)
        res = res * b + c - '0';
    return res;
}

int main()
{
    string a,b;
    cin >> a >> b;

    unordered_set <int> S;
    for(auto& c : a)
    {
        c ^= 1;
        S.insert(get (a, 2));
        c ^= 1;
    }
    for(auto& c : b)
    {
        char t = c;
        for(int i = 0; i < 3; i ++)
            if(i + '0' != t)
            {
                c = i + '0';
                int x = get (b, 3);
                if(S.count(x))
                {
                    cout << x << endl;
                    return 0;
                }
            }
            c = t;
    }
    return 0;
}





class Solution {
public:
    bool isMatch(string s, string p) {
        int n = s.size(), m = p.size();
        s = ' ' + s, p = ' ' + p;
        vector<vector<bool>> f(n + 1, vector<bool>(m + 1));
        f[0][0] = true;
        for (int i = 0; i <= n; i ++ )
            for (int j = 1; j <= m; j ++ ) {
                if (j + 1 <= m && p[j + 1] == '*') continue;
                if (i && p[j] != '*') {
                    f[i][j] = f[i - 1][j - 1] && (s[i] == p[j] || p[j] == '.');
                } else if (p[j] == '*') {
                    f[i][j] = f[i][j - 2] || i && f[i - 1][j] && (s[i] == p[j - 1] || p[j - 1] == '.');
                }
            }

        return f[n][m];
    }
};