-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBoundaryElements.java
30 lines (29 loc) · 986 Bytes
/
BoundaryElements.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*https://practice.geeksforgeeks.org/problems/boundary-elements-of-matrix1102/1*/
class Solution
{
public int[] BoundaryElements(int[][] matrix)
{
// code here
int[] result;
if (matrix.length == 1 && matrix[0].length == 1)
{
result = new int[1];
result[0] = matrix[0][0];
return result;
}
ArrayList<Integer> resultList = new ArrayList<Integer>();
for (int i = 0; i < matrix[0].length; ++i)
resultList.add(matrix[0][i]);
for (int i = 1; i < matrix.length-1; ++i)
{
resultList.add(matrix[i][0]);
resultList.add(matrix[i][matrix[0].length-1]);
}
for (int i = 0; i < matrix[0].length; ++i)
resultList.add(matrix[matrix.length-1][i]);
result = new int[resultList.size()];
for (int i = 0; i < result.length; ++i)
result[i] = (Integer)resultList.get(i);
return result;
}
}