【leetcode】Set Matrix Zeroes(middle)

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

思路:不能用额外空间,就用矩阵的第一行和第一列来标记这一行或这一列是否需要置0. 用两个bool量记录第一行和第一列是否需要置0

大神的代码和我的代码都是这个思路,但是我在画0的时候是行列分开处理的,大神的代码是一起处理的

void setZeroes(vector<vector<int> > &matrix) {
        if(matrix.empty())
            return;

        bool iszero1 = false; //第一行是否全0
        bool iszero2 = false; //第一列是否全0
        //第一行 第一列单独拿出来做标记
        for(int j = 0; j < matrix[0].size(); j++)
        {
            if(matrix[0][j] == 0) iszero1 = true;
        }
        for(int i = 0; i < matrix.size(); i++)
        {
            if(matrix[i][0] == 0) iszero2 = true;
        }

        for(int i = 1; i < matrix.size(); i++)
        {
            for(int j = 1; j < matrix[0].size(); j++)
            {
                //如果数值为0,把对应那一行的第一个 和 那一列的第一个数字置为0
                if(matrix[i][j] == 0)
                {
                    matrix[0][j] = 0;
                    matrix[i][0] = 0;
                }
            }
        }

        //分行列处理
        //先不考虑[0][0] 位置  如果某一行第一个为0,整行置0
        for(int i = 1; i < matrix.size(); i++)
        {
            if(matrix[i][0] == 0)
            {
                for(int j = 1; j < matrix[0].size(); j++)
                {
                    matrix[i][j] = 0;
                }
            }
        }

        for(int j = 1; j < matrix[0].size(); j++)
        {
            if(matrix[0][j] == 0)
            {
                for(int i = 1; i < matrix.size(); i++)
                {
                    matrix[i][j] = 0;
                }
            }
        }

        if(iszero1)
        {
            for(int j = 0; j < matrix[0].size(); j++)
            {
                matrix[0][j] = 0;
            }
        }
        if(iszero2)
        {
            for(int i = 0; i < matrix.size(); i++)
            {
                matrix[i][0] = 0;
            }
        }

        return;
    }

大神的代码:

public void setZeroes(int[][] matrix) {
    int rownum = matrix.length;
    if (rownum == 0)  return;
    int colnum = matrix[0].length;
    if (colnum == 0)  return;

    boolean hasZeroFirstRow = false, hasZeroFirstColumn = false;

    // Does first row have zero?
    for (int j = 0; j < colnum; ++j) {
        if (matrix[0][j] == 0) {
            hasZeroFirstRow = true;
            break;
        }
    }

    // Does first column have zero?
    for (int i = 0; i < rownum; ++i) {
        if (matrix[i][0] == 0) {
            hasZeroFirstColumn = true;
            break;
        }
    }

    // find zeroes and store the info in first row and column
    for (int i = 1; i < matrix.length; ++i) {
        for (int j = 1; j < matrix[0].length; ++j) {
            if (matrix[i][j] == 0) {
                matrix[i][0] = 0;
                matrix[0][j] = 0;
            }
        }
    }

    // set zeroes except the first row and column 一起处理的
    for (int i = 1; i < matrix.length; ++i) {
        for (int j = 1; j < matrix[0].length; ++j) {
            if (matrix[i][0] == 0 || matrix[0][j] == 0)  matrix[i][j] = 0;
        }
    }

    // set zeroes for first row and column if needed
    if (hasZeroFirstRow) {
        for (int j = 0; j < colnum; ++j) {
            matrix[0][j] = 0;
        }
    }
    if (hasZeroFirstColumn) {
        for (int i = 0; i < rownum; ++i) {
            matrix[i][0] = 0;
        }
    }
}
时间: 2024-07-28 21:10:12

【leetcode】Set Matrix Zeroes(middle)的相关文章

【leetcode】Factorial Trailing Zeroes(easy)

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 思路:编程之美里有,就是找因子5的个数. int trailingZeroes(int n) { int ans = 0; while(n > 0) { ans += n / 5; n /= 5; } return ans; }

【leetcode】Divide Two Integers (middle)☆

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 思路: 尼玛,各种通不过,开始用纯减法,超时了. 然后用递归,溢出了. 再然后终于开窍了,用循环,把被除数每次加倍去找答案,结果一遇到 -2147483648 就各种不行, 主要是这个数一求绝对值就溢出了. 再然后,受不了了,看答案. 发现,大家都用long long来解决溢

【leetcode】Insertion Sort List (middle)

Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分完全分开,指针不要有交叉. 即不会通过->next 重叠 class Solution { public: ListNode *insertionSortList(ListNode *head) { if(head == NULL) return NULL; ListNode * ans = hea

【leetcode】Combination Sum III(middle)

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

【leetcode】Repeated DNA Sequences(middle)★

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA. Write a function to find all the 10-letter-long seq

【leetcode】Compare Version Numbers(middle)

Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character.The . characte

【leetcode】Balanced Binary Tree(middle)

Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 思路: 我居然在这道题上卡了一个小时.关键是对于平衡的定义,我开始理解

【Leetcode】Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Could you devise a constant space solution? 思路:因为需要遍历整个矩阵,时间复杂度肯定需要O(m * n),对于空间复杂度而言,第一种是可以使用O(m * n),对每个位置的0的情况进行记录,第二种是使用O(m + n),对每行每列是否存在0进行记录,第三种是O(1)

【LeetCode】数组--合并区间(56)

写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组(Array)是一种线性表数据结构.它用一组连续的内存空间,来存储一组具有相同类型的数据.在每一种编程语言中,基本都会有数组这种数据类型.不过,它不仅仅是一种编程语言中的数据类型,还是一种最基础的数据结构. 贪心算法回顾: [LeetCode]贪心算法--买卖股票的最佳时机II(122) [LeetC