You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise).
Swap by row (first_row++, last_row--)
Swap elements by diagnals
/* * clockwise rotate * first reverse up to down, then swap the symmetry * 1 2 3 7 8 9 7 4 1 * 4 5 6 => 4 5 6 => 8 5 2 * 7 8 9 1 2 3 9 6 3*/publicvoidrotate(int[][] matrix) {int first =0, last =matrix.length-1;// swap by rowwhile(first < last){// swapRow(matrix[first++], matrix[last--]);int[] tmp = matrix[first]; matrix[first] = matrix[last]; matrix[last] = tmp; first++; last--; }// swap element in matrix by diagnalfor(int i=0; i<matrix.length; i++){for(int j=0; j<i; j++){int tmp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = tmp;// swapElement(matrix[i][j], matrix[j][i]); } }
836 Rectangle Overlap
Given two rectangles, find if the given two rectangles overlap or not.
l1: Top Left coordinate of first rectangle. r1: Bottom Right coordinate of first rectangle. l2: Top Left coordinate of second rectangle. r2: Bottom Right coordinate of second rectangle.
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. Move in-space without using extra array.