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 ...
随机推荐
- 张高兴的大模型开发实战:(五)使用 LLaMA Factory 微调与量化模型并部署至 Ollama
目录 环境搭建与配置 数据集准备 WebUI 配置微调参数 模型导出与量化 导入 Ollama LLaMA Factory 是一个开源的全栈大模型微调框架,简化和加速大型语言模型的训练.微调和部署流程 ...
- SparkSql 写 Es
官方文档 key 备注 es.write.operation index(默认)添加新数据,旧数据被替换,重新索引;create添加新数据,数据存在抛出异常;update 更新现有数据,不存在抛出异常 ...
- MCP 实践系列:看热点、蹭热点,创作与摸鱼两手抓!
连续工作累死人,身心疲惫时,总得有那么一点时间给自己松口气.每当这个时候,我总喜欢偷偷摸摸地看看新闻,整理一下逐渐疯狂的思维.毕竟,谁说程序员就只能埋头写代码?谁规定了只能死磕在堆积如山的bug中? ...
- SpringBoot 在IDEA中用MAVEN打包报错
今天在打包的时候遇到一个报错,项目可以正常运行但是就是无法使用MAVEN 的insert和package进行打包 报错如下 [ERROR] Tests run: 1, Failures: 0, Err ...
- C#反射报错之System.Reflection.AmbiguousMatchException:“Ambiguous match found.
.NET6 Type t = typeof(double).GetMethod("TryParse").GetParameters()[1].ParameterType; Cons ...
- PyPI 使用的国内源
通过几次 pip 的使用,对于默认的 pip 源的速度实在无法忍受,于是便搜集了一些国内的pip源,如下:阿里云 http://mirrors.aliyun.com/pypi/simple/中国科技大 ...
- Pytorch之Tensor学习
Pytorch之Tensor学习 Tensors是与数组和矩阵类似的数据结构,比如它与numpy 的ndarray类似,但tensors可以在GPU上运行.实际上,tensors和numpy数组经常共 ...
- jdk11.0.2下载安装-环境配置
下载 JDK下载地址https://www.oracle.com/technetwork/java/javase/downloads/index.html 然后同意,选择自己适合的版本 PS: 1.z ...
- 基于FPGA的超声波雷达感应预警系统 全过程记录
FPGA系统开发 综合实验记录 实验时间节点与想法记录 2023.4.24 新建本文档.目前决定有以下两个方案,要根据学校发的器件和自己的水平和后面时间决定. 课设想法 具体情况 基于FPGA的高速运 ...
- .NET周刊【5月第3期 2025-05-18】
国内文章 理解 C# 中的各类指针 https://www.cnblogs.com/eventhorizon/p/18873400 本文介绍了C#中的各种指针,包括对象引用.指针.IntPtr.函数指 ...