原题地址:https://oj.leetcode.com/problems/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. 解题思路:分别记录两个向量x, y,保存行和列是否有0,再次遍历数组时查询对应的行和列然后修改值. 代码: class Solution: # @param matrix, a list of…
Set Matrix ZeroesGiven a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up:Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple im…
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. 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…
原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], […
http://oj.leetcode.com/problems/set-matrix-zeroes/ 因为空间要求原地,所以一些信息就得原地存储.使用第一行第一列来存本行本列中是否有0.另外对于第一个元素[0][0],需要额外处理. #include <iostream> #include <vector> using namespace std; class Solution { public: void setZeroes(vector<vector<int>…
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 原题链接:https://oj.leetcode.com/problems/set-matrix-zeroes/ 题目:给定一个m * n 的矩阵.假设有一个元素是0.将其所在行和列设为0. 思路:先记录下是0 的元素的位置.再去置0. public void setZeroes(int[][] matrix)…
题意: 给一个n*m的矩阵,如果某个格子中的数字为0,则将其所在行和列全部置为0.(注:新置的0不必操作) 思路: 主要的问题是怎样区分哪些是新来的0? 方法(1):将矩阵复制多一个,根据副本来操作原矩阵. 方法(2):发现空间还可以用O(n)来解决. 方法(3):若m[i][j]=0,则将m[i][0]和m[0][j]标记为0,表示i行和j列都为0,但是这样的问题是,首行和首列会冲突?那就将首列先预处理一下,用另外的常量标记就行了.时间复杂度O(n*m),空间O(1). class Solut…
题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. python…
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘以后,其中有多少个数字是以0结尾的. 方法一: class Solution: # @return an integer def trailingZeroes(self, n): res = 0 if n < 5: return 0 else: return n/5+ self.trailingZe…
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space?A straight forward solution using O(m n) space is probably a bad idea.A simple improvement uses…