Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit0eb294b

Browse files
authored
Update Leftmost Column with at Least a One.java
1 parent7518ae0 commit0eb294b

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

‎Medium/Leftmost Column with at Least a One.java

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,25 @@
22
* // This is the BinaryMatrix's API interface.
33
* // You should not implement it, or speculate about its implementation
44
* interface BinaryMatrix {
5-
* public int get(intx, inty) {}
5+
* public int get(introw, intcol) {}
66
* public List<Integer> dimensions {}
77
* };
88
*/
99

1010
classSolution {
1111
publicintleftMostColumnWithOne(BinaryMatrixbinaryMatrix) {
12-
List<Integer>dims =binaryMatrix.dimensions();
13-
introws =dims.get(0);
14-
intcols =dims.get(1);
15-
intminIdx =Integer.MAX_VALUE;
16-
for (inti =0;i <rows;i++) {
17-
intidx =binarySearch(binaryMatrix,i,0,cols -1);
18-
if (idx != -1) {
19-
minIdx =Math.min(minIdx,idx);
12+
List<Integer>dimension =binaryMatrix.dimensions();
13+
intnumRows =dimension.get(0);
14+
intnumCols =dimension.get(1);
15+
intcurrRow =0;
16+
intcurrCol =numCols -1;
17+
while (currRow <numRows &&currCol >=0) {
18+
if (binaryMatrix.get(currRow,currCol) ==0) {
19+
currRow++;
20+
}else {
21+
currCol--;
2022
}
2123
}
22-
returnminIdx ==Integer.MAX_VALUE ? -1 :minIdx;
23-
}
24-
25-
privateintbinarySearch(BinaryMatrixbinaryMatrix,introw,intstart,intend) {
26-
intminIdx =Integer.MAX_VALUE;
27-
while (start <=end) {
28-
intmid = (start +end) /2;
29-
if (binaryMatrix.get(row,mid) ==1) {
30-
minIdx =Math.min(minIdx,mid);
31-
end =mid -1;
32-
}
33-
else {
34-
start =mid +1;
35-
}
36-
}
37-
returnminIdx ==Integer.MAX_VALUE ? -1 :minIdx;
24+
returncurrCol ==numCols -1 ? -1 :currCol +1;
3825
}
3926
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp