Skip to content

Commit 353957b

Browse files
committed
[Function add]
1.SunProblems. 2.Use list to realize a bag frame.
1 parent f52dbe1 commit 353957b

File tree

4 files changed

+138
-1
lines changed

4 files changed

+138
-1
lines changed

Algorithm(4th_Edition)/algorithm_note.txt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191
1.a在堆上开辟了一块空间,用于存储2018-1-1.
9292
2.b在堆上开辟了一块空间,用于存储2018-3-13.
9393
3.a指向b所对应的空间,此时2018-1-1对应的空间则没有任何的引用成为孤儿对象,则会被JVM的垃圾回收机制(Garbage Collection,GC)回收。
94+
->垃圾回收的时间是难以预估的。
95+
->没有引用的对象(或者说没有办法访问到的对象),被JVM视为孤儿对象,将会被GC回收。
9496

9597
9. 背包***:
9698
接口:ca.mcmaster.chapter.one.bag.Bag<T>
@@ -102,6 +104,40 @@
102104
Integer size();
103105
}
104106

107+
->链表实现背包:
108+
实现:ca.mcmaster.chapter.one.bag.ListBag<T>
109+
public class ListBag<T> implements Bag<T>{
110+
private Node first;
111+
private Integer size = 0;
112+
private class Node{
113+
T t;
114+
Node next;
115+
}
116+
public void add(T t) {
117+
Node oldFirst = first;
118+
first = new Node();
119+
first.t = t;
120+
first.next = oldFirst;
121+
size++;
122+
}
123+
public Boolean isEmpty() { return size.equals(0); };
124+
public Integer size() { return size; }
125+
public Iterator<T> iterator() {
126+
return new ListIterator();
127+
}
128+
private class ListIterator implements Iterator<T>{
129+
private Node current = first;
130+
public boolean hasNext() { return current != null; }
131+
public T next() {
132+
if(!hasNext()) throw new IndexOutOfBoundsException();
133+
T t = current.t;
134+
current = current.next;
135+
return t;
136+
}
137+
public void remove() {}
138+
}
139+
}
140+
105141
10. FIFO队列:
106142
接口:ca.mcmaster.chapter.one.FIFO.FifoQueue<T>
107143
public interface FifoQueue<T> extends Iterable<T> {
@@ -259,4 +295,23 @@
259295
System.out.println(value.pop());
260296
}
261297
}
298+
299+
14. 算法分析:
300+
增长数量级函数(大到小)
301+
指数级别 > 立方级别 > 平方级别 || > 线性对数(NlogN)> 线性级别 > || 对数级别 > 常数级别
302+
303+
15. Sum问题:
304+
给定一组不同数组,找其中为和为0的组合的个数:
305+
->TwoSum:
306+
1.BrutalTwoSum:O(N^2)
307+
2.FastTwoSum:O(NlogN)
308+
实现:ca.mcmaster.chapter.one.sum.SumProblems#twoSumFast(Integer[])
309+
1.对数组进行排序。
310+
2.对于所有元素进行遍历O(N)
311+
3.通过二分法寻找相反数,如果相反数对应的位置大于当前的index则说明一对TwoSum存在。
312+
->ThreeSum:
313+
实现:ca.mcmaster.chapter.one.sum.SumProblems#threeSumFast(Integer[])
314+
1.对数组进行排序。
315+
2.对数组进行二维遍历O(N^2)。
316+
3.通过二分法寻找a[i]+a[j]相反数,如果相反数对应的位置大于当前的index则说明一对ThreeSum存在。
262317

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/one/Recursive.java renamed to Algorithm(4th_Edition)/src/ca/mcmaster/chapter/one/BinarySearch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ca.mcmaster.chapter.one;
22

3-
public class Recursive {
3+
public class BinarySearch {
44
private static int rank(int key, int[] a){
55
return rank(key, a, 0, a.length - 1);
66
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package ca.mcmaster.chapter.one.bag;
2+
3+
import java.util.Iterator;
4+
5+
public class ListBag<T> implements Bag<T>{
6+
private Node first;
7+
private Integer size = 0;
8+
private class Node{
9+
T t;
10+
Node next;
11+
}
12+
public void add(T t) {
13+
Node oldFirst = first;
14+
first = new Node();
15+
first.t = t;
16+
first.next = oldFirst;
17+
size++;
18+
}
19+
public Boolean isEmpty() { return size.equals(0); };
20+
public Integer size() { return size; }
21+
public Iterator<T> iterator() {
22+
return new ListIterator();
23+
}
24+
private class ListIterator implements Iterator<T>{
25+
private Node current = first;
26+
public boolean hasNext() { return current != null; }
27+
public T next() {
28+
if(!hasNext()) throw new IndexOutOfBoundsException();
29+
T t = current.t;
30+
current = current.next;
31+
return t;
32+
}
33+
public void remove() {}
34+
}
35+
36+
public static void main(String[] args) {
37+
ListBag<Integer> bag = new ListBag<>();
38+
for(int i = 0; i < 10; i++){
39+
bag.add(i);
40+
}
41+
Iterator<Integer> it = bag.iterator();
42+
while (it.hasNext()) {
43+
System.out.println(it.next());
44+
}
45+
}
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ca.mcmaster.chapter.one.sum;
2+
3+
import java.util.Arrays;
4+
5+
public class SumProblems {
6+
public static int twoSumFast(Integer[] a){
7+
Arrays.sort(a);
8+
Integer length = a.length;
9+
int cnt = 0;
10+
for(int i = 0; i < length; i++){
11+
if(Arrays.binarySearch(a, -a[i]) > i){
12+
cnt++;
13+
}
14+
}
15+
return cnt;
16+
}
17+
18+
public static int threeSumFast(Integer[] a){
19+
int cnt = 0;
20+
Arrays.sort(a);
21+
Integer n = a.length;
22+
for(int i = 0; i < n; i++){
23+
for (int j = i+1; j < n; j++) {
24+
if(Arrays.binarySearch(a, -a[i]-a[j]) > j){
25+
cnt++;
26+
}
27+
}
28+
}
29+
return cnt;
30+
}
31+
32+
public static void main(String[] args) {
33+
Integer[] a = new Integer[]{-3,2,4,-2,5,-2,-2};
34+
System.out.println(threeSumFast(a));
35+
}
36+
}

0 commit comments

Comments
 (0)