GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Leetcode-Algorithms/src/leetcode/ReshapetheMatrix.java at master · edyluisrey/Leetcode-Algorithms Expand file tree/ Copy pathReshapetheMatrix.javaMore file actions 33 lines (31 loc) · 832 Bytes / Copy pathReshapetheMatrix.javaFile metadata and controls33 lines (31 loc) · 832 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 package leetcode; import java.util.Arrays; public class ReshapetheMatrix { public static void main(String[] args){ int[][] nums = new int[][] {{1,2},{3,4}}; ReshapetheMatrix test = new ReshapetheMatrix(); int[][] res= test.matrixReshape(nums, 1, 4); System.out.println(Arrays.toString(res[0])); } public int[][] matrixReshape(int[][] nums, int r, int c) { if(nums==null || nums.length==0) return nums; int a =nums.length; int b=nums[0].length; int total = a*b; int[][] res = new int[r][c]; if((r*c)==total){ int co=0, ro=0; for(int i =0; i< a;i++){ for(int j= 0; j< b; j++){ res[ro][co]= nums[i][j]; co++; if(co==c){ co=0; ro++; } } } }else return nums; return res; } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |