diff --git a/Diagonal-traverse.java b/Diagonal-traverse.java new file mode 100644 index 00000000..07569995 --- /dev/null +++ b/Diagonal-traverse.java @@ -0,0 +1,39 @@ +class Solution { + public int[] findDiagonalOrder(int[][] mat) { + int m = mat.length; + int n = mat[0].length; + int[] res = new int[m*n]; + boolean flag=true; + int r=0; + int c=0; + for(int i=0;i=0;i--){ + right[i]=nums[i+1]*rp; + rp = right[i]; + } + + //result + for(int i=0;i spiralOrder(int[][] matrix) { + int m = matrix.length; + int n = matrix[0].length; + int top=0, bottom=m-1, left=0, right=n-1; + List list = new ArrayList<>(); + while(top<=bottom && left<=right){ + //top + + for(int i=left;i<=right;i++){ + list.add(matrix[top][i]); + + } + top++; + + + + //right + if(top<=bottom && left<=right){ + for(int i=top;i<=bottom;i++){ + list.add(matrix[i][right]); + + } + right--; + } + + + //bottom + if(top<=bottom && left<=right){ + for(int i=right;i>=left;i--){ + list.add(matrix[bottom][i]); + + } + bottom--; + } + + + //left + if(top<=bottom && left<=right){ + for(int i=bottom;i>=top;i--){ + list.add(matrix[i][left]); + + } + left++; + } + + + } + return list; + } +} \ No newline at end of file