leetcode筆記:Set Matrix Zeroes,leetcodezeroes

來源:互聯網
上載者:User

leetcode筆記:Set Matrix Zeroes,leetcodezeroes

一.題目描述

Given a m*n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Follow up: Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?

二.題目分析

該題目最直觀的解法就是開闢一個新的矩陣,當原矩陣存在零元素的時候,就將新矩陣的對應行和列置為零。這樣空間複雜度較高,也是題目不允許的。

若要做到空間複雜度為常數,我的做法是就是利用矩陣的第一行和第一列來作為標記使用,這樣便不用開闢新的儲存空間。具體方法:

三.範例程式碼

第一種方法如下:

#include <vector>using namespace std;class Solution{public:    // 時間複雜度O(m * n),空間複雜度O(m + n)    void setZeros(vector<vector<int> >& matrix)    {        const size_t x = matrix.size();        const size_t y = matrix[0].size();        if (x == 0 || y == 0) return;        vector<bool> rowRes(x, false);        vector<bool> colRes(y, false);        for (size_t i = 0; i < x; i++)        {            for (size_t j = 0; j < y; j++)            {                if (matrix[i][j] == 0)                    rowRes[i] = colRes[j] = true;            }        }        // set zero        for (size_t i = 0; i < x; i++)        {            if (rowRes[i])                for (size_t k = 0; k < x; k++)                    matrix[i][k] = 0;        }        for (size_t j = 0; j < y; j++)        {            if (colRes[j])                for (size_t k = 0; k < x; k++)                    matrix[k][j] = 0;        }    }};

以上方法的空間複雜度為O(m + n),並不能達到題目要求的最終要求。

第二種方法如下:

#include <vector>using namespace std;class Solution{public:    void setZerosBetter(vector<vector<int> >& matrix)    {        const size_t x = matrix.size();        const size_t y = matrix[0].size();        bool x_key = false, y_key = false;        if (x == 0 || y == 0) return;        for (size_t i = 0; i < y; i++)        {            if (matrix[0][i] == 0)            {                x_key = true;                break;            }        }        for (size_t i = 0; i < x; i++)        {            if (matrix[i][0] == 0)            {                y_key = true;                break;            }        }        for (size_t i = 0; i < x; i++)        {            for (size_t j = 0; j < y; j++)            {                if (matrix[i][j] == 0 && i > 0 && j > 0)                {                    matrix[i][0] = 0;                    matrix[0][j] = 0;                }            }        }        // 調整1~x行、1~y列的元素        for (size_t i = 1; i < x; i++)            if (matrix[i][0] == 0)            {                for (size_t k = 1; k < y; k++)                    matrix[i][k] = 0;            }        for (size_t j = 1; j < y; j++)            if (matrix[0][j] == 0)            {                for (size_t k = 1; k < x; k++)                    matrix[k][j] = 0;            }        // 最後調整第一行第一列        if (y_key)            for (size_t k = 0; k < x; k++)                matrix[k][0] = 0;        if (x_key)            for (size_t k = 0; k < y; k++)                matrix[0][k] = 0;    }};

四.小結

這道題如果只是僅僅想實現功能的話,不需要什麼技巧,只有提高對空間複雜度的要求才能體現出演算法設計的思想。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.