Skip to content

Commit ad79e7a

Browse files
committed
ch03 stack
0 parents  commit ad79e7a

10 files changed

+661
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 数据结构与算法分析 Java语言描述
2+
3+
书中代码以及课后习题。

data-struct-java.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

src/ch02/MaxSubSum.java

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package ch02;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
6+
/**
7+
* Created by cookfront on 2017/2/14.
8+
*/
9+
public class MaxSubSum {
10+
public static void main(String ...args) {
11+
int[] subs = {-2, 11, -4, 13, -5, -2};
12+
System.out.println(maxSubSum1(subs));
13+
System.out.println(maxSubSum2(subs));
14+
System.out.println(maxSubSum4(subs));
15+
}
16+
17+
public static int maxSubSum1(int[] arr) {
18+
int maxSum = 0;
19+
20+
for (int i = 0; i < arr.length; i++) {
21+
for (int j = i; j < arr.length; j++) {
22+
int thisSum = 0;
23+
24+
for (int k = i; k <= j; k++) {
25+
thisSum += arr[k];
26+
}
27+
28+
if (thisSum > maxSum) {
29+
maxSum = thisSum;
30+
}
31+
}
32+
}
33+
34+
return maxSum;
35+
}
36+
37+
public static int maxSubSum2(int[] arr) {
38+
int maxSum = 0;
39+
40+
for (int i = 0; i < arr.length; i++) {
41+
int thisSum = 0;
42+
for (int j = i; j < arr.length; j++) {
43+
thisSum += arr[j];
44+
45+
if (thisSum > maxSum) {
46+
maxSum = thisSum;
47+
}
48+
}
49+
}
50+
51+
return maxSum;
52+
}
53+
54+
public static int maxSumRec(int[] arr, int left, int right) {
55+
if (left == right) {
56+
if (arr[left] > 0) {
57+
return arr[left];
58+
} else {
59+
return 0;
60+
}
61+
}
62+
63+
int center = (left + right / 2);
64+
int maxLeftSum = maxSumRec(arr, left, center);
65+
int maxRightSum = maxSumRec(arr, center + 1, right);
66+
67+
int maxLeftBorderSum = 0;
68+
int leftBorderSum = 0;
69+
for (int i = center; i >= left; i--) {
70+
leftBorderSum += arr[i];
71+
if (leftBorderSum > maxLeftBorderSum) {
72+
maxLeftBorderSum = leftBorderSum;
73+
}
74+
}
75+
76+
int maxRightBorderSum = 0;
77+
int rightBorderSum = 0;
78+
for (int i = center + 1; i <= right; i++) {
79+
rightBorderSum += arr[i];
80+
if (rightBorderSum > maxRightBorderSum) {
81+
maxRightBorderSum = rightBorderSum;
82+
}
83+
}
84+
85+
return max3(maxLeftSum, maxRightSum, maxLeftBorderSum + maxRightBorderSum);
86+
}
87+
88+
public static int max3(int a, int b, int c) {
89+
if (a > b && a > c) {
90+
return a;
91+
} else if (b > a && b > c) {
92+
return b;
93+
} else if (c > a && c > b) {
94+
return c;
95+
}
96+
return Integer.MIN_VALUE;
97+
}
98+
99+
public static int maxSubSum3(int[] arr) {
100+
return maxSumRec(arr, 0, arr.length - 1);
101+
}
102+
103+
public static int maxSubSum4(int[] arr) {
104+
int maxSum = 0;
105+
int thisSum = 0;
106+
107+
for (int i = 0; i < arr.length; i++) {
108+
thisSum += arr[i];
109+
110+
if (thisSum > maxSum) {
111+
maxSum = thisSum;
112+
} else if (thisSum < 0) {
113+
thisSum = 0;
114+
}
115+
}
116+
117+
return maxSum;
118+
}
119+
}

src/ch03/MyArrayList.java

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package ch03;
2+
3+
import java.lang.Iterable;
4+
import java.util.Iterator;
5+
import java.util.NoSuchElementException;
6+
7+
/**
8+
* Created by cookfront on 2017/2/20.
9+
*/
10+
public class MyArrayList<AnyType> implements Iterable<AnyType> {
11+
private static final int DEFAULT_CAPACITY = 10;
12+
13+
private int theSize;
14+
private AnyType[] theItems;
15+
16+
public MyArrayList() {
17+
doClear();
18+
}
19+
20+
public void clean() {
21+
doClear();
22+
}
23+
24+
private void doClear() {
25+
theSize = 0;
26+
ensureCapacity(DEFAULT_CAPACITY);
27+
}
28+
29+
public int size() {
30+
return theSize;
31+
}
32+
33+
public boolean isEmpty() {
34+
return size() == 0;
35+
}
36+
37+
public void trimToSize() {
38+
ensureCapacity(size());
39+
}
40+
41+
public AnyType get(int index) {
42+
if (index < 0 || index >= size()) {
43+
throw new ArrayIndexOutOfBoundsException();
44+
}
45+
return theItems[index];
46+
}
47+
48+
public AnyType set(int index, AnyType newVal) {
49+
if (index < 0 || index >= size()) {
50+
throw new ArrayIndexOutOfBoundsException();
51+
}
52+
AnyType old = theItems[index];
53+
theItems[index] = newVal;
54+
return old;
55+
}
56+
57+
public void ensureCapacity(int newCapacity) {
58+
if (newCapacity < theSize) {
59+
return;
60+
}
61+
62+
AnyType[] old = theItems;
63+
theItems = (AnyType []) new Object[newCapacity];
64+
for (int i = 0; i < size(); i++) {
65+
theItems[i] = old[i];
66+
}
67+
}
68+
69+
public boolean add(AnyType val) {
70+
add(size(), val);
71+
return true;
72+
}
73+
74+
public void add(int index, AnyType val) {
75+
if (theItems.length == size()) {
76+
ensureCapacity(size() * 2 + 1);
77+
}
78+
for (int i = theSize; i > index; i--) {
79+
theItems[i] = theItems[i - 1];
80+
}
81+
theItems[index] = val;
82+
theSize++;
83+
}
84+
85+
public AnyType remove(int index) {
86+
AnyType removedItem = theItems[index];
87+
for (int i = index; i < size() - 1; i++) {
88+
theItems[i] = theItems[i + 1];
89+
}
90+
return removedItem;
91+
}
92+
93+
public Iterator<AnyType> iterator() {
94+
return new ArrayListIterator();
95+
}
96+
97+
private class ArrayListIterator implements Iterator<AnyType> {
98+
private int current = 0;
99+
100+
public boolean hasNext() {
101+
return current < size();
102+
}
103+
104+
public AnyType next() {
105+
if (!hasNext()) {
106+
throw new NoSuchElementException();
107+
}
108+
return theItems[current++];
109+
}
110+
111+
public void remove() {
112+
MyArrayList.this.remove(--current);
113+
}
114+
}
115+
}

src/ch03/MyArrayQueue.java

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package ch03;
2+
3+
import java.lang.Iterable;
4+
import java.util.Iterator;
5+
6+
/**
7+
* Created by cookfront on 2017/3/2.
8+
*
9+
* 数组实现的队列
10+
*/
11+
public class MyArrayQueue<AnyType> implements Iterable<AnyType> {
12+
private static final int DEFAULT_CAPACITY = 10;
13+
14+
private int topOfStack;
15+
private AnyType[] theItems;
16+
private int theSize;
17+
18+
public MyArrayQueue() {
19+
doClear();
20+
}
21+
22+
private void doClear() {
23+
theSize = 0;
24+
topOfStack = -1;
25+
ensureCapacity(DEFAULT_CAPACITY);
26+
}
27+
28+
public int size() {
29+
return theSize;
30+
}
31+
32+
public boolean isEmpty() {
33+
return theSize == 0;
34+
}
35+
36+
public void push(AnyType val) {
37+
if (theItems.length == size()) {
38+
ensureCapacity(size() * 2 + 1);
39+
}
40+
theItems[++topOfStack] = val;
41+
}
42+
43+
public AnyType pop() {
44+
return theItems[--topOfStack];
45+
}
46+
47+
public void ensureCapacity(int newCapacity) {
48+
if (newCapacity < theSize) {
49+
return;
50+
}
51+
52+
AnyType[] old = theItems;
53+
theItems = (AnyType []) new Object[newCapacity];
54+
for (int i = 0; i < size(); i++) {
55+
theItems[i] = old[i];
56+
}
57+
}
58+
59+
public Iterator<AnyType> iterator() {
60+
return new ArrayQueueIterator();
61+
}
62+
63+
private class ArrayQueueIterator implements Iterator<AnyType> {
64+
65+
@Override
66+
public boolean hasNext() {
67+
return false;
68+
}
69+
70+
@Override
71+
public AnyType next() {
72+
return null;
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)