You are on page 1of 7

Name – Awadhesh Kumar Maurya Roll

no- 2100290100039 Cse (6)A

Leetcode problem no 867 Transpose


Matrix

Example 2:
Input: matrix = [[1,2,3],[4,5,6]]

Output: [[1,4],[2,5],[3,6]]

Constraints:
 m == matrix.length

 n == matrix[i].length

 1 <= m, n <= 1000

1 <= m * n <= 10 5

9
<= matrix[i][j] <= 10
9
-10

Solution in java

class Solution { public int[][]

transpose(int[][] mat) { int Row =

mat.length, Col = mat[0].length; int[][] ans

= new int[Col][Row]; for (int i = 0; i<

Row; ++i) for (int j = 0; j < Col; ++j) {

ans[j][i] = mat[i][j];

return ans;

}
Leetcode problem no 48 Rotate Image
You are given an nxn 2D matrix representing an image, rotate the image by 90 degrees
(clockwise).

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.

Example 1:

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]


Output:
[[7,4,1],[8,5,2],[9,6,3]]

Example 2:
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]

Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

Code in java solution


class Solution { public void rotate(int[][]
matrix) { for (int i=0,j = matrix.length - 1;
i<j;i++,j--) { int[] temp = matrix[i];
matrix[i] = matrix[j]; matrix[j] = temp;
}

for (int i = 0; i < matrix.length; i++)


for (int j = i + 1; j < matrix.length; j++) {
final int temp = matrix[i][j];
matrix[i][j] = matrix[j][i]; matrix[j][i]
= temp;

}
}
}
Printing boundary
element
import java.util.*;

public class boundryelement {


public static void printboundry(int[][] matrix) {
int n = matrix.length;
int m = matrix[0].length;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < m; i++)
list.add(matrix[0][i]);

for (int i = 1; i < n; i++)


list.add(matrix[i][m - 1]);

for (int i = m - 2; i >= 0; i--)


list.add(matrix[n - 1][i]);

for (int i = n - 2; i > 0; i--)


list.add(matrix[i][0]);
System.out.println(list);
// return list;
}

public static void main(String[] args) {


int[][] arr = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 8, 6, 5 },
{ 4, 3, 2, 1 } };
System.out.println("The enterd array is :");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println("\n");
}
System.out.println("the boundy element is :\n");
printboundry(arr);
}
}
Prin ng snake pa ern

class Solu on
{
//Func on to return list of integers
visited in snake pa ern in matrix.
sta c ArrayList<Integer> snakePa ern(int
matrix[][])
{
// code here
ArrayList<Integer> list=new
ArrayList<>();
int row=matrix.length;
int col=matrix[0].length;
for(int i=0;i<row;i++){
if((i+2)%2==0){
for(int j=0;j<col;j++)
list.add(matrix[i][j]);
}
else{
for(int j=col-1;j>=0;j--)
list.add(matrix[i][j]);
}
}
return list;
}
}

h ps://www.geeksforgeeks.org/problems/
print-matrix-in-snake-pa ern-
1587115621/1?itm_source=geeksforgeeks
&itm_medium=ar cle&itm_campaign=bot
tom_s cky_on_ar cle

You might also like