Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
1.8 kB
2
Indexable
Problem Description
You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Note:

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly.

DO NOT allocate another 2D matrix and do the rotation.

Input format
There are N+1 lines of input.

First line contains one integer N.

Next N line contains N space separated integers

Output format
Print the NxN rotated matrix.

Sample Input 1
3

1 2 3

4 5 6

7 8 9

Sample Output 1
7 4 1

8 5 2

9 6 3




'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.replace(/\s+/g, " ").trim();
    });
    main();
});

function readLine() {
    return inputString[currentLine++];
}

function readIntArr() {
    let str = readLine();
    str = str.split(" ");
    let arr = [];
    for ( let i = 0; i < str.length; i++ ) {
        arr.push(parseInt(str[i], 10));
    }
    return arr;
}

function print(x) {
    process.stdout.write(x + "");
}
/**
 * @param {number} n
 * @param {number[][]} matrix
 * @return {number[][]} 
 */
function rotateImage(n, matrix) {
    //implement this function
}


function main() {
    let n = parseInt(readLine());
    let matrix = [];

    for(let i=0;i<n;i++) {
        matrix.push(readIntArr());
    }

    let rotatedImage = rotateImage(n, matrix);
    let resultStr = "";

    for(let i in rotatedImage) {
        for(let j in rotatedImage[i]) {
            resultStr += rotatedImage[i][j] + " ";
        }
        resultStr += '\n';
    }

    print(resultStr);
}
Leave a Comment