Skip to content Navigation Menu Toggle navigation Sign in Appearance settings PlatformAI CODE CREATIONGitHub CopilotWrite better code with AIGitHub Copilot appDirect agents from issue to mergeMCP RegistryNewIntegrate external toolsDEVELOPER WORKFLOWSActionsAutomate any workflowCodespacesInstant dev environmentsIssuesPlan and track workCode ReviewManage code changesAPPLICATION SECURITYGitHub Advanced SecurityFind and fix vulnerabilitiesCode securitySecure your code as you buildSecret protectionStop leaks before they startEXPLOREWhy GitHubDocumentationBlogChangelogMarketplaceView all featuresSolutionsBY COMPANY SIZEEnterprisesSmall and medium teamsStartupsNonprofitsBY USE CASEApp ModernizationDevSecOpsDevOpsCI/CDView all use casesBY INDUSTRYHealthcareFinancial servicesManufacturingGovernmentView all industriesView all solutionsResourcesEXPLORE BY TOPICAISoftware DevelopmentDevOpsSecurityView all topicsEXPLORE BY TYPECustomer storiesEvents & webinarsEbooks & reportsBusiness insightsGitHub SkillsSUPPORT & SERVICESDocumentationCustomer supportCommunity forumTrust centerPartnersView all resourcesOpen SourceCOMMUNITYGitHub SponsorsFund open source developersPROGRAMSSecurity LabMaintainer CommunityAcceleratorGitHub StarsArchive ProgramREPOSITORIESTopicsTrendingCollectionsEnterpriseENTERPRISE SOLUTIONSEnterprise platformAI-powered developer platformAVAILABLE ADD-ONSGitHub Advanced SecurityEnterprise-grade security featuresCopilot for BusinessEnterprise-grade AI featuresPremium SupportEnterprise-grade 24/7 supportPricing Search or jump to... Search code, repositories, users, issues, pull requests... Search Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. Include my email address so I can be contacted Saved searches Use saved searches to filter your results more quickly Name Query To see all available qualifiers, see our documentation. Sign in Sign up Appearance settings Resetting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} rajeevranjancom / Leetcode_Java Public Notifications You must be signed in to change notification settings Fork 0 Star 0 Code Issues 0 Pull requests 0 Actions Projects Security and quality 0 Insights Additional navigation options Code Issues Pull requests Actions Projects Security and quality Insights FilesExpand file tree masterBreadcrumbsLeetcode_Java/048_RotateImage48.javaCopy pathBlameMore file actionsBlameMore file actions Latest commit HistoryHistoryHistory193 lines (162 loc) · 4.21 KB masterBreadcrumbsLeetcode_Java/048_RotateImage48.javaCopy pathTopFile metadata and controlsCodeBlame193 lines (162 loc) · 4.21 KBRawCopy raw fileDownload raw fileOpen symbols panelEdit and raw actions123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193class Solution {public void rotate(int[][] matrix) {for(int i=0;i<matrix.length;i++){for(int j=i+1;j<matrix.length;j++){int temp=matrix[i][j];matrix[i][j]=matrix[j][i];matrix[j][i]=temp;}}for(int i=0;i<matrix.length;i++){int low=0,high=matrix.length-1;while(low<high){int temp=matrix[i][low];matrix[i][low]=matrix[i][high];matrix[i][high]=temp;low++;high--; } }}} ************************************************************ class Solution { public void rotate(int[][] matrix) { int temp; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < i; j++) { temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } int h_ptr = 0, t_ptr = matrix.length - 1; for (int i = 0; i < matrix.length; i++) { while (h_ptr < t_ptr) { temp = matrix[i][h_ptr]; matrix[i][h_ptr] = matrix[i][t_ptr]; matrix[i][t_ptr] = temp; h_ptr++; t_ptr--; } h_ptr = 0; t_ptr = matrix.length - 1; } }} **************************************************************** /* Video explanation: https://youtu.be/6SohPBdwU94*/class Solution { /* [1,2,3], [4,5,6], [7,8,9] mirror around secondary diagonal [9, 6, 3], [8, 5, 2], [7, 4, 1] mirror horizontally around middle line [7, 4, 1], [8, 5, 2], [9, 6, 3] */ /* [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [15,14,12,16] mirror around secondary diagonal [16, 7, 10, 11], [12, 6, 8, 9], [14, 3, 4, 1], [15, 13, 2, 5] mirror horizontally around middle line ... */ public void rotate(int[][] matrix) { mirrorSecondaryDiagonal(matrix); mirrorHorizontally(matrix); } private static void mirrorSecondaryDiagonal(int[][] matrix) { // flip second diagonaly for(int i = 0; i < matrix.length; i++) { for(int j = 0; j < matrix.length - i - 1; j++) { int current = matrix[i][j]; matrix[i][j] = matrix[matrix.length - j - 1][matrix.length - i - 1]; matrix[matrix.length - j - 1][matrix.length - i - 1] = current; } } } // flip horizontally private static void mirrorHorizontally(int[][] matrix) { for(int i = 0; i < matrix.length / 2; i++) { for(int j = 0; j < matrix.length; j++) { int current = matrix[i][j]; matrix[i][j] = matrix[matrix.length - i - 1][j]; matrix[matrix.length - i - 1][j] = current; } } }} ************************************************************************ class Solution { public void rotate(int[][] matrix) { int rowUpper = 0; int rowLower = matrix.length - 1; int colLeft = 0; int colRight = matrix.length - 1; while(rowUpper<rowLower && colLeft<colRight) { rotate(matrix, rowUpper, rowLower, colLeft, colRight); rowUpper++; rowLower--; colLeft++; colRight--; } } public static void rotate(int[][] matrix, int rowUpper, int rowLower, int colLeft, int colRight) { int k=0; for(int i=colLeft; i<colRight; i++) { int tmp1 = matrix[rowUpper+k][colRight]; matrix[rowUpper+k][colRight] = matrix[rowUpper][i]; int tmp2 = matrix[rowLower][colRight-k]; matrix[rowLower][colRight-k] = tmp1; tmp1 = matrix[rowLower-k][colLeft]; matrix[rowLower-k][colLeft] = tmp2; matrix[rowUpper][i] = tmp1; k++; } }} ***************************************************** class Solution { public int[] reverse(int[] array) { int n = array.length; int[] b = new int[n]; int j = n; for (int i = 0; i < n; i++) { b[j - 1] = array[i]; j = j - 1; } return b; } public void rotate(int[][] matrix) { int N = matrix.length; int M = matrix[0].length; for (int i = 0; i < N; i++) { for (int j = i+1; j < M; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } for(int i=0; i<N; i++) { matrix[i] = reverse(matrix[i]); } }} You can’t perform that action at this time.