Skip to content

Commit 65ba185

Browse files
committed
apr.8
1 parent 49b9b44 commit 65ba185

12 files changed

Lines changed: 186 additions & 14 deletions
1.39 KB
Binary file not shown.
199 Bytes
Binary file not shown.
1.5 KB
Binary file not shown.
420 Bytes
Binary file not shown.
1.35 KB
Binary file not shown.
-99 Bytes
Binary file not shown.
166 Bytes
Binary file not shown.

src/Class02_Recursion/SearchInUnknownSizedSortedArray.java

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
package Class02_Recursion;
2+
3+
import java.util.Arrays;
4+
25
/*
36
* Given a integer dictionary A of unknown size, where the numbers
47
* in the dictionary are sorted in ascending order, determine if a
@@ -14,10 +17,6 @@
1417
* A = {1, 2, 5, 9, 12, ......}, T = 7, return -1
1518
* */
1619

17-
18-
interface Dictionary {
19-
public Integer get(int index);
20-
}
2120
// Solution:
2221
// Step1: keep jumping out until array[2^k] >= target || array[2^k] == null
2322
// Step2: left = 2^(k - 1); right = 2^k; do binary search.
@@ -33,12 +32,12 @@ public int search(Dictionary dict, int target) {
3332
while(dict.get(index) != null && dict.get(index) < target) {
3433
index = index * 2;
3534
}
36-
return BST(dict, target, (int)Math.sqrt(index),index);
35+
return BST(dict, target, index/2,index);
3736
}
3837
private int BST(Dictionary dict, int target, int l, int r) {
3938

4039
while (l <= r) {
41-
int mid = l + (l + r) / 2;
40+
int mid = l + (r - l) / 2;
4241
if (dict.get(mid) == null) {
4342
r = mid - 1;
4443
} else if (dict.get(mid) == target) {
@@ -52,7 +51,48 @@ private int BST(Dictionary dict, int target, int l, int r) {
5251
return -1;
5352
}
5453
public static void main(String[] args) {
55-
54+
SearchInUnknownSizedSortedArray sol = new SearchInUnknownSizedSortedArray();
55+
Dictionary dict = new DictImpl(null);
56+
System.out.println(sol.search(dict, 4));
5657
}
5758

5859
}
60+
interface Dictionary {
61+
public Integer get(int index);
62+
}
63+
class DictImpl implements Dictionary {
64+
private int[] array;
65+
public DictImpl(int[] input) {
66+
// TODO Auto-generated constructor stub
67+
array = input;
68+
}
69+
@Override
70+
public Integer get(int index) {
71+
// TODO Auto-generated method stub
72+
if(array == null || index >= array.length) {
73+
return null;
74+
}
75+
return array[index];
76+
}
77+
@Override
78+
public String toString() {
79+
if(array == null) {
80+
return String.valueOf(null);
81+
}
82+
if(array.length < 10) {
83+
return Arrays.toString(array);
84+
}
85+
// truncate array
86+
StringBuilder sb = new StringBuilder("[");
87+
for (int i = 0; i < 5; i++) {
88+
sb.append(array[i]).append(", ");
89+
}
90+
sb.append("..........");
91+
for (int j = array.length - 5; j < array.length; j++) {
92+
sb.append(array[j]).append(", ");
93+
}
94+
sb.replace(sb.length()-2, sb.length()-1, "]");
95+
return sb.toString();
96+
}
97+
98+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package Class03_List;
2+
3+
import java.util.LinkedList;
4+
5+
/*
6+
* Java: Implement a queue by using two stacks. The queue should provide size(),
7+
* isEmpty(), offer(), poll() and peek() operations. When the queue is empty,
8+
* poll() and peek() should return null.
9+
*
10+
* Assumptions
11+
* The elements in the queue are all Integers.
12+
* size() should return the number of elements buffered in the queue.
13+
* isEmpty() should return true if there is no element buffered in the queue, false otherwise.
14+
* */
15+
16+
public class QueueByTwoStacks {
17+
private LinkedList<Integer> in;
18+
private LinkedList<Integer> out;
19+
20+
public QueueByTwoStacks() {
21+
in = new LinkedList<Integer>();
22+
out = new LinkedList<Integer>();
23+
}
24+
25+
public Integer poll() {
26+
move();
27+
return out.isEmpty() ? null : out.pollFirst();
28+
}
29+
30+
public void offer(int element) {
31+
in.offerFirst(element);
32+
}
33+
34+
public Integer peek() {
35+
move();
36+
return out.isEmpty() ? null : out.peekFirst();
37+
}
38+
39+
public int size() {
40+
return in.size() + out.size();
41+
}
42+
43+
public boolean isEmpty() {
44+
return in.isEmpty() && out.isEmpty();
45+
}
46+
private void move() {
47+
if(out.isEmpty()) {
48+
while(!in.isEmpty()) {
49+
out.offerFirst(in.pollFirst());
50+
}
51+
}
52+
}
53+
54+
public static void main(String[] args) {
55+
// TODO Auto-generated method stub
56+
57+
}
58+
59+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package Class03_List;
2+
3+
public class ReverseLinkedList {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
8+
}
9+
10+
}

0 commit comments

Comments
 (0)