如何看网站是用什么语言做的,vr全景网站怎么做,室内设计基础知识点,网站加强队伍建设2024年2月11日力扣题目训练 2024年2月11日力扣题目训练561. 数组拆分566. 重塑矩阵572. 另一棵树的子树264. 丑数 II274. H 指数127. 单词接龙 2024年2月11日力扣题目训练
2024年2月11日第十八天编程训练#xff0c;今天主要是进行一些题训练#xff0c;包括简单题3道、中等… 2024年2月11日力扣题目训练 2024年2月11日力扣题目训练561. 数组拆分566. 重塑矩阵572. 另一棵树的子树264. 丑数 II274. H 指数127. 单词接龙 2024年2月11日力扣题目训练
2024年2月11日第十八天编程训练今天主要是进行一些题训练包括简单题3道、中等题2道和困难题1道。惰性太强现在才完成不过之后我会认真完成的,我会慢慢补回来争取一天发两篇把之前的都补上。
561. 数组拆分
链接: 数组拆分 难度 简单 题目
运行示例
思路 这道题就是排序让小的和小的组成一对从而能满足题意。 代码
class Solution {
public:int arrayPairSum(vectorint nums) {sort(nums.begin(),nums.end());int sum 0;for(int i 0; i nums.size(); i2){sum nums[i];}return sum;}
};566. 重塑矩阵
链接: 重塑矩阵 难度 简单 题目
运行示例
思路 这道题类似于二维数组的一维表示。 (i,j)→i×nj ix / n jx % n
代码
class Solution {
public:vectorvectorint matrixReshape(vectorvectorint nums, int r, int c) {int m nums.size();int n nums[0].size();if (m * n ! r * c) {return nums;}vectorvectorint ans(r, vectorint(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)||(!ot)||(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;vectorint 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(vectorint 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_mapstring, setstring from,
vectorstring 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, vectorstring wordList) {vectorvectorstring res;int ans INT_MAX;// 因为需要快速判断扩展出的单词是否在 wordList 里因此需要将 wordList 存入哈希表这里命名为「字典」unordered_setstring dict {wordList.begin(), wordList.end()};// 修改以后看一下如果根本就不在 dict 里面跳过if (dict.find(endWord) dict.end()) {return 0;}// 特殊用例处理dict.erase(beginWord);// 第 1 步广度优先搜索建图// 记录扩展出的单词是在第几次扩展的时候得到的key单词value在广度优先搜索的第几层unordered_mapstring, int steps {{beginWord, 0}};// 记录了单词是从哪些单词扩展而来key单词value单词列表这些单词可以变换到 key 它们是一对多关系unordered_mapstring, setstring from {{beginWord, {}}};int step 0;bool found false;queuestring q queuestring{{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 的 stepsteps[nextWord] step;if (nextWord endWord) {found true;}}nextWord[j] origin;}}if (found) {break;}}// 第 2 步回溯找到所有解从 endWord 恢复到 beginWord 所以每次尝试操作 path 列表的头部if (found) {vectorstring Path {endWord};backtrack(ans, endWord, from, Path);}return ans INT_MAX ? 0:ans;}
};