Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

rotate matrix in java

package Arrays;

import java.util.Arrays;

public class RotateMatrix {
    static int[][] rotate(int[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;

        int[][] ans = new int[rows][cols];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                ans[i][j] = matrix[j][i];

            }
            reverse(ans[i]);
        }
        return ans;
    }
    
    static void reverse(int[] arr) {
        for (int i = 0; i < arr.length/2; i++) {
            int temp = arr[i];
            arr[i] = arr[arr.length-i-1];
            arr[arr.length-i-1] = temp;
        }
    }

    public static void main(String[] args) {
        int[][] nums = {{1, 2, 3},{4, 5, 6}, {7, 8,9}};
        System.out.println(Arrays.deepToString(rotate(nums)));
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: class syntax in java 
Java :: arraylist contains doc 
Java :: extend vs implement in java 
Java :: multiple return values in function java 
Java :: print treeset java 
Java :: sorting algorithms in java 
Java :: java program to print vowels in a string 
Java :: enum to string java 
Java :: Java Filters 
Java :: xml button color not changing 
Java :: Exception in Application start method java.lang.reflect.InvocationTargetException 
Java :: imperative programming paradigm example 
Java :: Access HashMap Elements 
Java :: get sum of int array and return string 
Java :: rest api client url not connecting to the database in spring boot 
Java :: bukkit e.getCurrentItem() bytes? 
Sql :: sql developer search all packages for text 
Sql :: get database size mysql 
Sql :: guid to string sql 
Sql :: update sql server from another table 
Sql :: uninstall mysql on ubuntu 
Sql :: get tables in database sql 
Sql :: starts and end with vowel sql 
Sql :: oracle columns table 
Sql :: mysql update field from one table to another 
Sql :: how to stop all connections to a psql 12 database? 
Sql :: sql server check if temp table exists 
Sql :: delete first row in sql 
Sql :: create table sql server 
Sql :: add column table sql default value 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =