Posts

Showing posts with the label imp questions

INTERVIEW BIT SOLUTION

 1) Rotate Matrix Interview Bit Solution using ArrayList public class Solution { public void rotate(ArrayList<ArrayList<Integer>> a) { int n = a.get(0).size(); for(int i=0;i<n/2;i++) { for(int j=i;j<n-i-1;j++) { int temp = a.get(i).get(j); int pp = a.get(n-j-1).get(i); a.get(i).set(j, pp); int oo = a.get(n-i-1).get(n-j-1); a.get(n-j-1).set(i, oo); int ll = a.get(j).get(n-i-1); a.get(n-i-1).set(n-j-1,ll); a.get(j).set(n-i-1,temp); } } } } Using array import java.util.Arrays; public class Rotate2DArrayInPlace { public static void main (String args [] ) throws Exception { Integer [] [] myNxNArray = new Integer [][] { {1, 2, 3}, {4,5,6}, {7,8,9} }; System.out.println("SOURCE ARRAY: \n" + Arrays.deepToString(myNxNArray)); Integer [][] rotatedInPlace = rotateMatrixInPlaceCounterClockwise(myNxNArray); System.out.println("ROTATED IN PLACE - 90 degrees COUNTER CLOCKWISE"); System.out.println(Arrays.deepToStrin...