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
26 changes: 26 additions & 0 deletions Problem_1_Product_Except_Self.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public int[] productExceptSelf(int[] nums) {
//[1,2,3,4]
//left pass - [1,1,2,6]
//right pass -[24,12,4,1]
//result - [24,12,8,6]

int rp = 1;
int n = nums.length;
int result[] = new int[n];
result[0] = 1;

//forward pass...
for(int i = 1 ; i < n ; i++) {
rp = rp * nums[i-1];
result[i] = rp;
}
//backward pass...
rp = 1;
for(int i = n-2 ; i >= 0 ; i-- ) {
rp = rp * nums[i+1];
result[i] = rp * result[i];
}
return result;
}
}
39 changes: 39 additions & 0 deletions Problem_2_Diagonal_traverse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Solution {
public int[] findDiagonalOrder(int[][] mat) {
int r = 0; int c = 0;
int m = mat.length;
int n = mat[0].length;
int[] result = new int[m*n];
boolean flag = true;
for(int i = 0 ; i < result.length ; i++) {
result[i] = mat[r][c];
if(flag) {
if(r == 0 && c != n-1) {
c++;
flag = false;
}
else if(c == n-1) {
r++;
flag = false;
}
else {
r--; c++;
}
}
else {
if(c == 0 && r != m-1) {
r++;
flag = true;
}
else if(r == m-1) {
c++;
flag = true;
}
else {
c--; r++;
}
}
}
return result;
}
}
35 changes: 35 additions & 0 deletions Problem_3_Matrix_elements_spiral_order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int top = 0 ; int bottom = m-1; int right = n-1; int left = 0;
List<Integer> l = new ArrayList<>();
while(left <= right && top <= bottom) {
if(left <= right && top <= bottom) {
for(int j = left; j <= right; j++) {
l.add(matrix[top][j]);
}
}
top++;
if(left <= right && top <= bottom) {
for(int i = top; i <= bottom; i++) {
l.add(matrix[i][right]);
}
}
right--;
if(left <= right && top <= bottom) {
for(int j = right; j >= left; j--) {
l.add(matrix[bottom][j]);
}
}
bottom--;
if(left <= right && top <= bottom) {
for(int i = bottom; i >= top; i--) {
l.add(matrix[i][left]);
}
}
left++;
}
return l;
}
}