简介

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的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [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 ...

  4. 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 ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. python aiohttp异步协程实现同时执行多条请求

    我们在对多个链接进行处理的时候,往往是先请求一个链接获得数据后,再请求第二个. 中间在等待返回数据时候,存在一个空闲时间,脚本啥都没干. 用aiohttp异步协程的方法,创建多条任务发送请求(理论上不 ...

  2. [开源] Layouter(桌面助手)开源发布

    Layouter(桌面助手)是一款简洁.易用.美观的桌面整理软件,基于.net 6开发,支持Windows 7及以上操作系统.以 Apache-2.0 license 进行开源. 开源地址 https ...

  3. react-router-dom嵌套路由实践

    想要通过react-router-dom实现类似vue的router-view的嵌套路由效果,在点击导航菜单时切换页面,官方文档是真的没找到相关内容,现做个总结: 在createBrowserRout ...

  4. mysql免密登录

    开启mysql免密登录, vi /etc/my.cnf [mysqld]下添加 skip-grant-tables , 保存后重启mysql服务:service mysqld restart

  5. 聊聊@Autowired与@Resource的区别

    1. 前言 从事过很多家公司,见过很多项目,发现@Autowired和@Resource的使用都是一样的乱, 一个项目中有使用@Autowired的,有使用@Resource的, 甚至有的类中一会儿使 ...

  6. 集合流之"计算集合中的Integer或Double或BigDecimal的sum总和(累计)"

    一.BigDecimal类型 BigDecimal withdrawalFeeExchange = groupDeverList.stream().map(DevWeekReport::getWith ...

  7. 0.4元/TB/月!天翼云HBlock打响软件定义存储价格战

    惊爆价打响存储战争 当企业数据量以平均每年增加50%的速度狂飙,存储成本已成重负:传统方案动辄数百万的成本投入.动辄数周的部署周期.动辄30%的闲置资源浪费-- 今天,天翼云HBlock以" ...

  8. 网络编程:TCP 网络编程

    参考:盛延敏:网络编程实战 TCP TCP,又被叫做字节流套接字(Stream Socket),UDP 也有一个类似的叫法, 数据报套接字(Datagram Socket),一般分别以"SO ...

  9. java从小白到老白④

    PS:①小陌笔记中蓝色紫色等一切花哨字体皆用来引入知识点(废话流),可忽略不计 . ②黑字正文小陌竭力向言简意赅靠近再靠近. ③红色字体小陌觉得重要的地方 (3)先执行case语句,后再判断defau ...

  10. Spring注解之@Autowired:Setter 方法上使用@Autowired注解

    可以在 JavaBean中的 setter 方法中使用 @Autowired 注解.当 Spring遇到一个在 setter 方法中使用的 @Autowired 注解时,它会在方法中按照类型自动装配参 ...