力扣题目训练(18)

本文介绍了2024年2月11日的力扣编程训练中的五道题目,包括数组拆分、重塑矩阵、判断另一棵树的子树、丑数II和H指数计算,以及难度较高的单词接龙,展示了编程思路和代码实现。
摘要由CSDN通过智能技术生成

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;
    }
};

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;
    }
};

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);
    }
};

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];
    }
};

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;
    }
};

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;

    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值