<Matrix> 311 378
311. Sparse Matrix Multiplication
稀疏矩阵的计算。稀疏矩阵的特点是有大量的0,如果采用暴力算法则比然会有很多无意义的计算。
C[ i ][ j ] += A[ i ] [ k ] * B[ k ] [ j ]
我们首先遍历A数组,要确保A[i][k]不为0,才继续计算,然后我们遍历B矩阵的第k行,如果B[K][J]不为0,我们累加结果矩阵res[i][j] += A[i][k] * B[k][j]
class Solution {
public int[][] multiply(int[][] A, int[][] B) {
int m = A.length, n = A[0].length, nB = B[0].length;
int[][] C = new int[m][nB]; for(int i = 0; i < m; i++){
for(int k = 0; k < n; k++){
if(A[i][k] != 0){
for(int j = 0; j < nB; j++){
if(B[k][j] != 0) C[i][j] += A[i][k] * B[k][j];
}
}
}
}
return C;
}
}
378. Kth Smallest Element in a Sorted Matrix
class Solution {
public int kthSmallest(int[][] matrix, int k) {
int lo = matrix[0][0], hi = matrix[matrix.length - 1][matrix[0].length - 1] + 1; while(lo < hi){
int count = 0, j = matrix[0].length - 1;
int mid = lo + (hi - lo) / 2;
for(int i = 0; i < matrix.length; i++){
while(j >= 0 && matrix[i][j] > mid) j--;
count += (j + 1);
}
if(count < k) lo = mid + 1;
else hi = mid;
}
return lo;
}
}
<Matrix> 311 378的更多相关文章
- [LeetCode] 378. Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13
378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- Leetcode:378. Kth Smallest Element in a Sorted Matrix
题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the ...
- 378. Kth Smallest Element in a Sorted Matrix
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- 311. Sparse Matrix Multiplication
题目: Given two sparse matrices A and B, return the result of AB. You may assume that A's column numbe ...
- 378. Kth Smallest Element in a Sorted Matrix(java,优先队列)
题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the ...
- [leetcode]311. Sparse Matrix Multiplication 稀疏矩阵相乘
Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is ...
- 【Leetcode】378. Kth Smallest Element in a Sorted Matrix
Question: Given a n x n matrix where each of the rows and columns are sorted in ascending order, fin ...
随机推荐
- JS---DOM---为元素绑定事件的引入,为元素绑定多个代码,兼容代码
1. 为元素绑定事件的引入: 用src直接绑定多个,只实现最后一个(programmer2.js) <input type="button" value="按钮&q ...
- Pumpkin Raising Walk Through
概述: 这个靶机的规则是根据提示获取南瓜的seed,然后根据一次获取的seed 登录服务器并完成提权,里面涉及到一些CTF的知识,加密解密,提权! 主机端口扫描: ╰─ nmap -p1-65535 ...
- Redhat Linx使用Centos yum源
一.故障现象: 在安装了Read linux后,使用yum安装软件,出现如下提示:[root@localhost~]# yum install xxxLoaded plugins: product-i ...
- pycharm连接远程python的开发环境
一.准备工作 1.远程主机配置python的开发环境(pyenv) 2.本地windowsp安装pycharm 二.配置pycharm连接 配置运行环境:
- VMware Workstation下载-安装-破解-秘钥
永不过期序列号:UZ792-DHF8J-M81XP-MGM5T-MCAF2 Vmware15注册机下载:链接: https://pan.baidu.com/s/1KbLq71tw_5pUKv2lRjF ...
- 《Google软件测试之道》
Google软件测试之道 Google对质量的理解 质量不等于测试,即质量不是被测出来的 开发和测试应该并肩齐驱,测试就是开发过程中不可缺少的一部分 质量是一种预防行为而不是检测 Google对软件测 ...
- ReactNative: 使用输入框TextInput组件
一.简介 一个应用程序中,输入框基本不可或缺,它衍生的搜索功能在很多APP中随处可见.在iOS开发中,使用的输入框组件是UITextView和UITextField,在React-Native中使用的 ...
- SpringBoot源码学习系列之@PropertySource不支持yaml读取原因
然后,为什么@PropertySource注解默认不支持?可以简单跟一下源码 @PropertySource源码: 根据注释,默认使用DefaultPropertySourceFactory类作为资源 ...
- 2019年最新阿里Java工程师面试题
一.单选题(共10题,每题5分) 1 关于设计模式遵循的原则,说法错误的是? A.组合优于继承 B.针对实现编程 C.对扩展开放,对修改关闭 D.降低对象之间的耦合 参考答案:B 答案解析: 设计 ...
- form表单中的button自动刷新页面问题
form表单中如果存在button的话,有可能会出现一个问题:点击button,触发了页面的自动刷新事件. 原因是因为<button>标签默认的类型是submit,即默认的button点击 ...