如何在不使用额外空间的情况下解决这个问题?
how to solve this question without using extra space?
给定一个 m x n 矩阵,如果元素为 0,则将其整行和整列设置为 0。就地执行。
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
for(int i=0 ; i<matrix.size() ; ++i){
for(int j=0 ; matrix[i].size() ; ++j){
if(matrix[i][j]==0){
int col=0, row=0;
while(col<matrix.size()){
matrix[col][j]=0;
col++;
}
while(row<matrix[i].size()){
matrix[i][row]=0;
row++;
}
break;
}
}
}
}
};
在遍历我得到零的地方时,我使该行和列为零,我不知道为什么我会收到运行时错误
我猜您正在尝试一次性解决它,这是一件好事,但有时它会使解决方案变得复杂。
这个解决方案很容易理解,会通过,并且不需要额外的空间,但我们可以通过多次传递来做到这一点,这完全没问题,因为时间复杂度保持不变。
#include <vector>
class Solution {
public:
void setZeroes(std::vector<std::vector<int>>& matrix) {
bool is_zero_col = false;
bool is_zero_row = false;
int row_length = matrix.size();
int col_length = matrix[0].size();
for (int row = 0; row < row_length; row++) {
if (matrix[row][0] == 0) {
is_zero_col = true;
break;
}
}
for (int col = 0; col < col_length; col++) {
if (matrix[0][col] == 0) {
is_zero_row = true;
break;
}
}
for (int row = 1; row < row_length; row++) {
for (int col = 1; col < col_length; col++)
if (matrix[row][col] == 0) {
matrix[row][0] = 0;
matrix[0][col] = 0;
}
}
for (int row = 1; row < row_length; row++) {
for (int col = 1; col < col_length; col++)
if (matrix[row][0] == 0 || matrix[0][col] == 0) {
matrix[row][col] = 0;
}
}
if (is_zero_col) {
for (int row = 0; row < row_length; row++) {
matrix[row][0] = 0;
}
}
if (is_zero_row) {
for (int col = 0; col < col_length; col++) {
matrix[0][col] = 0;
}
}
}
};
<小时 />参考资料
- 有关其他详细信息,您可以查看讨论区。其中有很多公认的解决方案、解释、多种语言的高效算法以及时间/空间复杂性分析。
如果您的矩阵没有等于最大整数值的元素,则的解决方案。 首先找到所有零并为相应的行和列设置最大 int 值。 然后为所有最大值设置零。
class Solution {
public:
void setZeroes(std::vector<std::vector<int>>& matrix) {
constexpr auto max_value = std::numeric_limits<int>::max();
for (auto& col : matrix) {
const auto zeroElem = std::find(col.begin(), col.end(), 0);
if (zeroElem != col.end()) {
std::fill(col.begin(), col.end(), max_value);
const auto index = std::distance(col.begin(), zeroElem);
for (auto& sCol : matrix)
if (index < sCol.size()) {
sCol[index] = max_value;
}
}
}
for (auto& col : matrix)
for (auto& elem : col)
if (max_value == elem)
elem = 0;
}
};
int main(int, char**)
{
std::vector<std::vector<int>> Matrix;
Matrix.insert(Matrix.end(), { 1,1,1,1,1,1 });
Matrix.insert(Matrix.end(), { 1,1,1,1,0,1 });
Matrix.insert(Matrix.end(), { 1,1,1,1,1,1 });
Matrix.insert(Matrix.end(), { 1,1,0,1,1,1 });
Matrix.insert(Matrix.end(), { 1,1,1,1,1,1 });
Solution s;
s.setZeroes(Matrix);
for (auto& sCol : Matrix) {
std::cout << std::endl;
for (auto& Elem : sCol) {
std::cout << Elem << ' ';
}
}
return 0;
}
1 1 0 1 0 1
0 0 0 0 0 0
1 1 0 1 0 1
0 0 0 0 0 0
1 1 0 1 0 1
相关文章:
- 如何在不使用额外空间的情况下解决这个问题?
- 如何解决在负数的情况下程序以相同方式工作的问题?
- 在这种情况下,我们可以使用静态而不是朋友吗,还有其他解决方案是什么
- 如何在不设置 ulimit -n 的情况下解决套接字程序打开太多文件的错误
- 如何在不创建新配置的情况下对两个不同解决方案使用的一个项目使用不同的 #defines
- 模板和unique_ptr继承情况下的重载解决方案
- 在这种情况下如何解决?语法错误:在"* "node" ?
- Boost Filesystem flowly_caronical不会在没有父文件夹的情况下解决相对路径
- 解决方案重新生成在 30% 的情况下失败
- 在这种情况下,有没有办法用单个解决方案替换两个仅在类型上不同的相似函数?
- 如何在没有System32的情况下解决"java.lang.UnsatisfiedLinkError:找不到依
- 错误:在没有参数列表的情况下无效使用模板名称“ExTree” ||如果解决这个问题怎么办?(初学者)
- 在这种情况下如何解决"Reference to non-static member function must be called"?
- 我的项目如何在没有提示的情况下不抱怨未解决的未支出
- 我如何在不将所有相关代码带到标头的情况下解决模板成员函数的未定义引用
- 这个错误一般意味着什么?以及在这种情况下我如何解决它
- 在任何情况下都会出现意外输出.我的解决方案出了什么问题
- 有没有一种方法可以在不重建任何项目的情况下重新链接解决方案
- 在这种情况下,这是最优雅/最安全/推荐的解决方案吗?
- 如何解决,在使用元编程的条件基类的情况下没有成员函数"print"