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
21 changes: 21 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public int[] productExceptSelf(int[] nums) {
int rp=1;
int n= nums.length;
// forward pass left to right
int [] result = new int[n];
result[0]=1;
for(int i=1;i<n;i++){
rp=rp*nums[i-1];
result[i]=rp;
}

//backward pass right to left
rp=1;
for(int i=n-2;i>=0;i--){
rp=rp*nums[i+1];
result[i]=rp*result[i];
}
return result;
}
}
57 changes: 57 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class Solution {
public int[] findDiagonalOrder(int[][] mat) {
int m=mat.length;
int n=mat[0].length;

int[] result= new int[m*n];
int r=0;
int c=0;
boolean flag =true; //upward
for(int i=0;i<result.length;i++){

result[i]=mat[r][c];
//get r and c
if(flag){
//up
if(r==0 && c!=n-1){
//roof but not right
flag=false;
c++;
}
//right but not roof
else if(c==n-1){
flag=false;
r++;

}
//basic top roght traversal
else{
r--;
c++;
}
}
else{
//down
if(c==0 && r!=m-1){
//bottom but not left
flag=true;
r++;
}
//left but not bottom
else if(r==m-1){
flag=true;
c++;

}
//basic bottom left traversal
else{
r++;
c--;
}

}

}
return result;
}
}
48 changes: 48 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.util.ArrayList;
import java.util.List;

class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> li= new ArrayList<>();
int m=matrix.length;
int n=matrix[0].length;
int top=0;
int bottom = m-1;
int left=0;
int right=n-1;
while(top<=bottom && left<=right){
//top row iteration
for(int j=left; j<=right; j++){
li.add(matrix[top][j]);
}
//shrinking the top
top++;
if(top<=bottom){
//right col iteration
for(int i=top; i<=bottom; i++){
li.add(matrix[i][right]);
}
}
//shrinking right
right--;
if(top<=bottom && left<=right){
//bottom row iteration
for(int j=right; j>=left; j--){
li.add(matrix[bottom][j]);
}
}
//shrinking bottom
bottom--;
if(top<=bottom && left<=right){
//left col iteration
for(int i=bottom; i>=top; i--){
li.add(matrix[i][left]);
}
}
//shrinking left
left++;
}
return li;

}
}