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

Commit03fcafa

Browse files
author
piggy1991
committed
example
1 parent7a9a84b commit03fcafa

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
packageAlgorithms.algorithm.interviews.uber;
2+
3+
publicclassExample {
4+
5+
}

‎divide2/Sqrt.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
packageAlgorithms.divide2;
2+
3+
publicclassSqrt {
4+
publicintsqrt(intx) {
5+
if (x ==1 ||x ==0) {
6+
returnx;
7+
}
8+
9+
intleft =1;
10+
intright =x;
11+
12+
while (left <right -1) {
13+
intmid =left + (right -left) /2;
14+
intquo =x /mid;
15+
16+
if (quo ==mid) {
17+
returnquo;
18+
// mid is too big
19+
}elseif (quo <mid) {
20+
right =mid;
21+
}else {
22+
left =mid;
23+
}
24+
}
25+
26+
returnleft;
27+
}
28+
}

‎sort/BucketSorter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
packageAlgorithms.sort;
22

3-
importjava.util.Arrays;
43

54
/**
65
* @author yovn

‎tree/BuildTree.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
packageAlgorithms.tree;
2+
3+
/**
4+
* Definition for binary tree
5+
* public class TreeNode {
6+
* int val;
7+
* TreeNode left;
8+
* TreeNode right;
9+
* TreeNode(int x) { val = x; }
10+
* }
11+
*/
12+
publicclassBuildTree {
13+
publicstaticvoidmain(String[]strs) {
14+
int[]pre = {1,2};
15+
int[]in = {2,1};
16+
17+
buildTree(pre,in);
18+
}
19+
20+
publicstaticTreeNodebuildTree(int[]preorder,int[]inorder) {
21+
// bug 3: consider when length is 0.
22+
if (preorder ==null ||inorder ==null ||preorder.length ==0 ||preorder.length !=inorder.length) {
23+
returnnull;
24+
}
25+
26+
// bug 4: end index is length - 1.
27+
returnbuildTree(preorder,inorder,0,preorder.length -1,0,preorder.length -1);
28+
}
29+
30+
publicstaticTreeNodebuildTree(int[]preorder,int[]inorder,intpreStart,intpreEnd,intinStart,intinEnd) {
31+
// base case;
32+
if (preStart >preEnd) {
33+
returnnull;
34+
}
35+
36+
introotVal =preorder[preStart];
37+
TreeNoderoot =newTreeNode(rootVal);
38+
39+
intpos =findTarget(inorder,rootVal,inStart,inEnd);
40+
41+
// bug 5: left number is pos - instart can't add 1
42+
intleftNum =pos -inStart;
43+
44+
root.left =buildTree(preorder,inorder,preStart +1,preStart +leftNum,inStart,pos -1);
45+
root.right =buildTree(preorder,inorder,preStart +leftNum +1,preEnd,pos +1,inEnd);
46+
47+
returnroot;
48+
}
49+
50+
// bug 1: return type required.
51+
publicstaticintfindTarget(int[]A,inttarget,intstart,intend) {
52+
for (inti =start;i <=end;i++) {
53+
if (target ==A[i]) {
54+
returni;
55+
}
56+
}
57+
58+
return -1;
59+
}
60+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp