Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Diagonal traverese.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class Solution {//time compleaxity o(m*n)and space complexity o(1)
public:
vector<int> findDiagonalOrder(vector<vector<int>>& mat) {
if(mat.size()==0)
return {};
int m =mat.size();
int n= mat[0].size();
vector<int>result(n*m);
bool dir =true;
int r =0,c=0;

for(int i=0;i<m*n;i++)
{

result[i] =mat[r][c];
if(dir)//upward
{
if(c==n-1)// hit top edge
{
dir =false;
r++;
}
else if(r==0)// hit right edge
{
dir =false;
c++;
}
else //normal
{
r--;
c++;
}

}
else//downward
{
if(r==m-1) //hit bottom edge
{
dir =true;
c++;
}
else if(c==0) //hit left edge
{
dir=true;
r++;
}
else
{
r++;
c--;
}

}

}
return result;
}
};
20 changes: 20 additions & 0 deletions Product of array except self.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int>result;
result[0]=1;
for(int i =1;i<nums.size();i++) // calculate the left product for each element
{
result[i]= result[i-1]*nums[i-1];
}
int right = 1; // to calculate right product for each element and put in result array
for(int i=result.size()-1;i>=0;i++)
{
result[i]=result[i]*right;
right = right*nums[i];
i--;
}

return result;
}
};
31 changes: 31 additions & 0 deletions spiral matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {//tc:O(m*n),sc:O(1)
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if(matrix.size()==0)
return {};
int m =matrix.size();
int n= matrix[0].size();
int l =0,r=n-1,t=0,b=m-1;//left,right,top ,bottom
vector<int>result;
while(l<=r && t<=b)
{
for(int i =l;i<=r;i++)
result.push_back(matrix[t][i]);
t++;
for(int i =t;i<=b;i++)
result.push_back(matrix[i][r]);
r--;
if(t<=b){ // to avoid duplicates
for(int i =r;i>=l;i--)
result.push_back(matrix[b][i]);
b--;
}
if(l<=r){
for(int i =b;i>=t;i--)
result.push_back(matrix[i][l]);
l++;
}
}
return result;
}
};