FLAG必经之路

# Sparse Matrix Multiplication

Given two sparse matrices A and B, return the result of AB.

You may assume that A's column number is equal to B's row number.

Example:

A = [
[ 1, 0, 0],
[-1, 0, 3]
]

B = [
[ 7, 0, 0 ],
[ 0, 0, 0 ],
[ 0, 0, 1 ]
]

|  1 0 0 |   | 7 0 0 |   |  7 0 0 |
AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
| 0 0 1 |

public class Solution {
public int[][] multiply(int[][] A, int[][] B) {
int[][] C = new int[A.length][B[0].length];
for(int i=0; i<A.length; i++){
for(int k=0; k<A[0].length; k++){
if(A[i][k] != 0) {
for(int j=0; j<B[0].length; j++){
if(B[k][j] != 0){
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
}
return C;
}
}

public class Solution {

class Node{
int x;
int y;
public Node(int x, int y){
this.x = x;
this.y = y;
}
}

public int[][] multiply(int[][] A, int[][] B) {
int[][] C = new int[A.length][B[0].length];
List<Node> listA = new ArrayList<Node>();
List<Node> listB = new ArrayList<Node>();
for(int i=0; i<A.length; i++){
for(int j=0; j<A[0].length; j++){
if(A[i][j]!=0){
}
}
}

for(int i=0; i<B.length; i++){
for(int j=0; j<B[0].length; j++){
if(B[i][j]!=0){
}
}
}

for(Node a: listA){
for(Node b: listB){
if(a.y == b.x){
C[a.x][b.y] += A[a.x][a.y]*B[b.x][b.y];
}
}
}
return C;
}
}

#### [leetcode] 311. Sparse Matrix Multiplication 解题报告

2016-03-13 04:19:12

#### [leetcode 311] Sparse Matrix Multiplication

2015-12-29 22:45:36

#### LeetCode 311. Sparse Matrix Multiplication（稀疏矩阵相乘）

2016-04-21 08:12:23

#### 3318 Matrix Multiplication 随机算法

2008-11-18 23:09:00

#### 动态规划 (Dynamic Programming) 之 矩阵链乘法(Matrix Chain Multiplication)

2009-02-27 15:36:00

#### Matrix Chain Multiplication（栈的简单应用）

2013-01-12 11:57:39

#### CUDA Samples: matrix multiplication(C = A * B)

2017-08-03 13:32:13

#### 矩阵链乘法问题描述(Matrix-chain multiplication)

2015-09-13 14:44:32

#### C++作业9.|Matrix chain multiplication

2016-11-29 19:14:59

#### HDU 1082.Matrix Chain Multiplication【栈的应用+矩阵乘法规则】【1月4】

2016-01-04 23:06:25