Skip to content

Commit d4233aa

Browse files
sparse matrix multiplication
1 parent c11e205 commit d4233aa

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package medium;
2+
3+
public class SparseMatrixMultiplication {
4+
5+
public int[][] multiply(int[][] A, int[][] B) {
6+
int m = A.length, n = A[0].length, p = B[0].length;
7+
int[][] C = new int[m][p];
8+
for(int i = 0; i < m; i++){
9+
for(int j = 0; j < n; j++){
10+
if(A[i][j] != 0){
11+
for(int k = 0; k < p; k++){
12+
if(B[j][k] != 0) C[i][k] += A[i][j]*B[j][k];
13+
}
14+
}
15+
}
16+
}
17+
return C;
18+
}
19+
20+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
|338|[Counting Bits](https://leetcode.com/problems/counting-bits/)|[Solution](../../blob/master/MEDIUM/src/medium/CountingBits.java)| O(n)|O(n) | Medium|
4242
|325|[Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/)|[Solution] | O(n)|O(n) | Medium| HashMap
4343
|314|[Binary Tree Vertical Order Traversal](https://leetcode.com/problems/binary-tree-vertical-order-traversal/)|[Solution](../../blob/master/MEDIUM/src/medium/BinaryTreeVerticalOrderTraversal.java)| O(n)|O(n) | Medium| HashMap, BFS
44+
|311|[Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/)|[Solution](../../blob/master/MEDIUM/src/medium/SparseMatrixMultiplication.java)| O(m*n*l)|O(m*l)| Medium|
4445
|301|[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)|[Solution]| ? | ? | Hard| BFS
4546
|299|[Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/)|[Solution](../../blob/master/EASY/src/easy/BullsandCows.java)| O(n)|O(1) | Easy|
4647
|295|[Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/)|[Solution](../../blob/master/HARD/src/hard/FindMedianFromDataStream.java)| O(nlogn) | O(n) | Hard| Heap

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy