leetcode 1605
简介
leetcode 1605
思路:代码抄的,没啥想法
例如一个3*3的矩阵求解,我们已知6个方程,但是这个矩阵有9个变量,如何求解?注定是不能求出唯一解的,如果不是限定在非负整数数组范围内,那么就有无穷多解。
作者使用了贪心的策略,因为这题后面解的变动不会对前面的已经使用贪心策略的解产生变动,而且可以很快求出解,逻辑上可以使用贪心。
参考链接
https://github.com/haoel/leetcode
推荐个人的刷题框架(当然有新的想法我会合并的哈哈哈哈)
https://github.com/lishaohsuai/leetCode
code
#include <vector>
#include <queue>
using namespace std;
class Solution1605{
public:
vector<vector<int>> restoreMatrix(vector<int>& row, vector<int>& col){
int n = row.size();
int m = col.size();
if(n==0 || m ==0){
return {};
}
vector<vector<int>> res(n, vector<int>(m,0));
queue<pair<int, int>> p,q;
for(int i=0; i<n; i++){
p.push({row[i],i});
}
for(int j=0; j<m; j++){
q.push({col[j],j});
}
while(!q.empty() &&!p.empty()){
auto a = p.front();
auto b = q.front();
p.pop();
q.pop();
int t = min(a.first, b.first);
res[a.second][b.second] = t;
a.first -= t;
b.first -= t;
if(a.first > 0)
p.push(a);
if(b.first > 0)
q.push(b);
}
return res;
}
};
leetcode 1605的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- GC垃圾收集时,居然还有用户线程在奔跑
之前面试被问到过"当GC垃圾收集时,是所有的用户线程都停止了吗?",这一篇我们来探究一下这个问题. 其实执行本地代码的线程仍然可以运行,那么这些线程一旦改变了对象中的引用关系或创建 ...
- mysql8.0.16 设置远程主机访问
新版的的mysql版本已经将创建账户和赋予权限的方式分开了 1.创建账户 create user 'root'@'%' identified by '123456'; 注意密码是否符合要求,我用的阿里 ...
- springboot加载配置文件的优先级
如果springboot有多个配置文件,则加载顺序为项目根路径下的/config > 项目根路径 > resources/config > resources目录下 图示: 如果两个 ...
- 【HUST】网安|操作系统实验|实验二 进程管理与死锁
目的 1)理解进程/线程的概念和应用编程过程: 2)理解进程/线程的同步机制和应用编程: 任务 1)在Linux下创建一对父子进程. 2)在Linux下创建2个线程A和B,循环输出数据或字符串. 3) ...
- SQL 强化练习(三)
继续来练习 sql 查询, 似乎也没有什么窍门, 跟着写多了, 自然就记住了, 这个帖子, 来记录一波, 模糊查询 like; 四表关联查询: 老师名 -> 老师id -> 课程id -. ...
- Varlet UI-Material Design风格Vue 3框架移动端组件库
Varlet UI是什么 在现代Web开发中,Vue 3以其强大的组件系统特性,成为了构建可复用.模块化应用界面的首选框架.而在Vue 3的生态系统中,Varlet UI开源组件库以其高效.一致和可维 ...
- 干货分享:Dify中4种核心变量详解!
在 Dify 工作流(Workflow 和 Chatflow)的实现中,"变量"做为最基础.最核心的组件发挥着不可或缺的作用,因为它承载了不同节点间数据传递的作用. 也就是说,我们 ...
- 树形DP和状压DP
P1352 没有上司的舞会 作为一道经典例题,几乎学树形 \(DP\) 就得先做它. 设 \(f[i][j]\) ,当 \(j\) 为 \(0\) 时表示第 \(i\) 个人不来,当 \(j\) 为 ...
- centos 7 安装 netcoresdk 和Nginx 并发布netcore
微软官网的yum安装: 打开linux终端程序 netcore sdk 地址https://dotnet.microsoft.com/download/linux-package-manager/ce ...
- RabbitMq使用中常见错误--python版
用python的pika库错误集 一.pika.exceptions.ProbableAuthenticationError: ConnectionClosedByBroker: (403) 'ACC ...