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

Commitebb4be3

Browse files
author
piggy1991
committed
search
1 parentb94329c commitebb4be3

File tree

4 files changed

+167
-1
lines changed

4 files changed

+167
-1
lines changed

‎array/SetZeroes.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public static void setZeroes(int[][] matrix) {
4141
// cells whether or not set to 0.
4242
for (inti =1;i <rows;i++) {
4343
for (intj =1;j <cols;j++) {
44-
// 注意了,这个矩阵是0和非0,并不是0和1.
4544
if (matrix[i][j] ==0) {
4645
// set the flag in the first line and the first column
4746
matrix[i][0] =0;
@@ -77,4 +76,64 @@ public static void setZeroes(int[][] matrix) {
7776

7877
return;
7978
}
79+
80+
// solution 2: combine the first 3 loop.
81+
publicvoidsetZeroes2(int[][]matrix) {
82+
if (matrix ==null ||matrix.length ==0 ||matrix[0].length ==0) {
83+
return;
84+
}
85+
86+
booleanrow1 =false;
87+
booleancol1 =false;
88+
89+
introws =matrix.length;
90+
intcols =matrix[0].length;
91+
92+
// set flags.
93+
for (inti =0;i <rows;i++) {
94+
for (intj =0;j <cols;j++) {
95+
if (matrix[i][j] !=0) {
96+
continue;
97+
}
98+
99+
// set the flag of a column and a row.
100+
matrix[0][j] =0;
101+
matrix[i][0] =0;
102+
103+
// get flag of first row.
104+
if (i ==0) {
105+
row1 =true;
106+
}
107+
108+
// get flag of first column.
109+
if (j ==0) {
110+
col1 =true;
111+
}
112+
}
113+
}
114+
115+
// set the matrix.
116+
for (inti =1;i <rows;i++) {
117+
for (intj =1;j <cols;j++) {
118+
if (matrix[0][j] ==0 ||matrix[i][0] ==0) {
119+
matrix[i][j] =0;
120+
}
121+
}
122+
}
123+
124+
// set first column
125+
if (col1) {
126+
for (inti =0;i <rows;i++) {
127+
// bug 1: can't use matrix[i][j]
128+
matrix[i][0] =0;
129+
}
130+
}
131+
132+
// set first row.
133+
if (row1) {
134+
for (inti =0;i <cols;i++) {
135+
matrix[0][i] =0;
136+
}
137+
}
138+
}
80139
}

‎divide2/SearchMatrix.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
packageAlgorithms.divide2;
2+
3+
publicclassSearchMatrix {
4+
publicbooleansearchMatrix(int[][]matrix,inttarget) {
5+
if (matrix ==null ||matrix.length ==0 ||matrix[0].length ==0) {
6+
returnfalse;
7+
}
8+
9+
introws =matrix.length;
10+
intcols =matrix[0].length;
11+
12+
intnum =rows *cols;
13+
14+
intleft =0;
15+
intright =num -1;
16+
17+
while (left <=right) {
18+
intmid =left + (right -left) /2;
19+
20+
introw =mid /cols;
21+
intcol =mid %cols;
22+
23+
intn =matrix[row][col];
24+
25+
if (n ==target) {
26+
returntrue;
27+
}elseif (n <target) {
28+
left =mid +1;
29+
}else {
30+
right =mid -1;
31+
}
32+
}
33+
34+
returnfalse;
35+
}
36+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
packageAlgorithms.lintcode.array;
2+
3+
/**
4+
* public class VersionControl {
5+
* public static boolean isBadVersion(int k);
6+
* }
7+
* you can use VersionControl.isBadVersion(k) to judge wether
8+
* the kth code version is bad or not.
9+
*/
10+
classSolution {
11+
/**
12+
* @param n: An integers.
13+
* @return: An integer which is the first bad version.
14+
*/
15+
publicintfindFirstBadVersion(intn) {
16+
// write your code here
17+
if (n ==1) {
18+
return1;
19+
}
20+
21+
intleft =1;
22+
intright =n;
23+
24+
while (left +1 <right) {
25+
intmid =left + (right -left) /2;
26+
if (VersionControl.isBadVersion(mid)) {
27+
right =mid;
28+
}else {
29+
left =mid;
30+
}
31+
}
32+
33+
if (VersionControl.isBadVersion(left)) {
34+
returnleft;
35+
}
36+
37+
returnright;
38+
}
39+
40+
// solution 2.
41+
publicintfindFirstBadVersion2(intn) {
42+
// write your code here
43+
if (n ==1) {
44+
return1;
45+
}
46+
47+
intleft =1;
48+
intright =n;
49+
50+
while (left <right) {
51+
intmid =left + (right -left) /2;
52+
if (VersionControl.isBadVersion(mid)) {
53+
right =mid;
54+
}else {
55+
left =mid +1;
56+
}
57+
}
58+
59+
returnright;
60+
}
61+
}

‎lintcode/array/VersionControl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
packageAlgorithms.lintcode.array;
2+
3+
publicclassVersionControl {
4+
5+
publicstaticbooleanisBadVersion(intmid) {
6+
// TODO Auto-generated method stub
7+
returnfalse;
8+
}
9+
10+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp