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

Commitbd5e354

Browse files
committed
water
1 parentf299cda commitbd5e354

File tree

7 files changed

+310
-9
lines changed

7 files changed

+310
-9
lines changed

‎array/Trap.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
packageAlgorithms.array;
22

33
publicclassTrap {
4-
publicinttrap(int[]A) {
4+
publicinttrap1(int[]A) {
55
if (A ==null) {
66
return0;
77
}
@@ -32,4 +32,37 @@ public int trap(int[] A) {
3232

3333
returnmax;
3434
}
35+
36+
publicinttrap(int[]A) {
37+
// 2:37
38+
if (A ==null) {
39+
return0;
40+
}
41+
42+
intlen =A.length;
43+
int[]l =newint[len];
44+
int[]r =newint[len];
45+
46+
for (inti =0;i <len;i++) {
47+
if (i ==0) {
48+
l[i] =A[i];
49+
}else {
50+
l[i] =Math.max(l[i -1],A[i]);
51+
}
52+
}
53+
54+
intwater =0;
55+
for (inti =len -1;i >=0;i--) {
56+
if (i ==len -1) {
57+
r[i] =A[i];
58+
}else {
59+
// but: use Math, not max
60+
r[i] =Math.max(r[i +1],A[i]);
61+
}
62+
63+
water +=Math.min(l[i],r[i]) -A[i];
64+
}
65+
66+
returnwater;
67+
}
3568
}

‎dp/CanJump.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,28 @@ public static boolean canJump(int[] A, int index) {
7979

8080
returnfalse;
8181
}
82+
83+
// greedy.
84+
publicbooleancanJump4(int[]A) {
85+
// 4:42
86+
if (A ==null) {
87+
returnfalse;
88+
}
89+
90+
intlen =A.length;
91+
92+
intright =0;
93+
for (inti =0;i <A.length;i++) {
94+
right =Math.max(right,i +A[i]);
95+
if (right ==len -1) {
96+
returntrue;
97+
}
98+
99+
if (i ==right) {
100+
returnfalse;
101+
}
102+
}
103+
104+
returntrue;
105+
}
82106
}

‎greedy/Jump.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public static void main(String[] strs) {
66
System.out.println(jump(A));
77
}
88

9-
publicstaticintjump(int[]A) {
9+
publicstaticintjump1(int[]A) {
1010
if (A ==null ||A.length ==0) {
1111
return0;
1212
}
@@ -31,4 +31,48 @@ public static int jump(int[] A) {
3131

3232
returnsum;
3333
}
34+
35+
// solution 2: one pass greedy.
36+
publicstaticintjump(int[]A) {
37+
if (A ==null ||A.length ==0) {
38+
return0;
39+
}
40+
41+
// bug:
42+
/*
43+
Input: [1]
44+
Output: 1
45+
Expected: 0
46+
*/
47+
if (A.length ==1) {
48+
return0;
49+
}
50+
51+
intlen =A.length;
52+
53+
intstart =0;
54+
intend =0;
55+
56+
intsteps =0;
57+
while (end <len -1) {
58+
intmax =0;
59+
steps++;
60+
for (inti =start;i <=end;i++) {
61+
max =Math.max(max,i +A[i]);
62+
63+
if (max >=len -1) {
64+
returnsteps;
65+
}
66+
}
67+
68+
start =end +1;
69+
end =max;
70+
71+
if (start >end) {
72+
break;
73+
}
74+
}
75+
76+
returnInteger.MAX_VALUE;
77+
}
3478
}

‎hash/SolveSudoku.java

Lines changed: 99 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
packageAlgorithms.hash;
22

33
publicclassSolveSudoku {
4-
publicvoidsolveSudoku(char[][]board) {
5-
dfs(board,0,0);
4+
publicstaticvoidmain(String[]args) {
5+
char[][]board = {
6+
{'.','.','9','7','4','8','.','.','.'},
7+
{'7','.','.','.','.','.','.','.','.'},
8+
{'.','2','.','1','.','9','.','.','.'},
9+
{'.','.','7','.','.','.','2','4','.'},
10+
{'.','6','4','.','1','.','5','9','.'},
11+
{'.','9','8','.','.','.','3','.','.'},
12+
{'.','.','.','8','.','3','.','2','.'},
13+
{'.','.','.','.','.','.','.','.','6'},
14+
{'.','.','.','2','7','5','9','.','.'},
15+
};
16+
solveSudoku(board);
17+
for(inti=0;i<9;i++){
18+
for(intj=0;j<9;j++){
19+
System.out.print(board[i][j]);
20+
}
21+
System.out.println();
22+
}
23+
}
24+
25+
publicvoidsolveSudoku1(char[][]board) {
26+
dfs1(board,0,0);
627
}
728

8-
publicbooleandfs(char[][]board,intx,inty) {
29+
publicbooleandfs1(char[][]board,intx,inty) {
930
// go to next row.
1031
if (y ==9) {
1132
y =0;
@@ -19,14 +40,14 @@ public boolean dfs(char[][] board, int x, int y) {
1940

2041
// Skip the solved point.
2142
if (board[x][y] !='.') {
22-
returndfs(board,x,y +1);
43+
returndfs1(board,x,y +1);
2344
}
2445

2546
// Go throught all the possibilities.
2647
for (intk =0;k <9;k++) {
2748
board[x][y] = (char)('1' +k);
2849
// SHOULD RETURN HERE IF INVALID.
29-
if (isValid(board,x,y) &&dfs(board,x,y +1)) {
50+
if (isValid1(board,x,y) &&dfs1(board,x,y +1)) {
3051
returntrue;
3152
}
3253
board[x][y] ='.';
@@ -36,7 +57,7 @@ public boolean dfs(char[][] board, int x, int y) {
3657
returnfalse;
3758
}
3859

39-
publicbooleanisValid(char[][]board,intx,inty) {
60+
publicbooleanisValid1(char[][]board,intx,inty) {
4061
// Judge the column.
4162
for (inti =0;i <9;i++) {
4263
if (i !=x &&board[i][y] ==board[x][y]) {
@@ -68,4 +89,76 @@ public boolean isValid(char[][] board, int x, int y) {
6889

6990
returntrue;
7091
}
92+
93+
// solution 2:
94+
publicstaticvoidsolveSudoku(char[][]board) {
95+
// 3:01
96+
if (board ==null ||board.length ==0 ||board[0].length ==0) {
97+
return;
98+
}
99+
100+
dfs(board,0,0);
101+
}
102+
103+
publicstaticbooleandfs(char[][]board,intx,inty) {
104+
// 3:01
105+
// next row.
106+
if (y >=9) {
107+
returndfs(board,x +1,0);
108+
}
109+
110+
if (x >=9) {
111+
returntrue;
112+
}
113+
114+
// skip the number.
115+
if (board[x][y] !='.') {
116+
returndfs(board,x,y +1);
117+
}
118+
119+
// solve the current node.
120+
for (charc ='0';c <='9';c++) {
121+
board[x][y] =c;
122+
if (isValid(board,x,y,c) &&dfs(board,x,y +1)) {
123+
returntrue;
124+
}
125+
board[x][y] ='.';
126+
}
127+
128+
returnfalse;
129+
}
130+
131+
publicstaticbooleanisValid(char[][]board,intx,inty,charc) {
132+
// the current row.
133+
for (inti =0;i <9;i++) {
134+
if (y !=i &&c ==board[x][i]) {
135+
returnfalse;
136+
}
137+
}
138+
139+
// the current col.
140+
for (inti =0;i <9;i++) {
141+
// BUG: should use board[i][y]
142+
if (x !=i &&c ==board[i][y]) {
143+
returnfalse;
144+
}
145+
}
146+
147+
// the current block.
148+
intstartX =x /3 *3;
149+
intstartY =y /3 *3;
150+
for (intk =0;k <9;k++) {
151+
intindexX =startX +k /3;
152+
intindexY =startY +k %3;
153+
if (indexX ==x &&indexY ==y) {
154+
continue;
155+
}
156+
157+
if (board[indexX][indexY] ==c) {
158+
returnfalse;
159+
}
160+
}
161+
162+
returntrue;
163+
}
71164
}

‎permutation/NextPermutation.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,41 @@ public void swap(int[] num, int i, int j) {
5252
num[i] =num[j];
5353
num[j] =tmp;
5454
}
55+
56+
// Solution 2:
57+
publicvoidnextPermutation2(int[]num) {
58+
if (num ==null) {
59+
return;
60+
}
61+
62+
intlen =num.length;
63+
64+
// Find the index which drop.
65+
intdropIndex = -1;
66+
for (inti =len -1;i >=0;i--) {
67+
if (i !=len -1 &&num[i] <num[i +1]) {
68+
dropIndex =i;
69+
break;
70+
}
71+
}
72+
73+
// replace the drop index.
74+
if (dropIndex != -1) {
75+
for (inti =len -1;i >=0;i--) {
76+
if (num[i] >num[dropIndex]) {
77+
swap(num,dropIndex,i);
78+
break;
79+
}
80+
}
81+
}
82+
83+
// reverse the link.
84+
intl =dropIndex +1;
85+
intr =len -1;
86+
while (l <r) {
87+
swap(num,l,r);
88+
l++;
89+
r--;
90+
}
91+
}
5592
}

‎permutation/PermutationSequence.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
packageAlgorithms.permutation;
22

3+
importjava.util.LinkedList;
4+
35
/*
46
The set [1,2,3,…,n] contains a total of n! unique permutations.
57
@@ -18,7 +20,7 @@ We get the following sequence (ie, for n = 3):
1820
Note: Given n will be between 1 and 9 inclusive.
1921
* */
2022
publicclassPermutationSequence {
21-
publicstaticStringgetPermutation(intn,intk) {
23+
publicstaticStringgetPermutation1(intn,intk) {
2224
if (n ==0) {
2325
return"";
2426
}
@@ -65,5 +67,39 @@ public static String getPermutation(int n, int k) {
6567
publicstaticvoidmain(String[]args) {
6668
System.out.println(getPermutation(3,5));
6769
}
70+
71+
publicstaticStringgetPermutation(intn,intk) {
72+
// 1:17 -> 1:43
73+
LinkedList<Character>digits =newLinkedList<Character>();
74+
75+
// bug 2: should only add n elements.
76+
for (chari ='1';i <='0' +n;i++) {
77+
digits.add(i);
78+
}
79+
80+
// The index start from 0;
81+
k--;
82+
StringBuildersb =newStringBuilder();
83+
84+
intsum =1;
85+
// n!
86+
for (inti =1;i <=n;i++) {
87+
sum *=i;
88+
}
89+
90+
for (inti =n;i >=1;i--) {
91+
sum /=i;
92+
intdigitIndex =k /sum;
93+
k =k %sum;
94+
95+
//Line 25: error: cannot find symbol: method digits(int)
96+
sb.append(digits.get(digitIndex));
97+
98+
// remove the used digit.
99+
digits.remove(digitIndex);
100+
}
101+
102+
returnsb.toString();
103+
}
68104

69105
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp