Facebook
From sandip_jana, 2 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 337
  1. class Solution {
  2.     public boolean findRotation(int[][] mat, int[][] target) {
  3.                 for (int i=0 ; i<4 ; i++) {
  4.                         if ( same( mat , target ) ) {
  5.                                 return true;
  6.                         }
  7.                         mat = rotate(mat);
  8.                 }
  9.                 return false;
  10.         }
  11.  
  12.         private boolean same(int[][] mat, int[][] target) {
  13.                 if (mat.length == target.length && mat[0].length == target[0].length) {
  14.                         for(int i=0 ; i<mat.length ; i++) {
  15.                                 for(int j=0 ; j<mat[0].length ;j++) {
  16.                                         if ( mat[i][j] != target[i][j] )
  17.                                                 return false;
  18.                                 }
  19.                         }
  20.                 }
  21.                 return true;
  22.         }
  23.  
  24.         private int[][] rotate(int[][] mat) {
  25.                 int n = mat.length;
  26.                 int m = mat[0].length;
  27.                 int b[][] = new int[m][n];
  28.                 for(int i=0 ; i<n ; i++) {
  29.                         for(int j=0 ; j<m ; j++) {
  30.                                 b[j][n-i-1] = mat[i][j];
  31.                         }
  32.                 }
  33.                 return b;
  34.         }
  35.  
  36. }

Replies to Leetcode 1886 rss

Title Name Language When
Re: Leetcode 1886 mie_writes c_loadrunner 1 Month ago.