package com.fishercoder.solutions;

/**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 _311 {

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

}

@@ -32,16 +32,60 @@
 ns;%0A%0A/**
+%0A * 311. Sparse Matrix Multiplication%0A *%0A * 
 Given tw
@@ -304,17 +304,16 @@
 1 %5D%0A %5D%0A%0A
-%0A
       %7C 
@@ -416,16 +416,19 @@
  0 0 1 %7C
+%0A%0A 
 */%0Apubli
@@ -443,16 +443,56 @@
 _311 %7B%0A%0A
+    public static class Solution1 %7B%0A    
     publ
@@ -539,24 +539,28 @@
 ) %7B%0A        
+    
 int m = A.le
@@ -569,24 +569,28 @@
 th;%0A        
+   
+ 
 int n = A%5B0%5D
@@ -590,32 +590,36 @@
  = A%5B0%5D.length;%0A
+    
         int p = 
@@ -639,16 +639,20 @@
         
+  
+  
 int%5B%5D%5B%5D 
@@ -670,16 +670,20 @@
 %5Bm%5D%5Bp%5D;%0A
+    
         
@@ -716,32 +716,36 @@
 ) %7B%0A            
+ 
+   
 for (int j = 0; 
@@ -754,24 +754,28 @@
 %3C n; j++) %7B%0A
+    
             
@@ -810,32 +810,36 @@
                 
+    
 for (int k = 0; 
@@ -872,24 +872,28 @@
             
+   
+ 
 if (B%5Bj%5D%5Bk%5D 
@@ -892,32 +892,36 @@
 B%5Bj%5D%5Bk%5D != 0) %7B%0A
+    
                 
@@ -962,16 +962,46 @@
 %5Bj%5D%5Bk%5D;%0A
+                            %7D%0A
         
@@ -1072,30 +1072,24 @@
       %7D%0A    
-    %7D%0A
         retu
@@ -1098,13 +1098,22 @@
  C;%0A    
+    %7D%0A    
 %7D
-%0A
 %0A%7D%0A

refactor 311