<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)311:稀疏矩陣的乘法

          共 2992字,需瀏覽 6分鐘

           ·

          2021-07-04 09:04

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做 稀疏矩陣的乘法,我們先來看題面:
          https://leetcode-cn.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.

          給你兩個 稀疏矩陣 A 和 B,請你返回 AB 的結(jié)果。
          你可以默認 A 的列數(shù)等于 B 的行數(shù)。


          示例



          解題


          由于是稀疏數(shù)組可以只計算非零數(shù)字。
          將A矩陣轉(zhuǎn)存為hashmap,然后對每一個非零數(shù)遍歷B的列數(shù)來更新它在ans矩陣里可以被用到的位置。

          class Solution {
          public:
              vector<vector<int>> multiply(vector<vector<int>>& A, vector<vector<int>>& B) {
                  int n = A.size(), m = A[0].size(), k = B[0].size();
                  vector<vector<int>> res(n, vector<int>(k, 0));

                  unordered_map<int, int> am;

                  for (int i = 0; i < n; i ++)
                      for (int j = 0; j < m; j ++)
                      {
                          if (!A[i][j]) continue;
                          int idx = i * m + j;
                          am[idx] = A[i][j];
                      }

                  for (auto aij : am)
                  {
                      int va = aij.second, aidx = aij.first, ai = aidx / m, aj = aidx % m;
                      for (int j = 0; j < k; j ++)
                      {
                          res[ai][j] += va * B[aj][j];
                      }
                  }

                  return res;
              }
          };


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-280題匯總,希望對你有點幫助!

          LeetCode刷題實戰(zhàn)301:刪除無效的括號

          LeetCode刷題實戰(zhàn)302:包含全部黑色像素的最小矩陣
          LeetCode刷題實戰(zhàn)303:區(qū)域和檢索 - 數(shù)組不可變
          LeetCode刷題實戰(zhàn)304:二維區(qū)域和檢索 - 矩陣不可變
          LeetCode刷題實戰(zhàn)305:島嶼數(shù)量II
          LeetCode刷題實戰(zhàn)306:累加數(shù)
          LeetCode刷題實戰(zhàn)307:區(qū)域和檢索 - 數(shù)組可修改
          LeetCode刷題實戰(zhàn)308:二維區(qū)域和檢索 - 可變
          LeetCode刷題實戰(zhàn)309:最佳買賣股票時機含冷凍期
          LeetCode刷題實戰(zhàn)310:最小高度樹

          瀏覽 35
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  秋霞午夜 | 免费黄色视频网站亚洲 | 黄色一级视频网站 | 亚洲AV无码国产精品夜色午夜 | 18禁欧美网站 |