<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 ...
随机推荐
- 线程join方法详解
执行逻辑:在当前代码块(比如main方法)中的线程A执行了join方法, 那么当代码块(main)执行到join方法时,会停止继续向下执行,一直到线程A执行完毕, main方法才会继续向下执行. 代码 ...
- English: Class GXX
######################################## GGGGG GG GG GG GG GGGG GG GGG GGGGGG author:enomothem date: ...
- 白话 MVC、MVP、MVVP
白话 MVC.MVP.MVVP 注意这里单纯的通过例子来讲解 MVC MVP MVVP 这三种架构模式的起源和作用,不牵扯某种特定的语言.具体到各种语言各种软件系统上体现有所不同,但是原理都是这样的. ...
- n个数字相加
求s=a+aa+aaa+aaaa+aa...a的值 其中a是一个数字,多少个数字相加由键盘输入控制 a = int(input("数字:")) count = int(input( ...
- Lua 5.1 学习笔记
1 简介 2 语法 2.1 语法约定 2.1.1 保留关键字 2.1.2 操作符 2.1.3 字符串定义 2.2 值与类型 2.2.1 强制转换 2.3 变量 2.3.1 索引 2.3.2 环境表 2 ...
- jenkins实现git自动拉取代码时替换配置文件
jenkins实现从git上自动拉取源代码——>自动编译——>发布到测试服务器——>验证测试,这个大家应该都知道,但是关于源代码里的配置文件,可能就会有点头疼了, 一般测试都会自己的 ...
- Python元组是什么
引出 在使用Python过程中,列表.集合和字典是比较常用的数据结构. 列表简单说就是数组,不对,它就是数组 集合就是去重的元素结构,和JAVA中的set一样 字典就是一个key-value的键值对, ...
- 无需重新安装软件,将软件从C盘移至其他盘(mklink)
事先说明,这篇博文算是视频的文字转载版,来源为该视频的五分钟后内容→https://www.bilibili.com/video/av78397784/ 如何拯救标红的C盘[高玩] 有时候安装软件会手 ...
- python 读取ini 配置文件
安装 pip install configparser 1 配置文件 config.ini: [MysqlDB]user=rootpasswd=123456sport=3306db_name=my_d ...
- queue队列基础讲解
前言 似乎这种对蒟蒻最重要的概念都搜不到,对巨佬来说也根本不必要提及. 导致我也不懂. Queue 意义 queue,队列,一种数据结构. 队列是一种操作受限制的线性表: 特点: 1.元素先进先出. ...