Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

matrix rotation 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 :: string split java 
Java :: java.lang.stringindexoutofboundsexception: string index out of range: 10 
Java :: convert long to localdatetime java 
Java :: ascii values to display certain characters in java 
Java :: how to access methods from another class in java 
Java :: when does black jack happens 
Java :: final method java 
Java :: how to saperate string to array 
Java :: java stream group by multiple fields 
Java :: how to sprint minecraft java 
Java :: javadoc link 
Java :: start an activity in adapter 
Java :: thymeleaf spring boot dependency for mail 
Java :: spring jpa query with union all 
Java :: $950 at 6% per annum for three years. 
Java :: partioning operation Java 
Java :: bootstrap messages red 
Sql :: fatal error: libpq-fe.h: No such file or directory 
Sql :: find column in all stored procedures sql server 
Sql :: django.core.exceptions.ImproperlyConfigured: mysqlclient 1.4.0 or newer is required; you have 0.10.1. 
Sql :: oracle finding duplicate records 
Sql :: mysql stop password policy 
Sql :: alembic.util.exc.CommandError: Target database is not up to date. 
Sql :: tsql copy table 
Sql :: import sql file from laravel 
Sql :: get table columns from sql 
Sql :: mysql show table character set 
Sql :: mysql add days to date 
Sql :: mysql timestamp in laravel migration 
Sql :: postgres DROP and create contraint 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =