头像

yxc

北京大学


访客:1282820

在线 



yxc
20小时前

本题是special judge,虽然在标准答案里每行第一个数是整行的数的个数,但大家在输出时不要输出数的个数,输出格式要跟题目描述中给出的样例保持一致。




yxc
1天前

本题要求将二叉搜索树改成双向链表。

LeetCode 426题要求将二叉搜索树改成双向 循环 链表。

所以本题的代码需要先将返回的双向链表的首位相连,才可以在lc上通过。



活动打卡代码 LeetCode 1452. 收藏清单

yxc
1天前
class Solution {
public:
    vector<int> peopleIndexes(vector<vector<string>>& strs) {
        int n = strs.size();
        for (auto &l : strs) sort(l.begin(), l.end());

        vector<int> res;
        for (int i = 0; i < n; i ++ )
        {
            bool is_subset = false;
            for (int j = 0; j < n; j ++ )
                if (i != j)
                {
                    auto &A = strs[i], &B = strs[j];
                    int a = 0;
                    for (int b = 0; b < B.size() && a < A.size(); b ++ )
                        if (A[a] == B[b])
                            a ++ ;

                    if (a == A.size())
                    {
                        is_subset = true;
                        break;
                    }
                }

            if (!is_subset) res.push_back(i);
        }

        return res;
    }
};



yxc
1天前
class Solution {
public:
    string arrangeWords(string text) {
        stringstream ssin(text);
        vector<string> words;
        string word;
        while (ssin >> word) words.push_back(word);
        words[0][0] = tolower(words[0][0]);
        stable_sort(words.begin(), words.end(), [](string a, string b) {
            return a.size() < b.size();
        });
        words[0][0] = toupper(words[0][0]);

        string res;
        for (auto word : words) res += word + ' ';
        res.pop_back();
        return res;
    }
};



yxc
1天前
class Solution {
public:
    int busyStudent(vector<int>& startTime, vector<int>& endTime, int queryTime) {
        int res = 0;
        for (int i = 0; i < startTime.size(); i ++ )
            if (queryTime >= startTime[i] && queryTime <= endTime[i])
                res ++ ;
        return res;
    }
};



yxc
3天前
class Solution {
public:
    int maxDotProduct(vector<int>& nums1, vector<int>& nums2) {
        int n = nums1.size(), m = nums2.size();
        vector<vector<int>> f(n + 1, vector<int>(m + 1, -0x3f3f3f3f));

        for (int i = 0; i <= n; i ++ ) f[i][0] = 0;
        for (int i = 0; i <= m; i ++ ) f[0][i] = 0;

        int res = INT_MIN;
        for (int i = 1; i <= n; i ++ )
            for (int j = 1; j <= m; j ++ )
            {
                f[i][j] = max(f[i - 1][j], f[i][j - 1]);
                int t = f[i - 1][j - 1] + nums1[i - 1] * nums2[j - 1];
                res = max(res, t);
                f[i][j] = max(f[i][j], t);
            }

        return res;
    }
};



yxc
3天前
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    unordered_map<int, int> cnt;

    int pseudoPalindromicPaths (TreeNode* root) {
        cnt[root->val] ++ ;
        if (!root->left && !root->right) {
            int t = 0;
            for (auto item : cnt)
                if (item.second % 2)
                    t ++ ;

            cnt[root->val] -- ;
            return t <= 1;
        }

        int res = 0;
        if (root->left) res += pseudoPalindromicPaths(root->left);
        if (root->right) res += pseudoPalindromicPaths(root->right);

        cnt[root->val] -- ;
        return res;
    }
};



yxc
3天前
class Solution {
public:
    int maxVowels(string s, int k) {
        int res = 0;
        unordered_set<char> S({'a', 'e', 'i', 'o', 'u'});
        for (int i = 0, cnt = 0; i < s.size(); i ++ )
        {
            cnt += S.count(s[i]);
            if (i >= k) cnt -= S.count(s[i - k]);
            if (i >= k - 1) res = max(res, cnt);
        }

        return res;
    }
};



yxc
3天前
class Solution {
public:
    int isPrefixOfWord(string sentence, string searchWord) {
        stringstream ssin(sentence);

        string word;
        for (int i = 1; ssin >> word; i ++ )
            if (word.substr(0, searchWord.size()) == searchWord)
                return i;
        return -1;
    }
};


活动打卡代码 AcWing 1525. 独一无二

yxc
4天前
#include <iostream>

using namespace std;

const int N = 100010;

int n;
int a[N], c[N];

int main()
{
    cin >> n;
    for (int i = 0; i < n; i ++ )
    {
        cin >> a[i];
        c[a[i]] ++ ;
    }

    for (int i = 0; i < n; i ++ )
        if (c[a[i]] == 1)
        {
            cout << a[i] << endl;
            return 0;
        }

    puts("None");
    return 0;
}