diff --git a/Diagonal traverse b/Diagonal traverse new file mode 100644 index 00000000..47e19438 --- /dev/null +++ b/Diagonal traverse @@ -0,0 +1,43 @@ +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, c = 0; + + boolean dir = true; + + for(int i = 0; i < m * n; i++) { + result[i] = mat[r][c]; + + if(dir) { + if(c == n - 1) { + r++; + dir = false; + } + else if(r == 0) { + c++; + dir = false; + } + else { + r--; c++; + } + } else { + if(r == m - 1) { + c++; + dir = true; + } + else if(c == 0) { + r++; + dir = true; + } + else { + r++; c--; + } + } + } + + return result; + } +} diff --git a/Product except itself b/Product except itself new file mode 100644 index 00000000..005e3599 --- /dev/null +++ b/Product except itself @@ -0,0 +1,24 @@ +class Solution { + public int[] productExceptSelf(int[] nums) { + int n = nums.length; + + int[] result = new int[n]; + + int rp = 1; + result[0] = 1; + + for(int i=1; i=0; i--){ // right pass + rp = rp*nums[i+1]; + result[i] = result[i] * rp; + } + + return result; + } +}