-
Notifications
You must be signed in to change notification settings - Fork 0
/
DAY 4 problem solutions
45 lines (40 loc) · 1.1 KB
/
DAY 4 problem solutions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//problem 1 - reshape the matrix // leetcode Problem .......
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
int x=mat.size();
int y=mat[0].size();
if(x*y != r*c)
{
return mat;
}
vector<vector<int>> result(r, vector<int>(c));
int i=0, j=0;
for(int row= 0; row <x; row++){
for(int col=0; col<y; col++){
result[i][j++]=mat[row][col];
if(j>=c)
{
i++;
j=0;
}
}
}
return result;
}
};
// Problem 2 Pasale tringle
class Solution { // abhishektyagi0020(1923it1200)
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>>r(numRows);// n dynamic vector
for(int i=0; i<numRows; i++) //genreate n rows
{
r[i].resize(i+1); // always equal no of row and columns
r[i][0]=r[i][i]=1; //every time first and last element is 1
for(int j=1; j<i; j++)// first col to second last column
r[i][j]=r[i-1][j-1]+r[i-1][j];
}
return r;
}
};