LeetCode 311. Sparse Matrix Multiplication(稀疏矩阵相乘)

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 equal to B’s row number.Example: Solution Transform B into defaultdict(dict) GPU Solution,来自DeepSeek的总结 根据2025年最新研究进展,GPU处理稀疏矩阵乘法(SpMM/SparseGEMM)的主流算子可分 阅读详情

原题网址:https://leetcode.com/problems/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 j=0; j<A[i].length; j++) {
                if (A[i][j] == 0) continue;
                for(int k=0; k<B[0].length; k++) {
                    if (B[j][k] == 0) continue;
                    C[i][k] += A[i][j] * B[j][k];
                }
            }
        }
        return C;
    }
}
方法二:使用哈希映射来存放非0数字。

public class Solution {
    private Map<Integer, Map<Integer, Integer>> map(int[][] m) {
        Map<Integer, Map<Integer, Integer>> rows = new HashMap<>();
        for(int i=0; i<m.length; i++) {
            for(int j=0; j<m[i].length; j++) {
                if (m[i][j]==0) continue;
                Map<Integer, Integer> cols = rows.get(i);
                if (cols == null) {
                    cols = new HashMap<>();
                    rows.put(i, cols);
                }
                cols.put(j, m[i][j]);
            }
        }
        return rows;
    }
    public int[][] multiply(int[][] A, int[][] B) {
        int[][] C = new int[A.length][B[0].length];
        Map<Integer, Map<Integer, Integer>> arows = map(A);
        Map<Integer, Map<Integer, Integer>> brows = map(B);
        for(int i: arows.keySet()) {
            Map<Integer, Integer> acol = arows.get(i);
            for(int j: acol.keySet()) {
                Map<Integer, Integer> bcol = brows.get(j);
                if (bcol == null) continue;
                int a = acol.get(j);
                for(int l: bcol.keySet()) {
                    C[i][l] += a * bcol.get(l);
                }
            }
        }
        return C;
    }
}


方法三:使用链表存储稀疏矩阵。

public class Solution {
    public int[][] multiply(int[][] A, int[][] B) {
        List<Line> arows = new ArrayList<>();
        Line line = new Line();
        for(int i=0; i<A.length; i++) {
            for(int j=0; j<A[i].length; j++) {
                if (A[i][j] != 0) {
                    line.p1 = i;
                    line.p2.add(j);
                }
            }
            if (line.p1 != -1) {
                arows.add(line);
                line = new Line();
            }
        }
        List<Line> bcols = new ArrayList<>();
        line = new Line();
        for(int j=0; j<B[0].length; j++) {
            for(int i=0; i<B.length; i++) {
                if (B[i][j] != 0) {
                    line.p1 = j;
                    line.p2.add(i);
                }
            }
            if (line.p1 != -1) {
                bcols.add(line);
                line = new Line();
            }
        }
        
        int[][] C = new int[A.length][B[0].length];
        
        for(int i=0; i<arows.size(); i++) {
            for(int j=0; j<bcols.size(); j++) {
                Line arow = arows.get(i);
                Line bcol = bcols.get(j);
                int a=0, b=0;
                while (a<arow.p2.size() && b<bcol.p2.size()) {
                    if (arow.p2.get(a) == bcol.p2.get(b)) {
                        // System.out.printf("arow.p1=%d, bcol.p1=%d, arow.p2.get(%d)=%d, bcol.p2.get(%d)=%d\n", 
                        // arow.p1, bcol.p1, a, arow.p2.get(a), b, bcol.p2.get(b));
                        C[arow.p1][bcol.p1] += A[arow.p1][arow.p2.get(a)] * B[bcol.p2.get(b)][bcol.p1];
                        a ++;
                        b ++;
                    } else if (arow.p2.get(a) > bcol.p2.get(b)) b ++;
                    else a ++;
                }
            }
        }
        
        return C;
    }
}

class Line {
    int p1 = -1;
    List<Integer> p2 = new ArrayList<>();
}


niushop靶场漏洞查找-文件上传漏洞等(超详细) 发现报错,而且这个报错十分熟悉,与我们所学的sqli报错注入特别像。这里查询到后面的url有且仅有一个,目测估计是后台。上传的图片是包含一句话木马的图片。发现获取到了webshell。实战漏洞-niushop。 阅读详情

相关推荐

IGCT 测试原理概述

IGCT(Integrated Gate-Commutated Thyristor,集成门极换流晶闸管)是一种高功率半导体器件,基于门极可关断晶闸管(GTO)的原理,但通过集成门极驱动单元和优化结构(如硬驱动技术),实现了更低的导通损耗、更快的开关速度和更高的可靠性。它适用于中高压大功率应用,如高压直流输电(HVDC)、中压变频器和模块化多电平变换器(MMC)。IGCT 的结构为四层 p-n-p-n 器件,门极电流控制导通和关断,阳极电流在关断时快速换流至门极,通常在 1 μs 内完成。

专注于前沿技术的探索与实践,热衷于从代码逻辑、架构设计到行业应用等多维度深挖技术本质。期待在技术之路上与你共同成长~ 174

力扣(LeetCode)算法_C++——稀疏矩阵乘法

给定两个 稀疏矩阵 :大小为 m x k 的稀疏矩阵 mat1 和大小为 k x n 的稀疏矩阵 mat2 ,返回 mat1 x mat2 的结果。你可以假设乘法总是可能的。输入:mat1 = [[1,0,0],[-1,0,3]], mat2 = [[7,0,0],[0,0,0],[0,0,1]]输入:mat1 = [[0]], mat2 = [[0]]输出:[[7,0,0],[-7,0,3]]

weixin_43945471的博客 817

微信小程序扫码点餐(订餐)系统(uni-app+SpringBoot后端+Vue管理端)

微信小程序扫码点餐(订餐)系统(uni-app+SpringBoot后端+Vue管理端)

【中等】力扣算法题解析LeetCode311稀疏矩阵乘法

LeetCode 311题要求计算两个稀疏矩阵的乘积。传统三重循环解法因大量零元素计算效率低下。本文提出优化策略:通过调整循环顺序(优先遍历中间层k)并跳过A[i][k]=0的情况,减少无效计算。核心公式C[i][j] = Σ(A[i][k]*B[k][j])中,当A[i][k]=0时可跳过整轮j循环。Java实现通过三重循环(外层i、中层k、内层j)结合零值检测,显著提升稀疏矩阵运算效率。

ylumwd的博客 1277

leetcode311. 稀疏矩阵相乘

首先,两个矩阵要是想相乘需要满足,第一个矩阵的列数等于第二个矩阵的行数,满足的话就可以相乘得到新的矩阵了。p的矩阵相乘,将会得到一个m*p的矩阵。首先要知道矩阵是怎么相乘的。

OceanStar的博客 2865

LeetCode Hot 100——数组、矩阵

从零开始,用Java刷力扣 Hot 100,分享题解和思路。今天的专题是Hot100中的矩阵的三道题——除自身以外数组的乘积、缺失的第一个正数、矩阵置零。

Jacob_611的博客 382

LeetCode - 311稀疏矩阵乘法

【中等】数学、哈希表

学哥斌的博客 698

[Swift]LeetCode311. 稀疏矩阵相乘 $ Sparse Matrix Multiplication

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/stren...

weixin_30615767的博客 343

[LeetCode] 311. Sparse Matrix Multiplication 稀疏矩阵相乘

Given twosparse matricesAandB, return the result ofAB. You may assume thatA's column number is equal toB's row number. Example: A = [ [ 1, 0, 0], [-1, 0, 3] ] B = [ [ 7, 0, 0 ],...

weixin_30920853的博客 231

[leetcode]311. Sparse Matrix Multiplication 稀疏矩阵相乘

Given twosparse matricesAandB, return the result ofAB. You may assume thatA's column number is equal toB's row number. Example: Input: A = [ [ 1, 0, 0], [-1, 0, 3] ] B = [ ...

weixin_30656145的博客 162

Leetcode311. Sparse Matrix Multiplication

【代码】【Leetcode311. Sparse Matrix Multiplication

数学、算法爱好者的博客 305

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

题目链接: https://leetcode.com/problems/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

小榕流光的专栏 5426

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 equal to B's row number. Example: A = [ [ 1, 0, 0], [-1, 0, 3] ] B = [ [ 7, 0, 0 ],

606

Leetcode 311. Sparse Matrix Multiplication (Medium) (cpp)

Leetcode 311. Sparse Matrix Multiplication (Medium) (cpp)

还是需要多学习!闷声大发财,这是坠吼的! 722

LeetCode 311. Sparse Matrix Multiplication

需要利用 sparse 的特性来做这道题。扫描A的每一个元素 A[i][j],考虑A[i][j]会被B中哪些元素乘到。 对于A的第i行 A[i][*],会乘以B的每一列 B[*][k]∀k,得到 res[i][k]。因此,最后 res[i][k] += A[i][j] * B[j][k]。 这样的好处是,可以先判断 A[i][j] 是否为0,如果是0,可以节省所有需要和 A[i][...

weixin_30507269的博客 84

Leetcode 311. Sparse Matrix Multiplication - Fb tag

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:Input: A = [ [ 1, 0, 0], [-1, 0, 3] ] B = [ [ 7, 0, 0 ], [...

a568093361的博客 356

Leetcode 311: Sparse Matrix Multiplication

Given twosparse matricesAandB, return the result ofAB. You may assume thatA's column number is equal toB's row number. Example: A = [ [ 1, 0, 0], [-1, 0, 3] ] B = [ [ 7, 0, 0 ],...

weixin_30501857的博客 81

Leetcode 311 / Lintcode 654 Sparse Matrix Multiplication

LQ 要钱故跑到 Lintcode去做… 首先看暴力解: 就是res[i][j] 等于A的第i行和B第j列的点积 class Solution { public: vector<vector<int>> multiply(vector<vector<int>> &A, vector<vector<int>> &B) { int m = A.size(), n = B[0].siz.

Xurui_Luo的博客 272

leetcode 311. 稀疏矩阵乘法

矩阵乘,按照definition来就可以

邹九的个人博客 359

LeetCode学习:动态规划 矩阵连乘

基本思想:待求解问题分解成若干个子问题,先求解子问题,然后从这些子问题的解得到原问题的解。与分治法不同:子问题往往不是互相独立。 特征:1.最优解包含着其子问题的最优解(最优子结构)。2.子问题重叠性质。 用一个表来记录所有已解决的子问题的答案。不管该子问题是否被用到,只要它被计算过,就将其结过填入表中。 设计算法步骤: (1)找出最优解的性质,并刻画其结构特征。 (2)递归地定义最优解...

LYROOO的博客 3106

Python leetcode 2906 构造乘积矩阵,力扣练习,矩阵递推,经典递推题目,递推代码实战

Python leetcode 2906 构造乘积矩阵,力扣练习,矩阵递推,经典递推题目,递推代码实战

L_goodboy的博客 576
上一篇: LeetCode 310. Minimum Height Trees(最小高度树)
下一篇: LeetCode 312. Burst Balloons(戳气球)
jmspan
博客等级 码龄10年 99粉丝 431原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值