Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
82 lines (66 loc) · 1.95 KB

File metadata and controls

82 lines (66 loc) · 1.95 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package search;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class SearchIn2DTest {
/*
TASK
정렬된 2차원 배열에서 검색한다.
1. 각 row 별로 for-loop 돌면서 O(log n)의 sort을 한다.
2. O(n)
*/
@Test
public void test() {
int[][] matrix = new int[5][5];
for (int i = 0; i < matrix[0].length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = i + j;
}
}
assertThat(getTargetPosition(matrix, 5), is(new Position(4,1)));
}
public Position getTargetPosition(int[][] matrix, int target) {
if (matrix == null) return null;
int row = matrix.length - 1;
int col = 0;
while (row >= 0 && col < matrix[0].length) {
if (matrix[row][col] == target) {
return new Position(row, col);
} else if (matrix[row][col] < target) {
col++;
} else {
row--;
}
}
return null;
}
public class Position {
int row;
int col;
public Position(int row, int col) {
this.row = row;
this.col = col;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Position position = (Position) o;
if (row != position.row) return false;
return col == position.col;
}
@Override
public int hashCode() {
int result = row;
result = 31 * result + col;
return result;
}
@Override
public String toString() {
return "Position{" +
"row=" + row +
", col=" + col +
'}';
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.