-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3fcdad
commit 3c61b7d
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <gtest/gtest.h> | ||
|
||
class Solution { | ||
public: | ||
static auto set_zeroes(std::vector<std::vector<int32_t>> &matrix) -> void { | ||
const int32_t row_length = matrix.size(); | ||
const int32_t column_length = matrix[0].size(); | ||
|
||
std::vector<bool> row_zero(row_length); | ||
std::vector<bool> column_zero(column_length); | ||
|
||
for (size_t row = 0; row < row_length; ++row) { | ||
for (size_t column = 0; column < column_length; ++column) { | ||
if (matrix[row][column] == 0) { | ||
row_zero[row] = true; | ||
column_zero[column] = true; | ||
} | ||
} | ||
} | ||
|
||
for (size_t row = 0; row < row_length; ++row) { | ||
for (size_t column = 0; column < column_length; ++column) { | ||
if (row_zero[row] || column_zero[column]) { | ||
matrix[row][column] = 0; | ||
} | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include "../include/solution.hpp" | ||
|
||
class SetMatrixZeroes : public ::testing::Test { | ||
protected: | ||
~SetMatrixZeroes() override = default; | ||
}; | ||
|
||
TEST_F(SetMatrixZeroes, FirstTest) { | ||
std::vector<std::vector<int32_t>> matrix = | ||
{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}; | ||
|
||
Solution::set_zeroes(matrix); | ||
|
||
std::vector<std::vector<int32_t>> expected = | ||
{{1, 0, 1}, {0, 0, 0}, {1, 0, 1}}; | ||
|
||
ASSERT_EQ(matrix, expected); | ||
} | ||
|
||
TEST_F(SetMatrixZeroes, SecondTest) { | ||
std::vector<std::vector<int32_t>> matrix = | ||
{{0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5}}; | ||
|
||
Solution::set_zeroes(matrix); | ||
|
||
std::vector<std::vector<int32_t>> expected = | ||
{{0, 0, 0, 0}, {0, 4, 5, 0}, {0, 3, 1, 0}}; | ||
|
||
ASSERT_EQ(matrix, expected); | ||
} | ||
|
||
auto main(int argc, char **argv) -> int { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |