Arrays
73. Set Matrix Zeroes Solved on: 6th Jan 2024
// Question: https://leetcode.com/problems/set-matrix-zeroes/
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
vector<int> rows;
vector<int> cols;
// find which all rows and cols has to be marked 0
for (int i=0;i<m;++i){
for (int j=0;j<n;++j){
if (matrix[i][j] == 0) {
rows.push_back(i);
cols.push_back(j);
}
}
}
// Mark all the required rows 0
for (int row : rows) {
for (int i=0;i<m;++i){
for (int j=0;j<n;++j){
if (i == row) {
matrix[i][j] = 0;
}
}
}
}
// Mark all the required cols 0
for (int col : cols) {
for (int i=0;i<m;++i){
for (int j=0;j<n;++j){
if (j == col) {
matrix[i][j] = 0;
}
}
}
}
}
};
// Question: https://leetcode.com/problems/pascals-triangle/
class Solution {
public:
vector<vector<int>> generate(int numRows) {
cout << numRows;
vector <vector<int>> v;
for (int i=0;i<numRows;i++){
vector<int> l;
for (int j=0;j<=i;j++){
if (j==0 || j==i){
l.push_back(1);
}
else {
l.push_back(v[i-1][j-1] + v[i-1][j]);
}
}
v.push_back(l);
}
return v;
}
};
Last updated
Was this helpful?