• 力扣题目训练(18)


    2024年2月11日力扣题目训练

    2024年2月11日第十八天编程训练,今天主要是进行一些题训练,包括简单题3道、中等题2道和困难题1道。惰性太强现在才完成,不过之后我会认真完成的,我会慢慢补回来,争取一天发两篇,把之前的都补上。

    561. 数组拆分

    链接: 数组拆分
    难度: 简单
    题目:
    题目描述

    运行示例:
    运行示例

    思路:
    这道题就是排序,让小的和小的组成一对,从而能满足题意。
    代码:

    class Solution {
    public:
        int arrayPairSum(vector<int>& nums) {
            sort(nums.begin(),nums.end());
            int sum = 0;
            for(int i = 0; i < nums.size(); i+=2){
                sum += nums[i];
            }
            return sum;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    566. 重塑矩阵

    链接: 重塑矩阵
    难度: 简单
    题目:
    题目描述

    运行示例:
    运行示例

    思路:
    这道题类似于二维数组的一维表示。
    (i,j)→i×n+j
    i=x / n
    j=x % n

    代码:

    class Solution {
    public:
        vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
            int m = nums.size();
            int n = nums[0].size();
            if (m * n != r * c) {
                return nums;
            }
    
            vector<vector<int>> ans(r, vector<int>(c));
            for (int x = 0; x < m * n; ++x) {
                ans[x / c][x % c] = nums[x / n][x % n];
            }
            return ans;
        }
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    572. 另一棵树的子树

    链接: 另一棵树的子树
    难度: 简单
    题目:
    题目描述

    运行示例:
    运行示例

    思路:
    这道题就是树的遍历,自顶而下,从该节点判断是否是子树。
    代码:

    class Solution {
    public:
        bool check(TreeNode*o, TreeNode* t){
            if(!o && !t) return true;
            if((o&&!t)||(!o&&t)||(o->val!=t->val)) return false;
            return check(o->left,t->left) && check(o->right,t->right);
        }
         bool dfs(TreeNode *o, TreeNode *t) {
            if (!o) {
                return false;
            }
            return check(o, t) || dfs(o->left, t) || dfs(o->right, t);
        }
    
        bool isSubtree(TreeNode* root, TreeNode* subRoot) {
            return dfs(root,subRoot);
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    264. 丑数 II

    链接: 丑数 II
    难度: 中等
    题目:
    题目描述
    运行示例:
    运行示例
    思路:
    这道题丑数只包含2,3,5的质因子,所以大的丑数是由小的丑数乘以2或3或5得到的。所以我们可以利用三个指针,得到该丑数是小丑数乘以几表示的。
    代码:

    class Solution {
    public:
        int nthUglyNumber(int n) {
            int a = 0;
            int b = 0;
            int c = 0;
            int count = 1;
            vector<int> ans(n);
            ans[0] = 1;
            while(count < n){
                int n1 = ans[a]*2;
                int n2 = ans[b]*3;
                int n3 = ans[c]*5;
                ans[count] = min(min(n1,n2),n3);
                if(n1 == ans[count]) a++;
                if(n2 == ans[count]) b++;
                if(n3 == ans[count]) c++;
                count++;
            }
            return ans[n-1];
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    274. H 指数

    链接: H 指数
    难度: 中等
    题目:
    题目描述

    运行示例:
    运行示例

    思路:
    这道题就是简单的排序,然后进行比较就行。
    代码:

    class Solution {
    public:
        int hIndex(vector<int>& citations) {
            sort(citations.begin(),citations.end());
            int h = 0, i = citations.size() - 1;
            while (i >= 0 && citations[i] > h) {
                h++;
                i--;
            }
            return h;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    127. 单词接龙

    链接: 单词接龙
    难度: 困难
    题目:
    题目描述

    运行示例:
    运行示例

    思路:
    与上次126. 单词接龙 II的类似也是利用的广度优先遍历和建图。
    代码:

    class Solution {
    public:
        void backtrack(int& ans, const string &Node, unordered_map<string, set<string>> &from,
    vector<string> &path) {
            if (from[Node].empty()) {
                ans = ans > path.size()?path.size():ans;
                return;
            }
            for (const string &Parent: from[Node]) {
                path.push_back(Parent);
                backtrack(ans, Parent, from, path);
                path.pop_back();
            }
        }
        int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
            vector<vector<string>> res;
            int ans = INT_MAX;
            // 因为需要快速判断扩展出的单词是否在 wordList 里,因此需要将 wordList 存入哈希表,这里命名为「字典」
            unordered_set<string> dict = {wordList.begin(), wordList.end()};
            // 修改以后看一下,如果根本就不在 dict 里面,跳过
            if (dict.find(endWord) == dict.end()) {
                return 0;
            }
            // 特殊用例处理
            dict.erase(beginWord);
    
            // 第 1 步:广度优先搜索建图
            // 记录扩展出的单词是在第几次扩展的时候得到的,key:单词,value:在广度优先搜索的第几层
            unordered_map<string, int> steps = {{beginWord, 0}};
            // 记录了单词是从哪些单词扩展而来,key:单词,value:单词列表,这些单词可以变换到 key ,它们是一对多关系
            unordered_map<string, set<string>> from = {{beginWord, {}}};
            int step = 0;
            bool found = false;
            queue<string> q = queue<string>{{beginWord}};
            int wordLen = beginWord.length();
            while (!q.empty()) {
                step++;
                int size = q.size();
                for (int i = 0; i < size; i++) {
                    const string currWord = move(q.front());
                    string nextWord = currWord;
                    q.pop();
                    // 将每一位替换成 26 个小写英文字母
                    for (int j = 0; j < wordLen; ++j) {
                        const char origin = nextWord[j];
                        for (char c = 'a'; c <= 'z'; ++c) {
                            nextWord[j] = c;
                            if (steps[nextWord] == step) {
                                from[nextWord].insert(currWord);
                            }
                            if (dict.find(nextWord) == dict.end()) {
                                continue;
                            }
                            // 如果从一个单词扩展出来的单词以前遍历过,距离一定更远,为了避免搜索到已经遍历到,且距离更远的单词,需要将它从 dict 中删除
                            dict.erase(nextWord);
                            // 这一层扩展出的单词进入队列
                            q.push(nextWord);
                            // 记录 nextWord 从 currWord 而来
                            from[nextWord].insert(currWord);
                            // 记录 nextWord 的 step
                            steps[nextWord] = step;
                            if (nextWord == endWord) {
                                found = true;
                            }
                        }
                        nextWord[j] = origin;
                    }
                }
                if (found) {
                    break;
                }
            }
            // 第 2 步:回溯找到所有解,从 endWord 恢复到 beginWord ,所以每次尝试操作 path 列表的头部
            if (found) {
                vector<string> Path = {endWord};
                backtrack(ans, endWord, from, Path);
            }
            return ans == INT_MAX ? 0:ans;
    
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
  • 相关阅读:
    【C++】list 容器的增删改查---模拟实现(图例超详细解析!!!)
    Opencv之Mat常用类成员(一篇就够了)
    计算机网络——传输层
    如何让iOS设备上App定时执行后台任务(上)
    java毕业设计花苑物业综合服务平台mybatis+源码+调试部署+系统+数据库+lw
    如何使用 GPU 访问运行 Docker Compose 容器
    建筑建材行业B2B电子商务网站方案:赋能建材企业转型升级,实现降本提效
    codeforces.com/contest/1649--补题
    UG\NX二次开发 连接曲线、连结曲线 UF_CURVE_auto_join_curves
    提升用户体验,给你的模态弹窗加个小细节
  • 原文地址:https://blog.csdn.net/LOVE_105/article/details/136157105