Skip to content

Commit a537538

Browse files
authored
Merge pull request onlyliuxin#7 from yangdd1205/master
交作业
2 parents c399cd9 + 9c08fc8 commit a537538

File tree

16 files changed

+1301
-0
lines changed

16 files changed

+1301
-0
lines changed

group18/1049843090/.classpath

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
4+
<classpathentry kind="src" path="src"/>
5+
<classpathentry kind="src" path="test"/>
6+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/.jar"/>
7+
<classpathentry kind="lib" path="lib/junit-4.12.jar"/>
8+
<classpathentry kind="lib" path="lib/hamcrest-core-1.3.jar"/>
9+
<classpathentry kind="output" path="out/production/1049843090"/>
10+
</classpath>

group18/1049843090/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.idea
2+
*.iml
3+
*.eml
4+
.settings/
5+
target/
6+
build/
7+
out/

group18/1049843090/.project

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>1049843090</name>
4+
<comment/>
5+
<projects/>
6+
<buildSpec>
7+
<buildCommand>
8+
<name>org.eclipse.jdt.core.javabuilder</name>
9+
<arguments/>
10+
</buildCommand>
11+
</buildSpec>
12+
<natures>
13+
<nature>org.eclipse.jdt.core.javanature</nature>
14+
</natures>
15+
</projectDescription>
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package com.coderising.array;
2+
3+
4+
import com.coding.basic.Queue;
5+
import com.coding.basic.Stack;
6+
7+
public class ArrayUtil {
8+
9+
/**
10+
* 给定一个整形数组a , 对该数组的值进行置换
11+
* 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7]
12+
* 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7]
13+
*
14+
* @param origin
15+
* @return
16+
*/
17+
public void reverseArray(int[] origin) {
18+
if (isEmptyOrNull(origin)) {
19+
return;
20+
}
21+
//solution 1 move element
22+
//for (int i = 0; i <= origin.length >> 2; i++) {
23+
// int temp = origin[i];
24+
// origin[i] = origin[origin.length - 1 - i];
25+
// origin[origin.length - 1 - i] = temp;
26+
//}
27+
28+
//solution 2 use Stack
29+
Stack<Integer> stack = new Stack<>();
30+
for (int i : origin) {
31+
stack.push(i);
32+
}
33+
for (int i = 0; i < origin.length; i++) {
34+
origin[i]=stack.pop();
35+
}
36+
}
37+
38+
/**
39+
* 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}
40+
* 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为:
41+
* {1,3,4,5,6,6,5,4,7,6,7,5}
42+
*
43+
* @param oldArray
44+
* @return
45+
*/
46+
47+
public int[] removeZero(int[] oldArray) {
48+
if (isEmptyOrNull(oldArray)) {
49+
return null;
50+
}
51+
//solution 1 use Queue OR Stack
52+
//Queue<Integer> queue = new Queue<>();
53+
//for (int i : oldArray) {
54+
// if (i != 0) {
55+
// queue.enQueue(i);
56+
// }
57+
//}
58+
//int[] newArray = new int[queue.size()];
59+
//for (int i = 0; i < newArray.length; i++) {
60+
// newArray[i] = queue.deQueue();
61+
//}
62+
//return newArray;
63+
64+
65+
//solution 2 use Array
66+
int[] tempArray = new int[oldArray.length];
67+
int index = 0;
68+
for (int i = 0; i < oldArray.length; i++) {
69+
if (oldArray[i] != 0) {
70+
tempArray[index++] = oldArray[i];
71+
}
72+
}
73+
int[] newArray = new int[index];
74+
System.arraycopy(tempArray,0,newArray,0,index);
75+
return newArray;
76+
}
77+
78+
/**
79+
* 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的
80+
* 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复
81+
*
82+
* @param array1
83+
* @param array2
84+
* @return
85+
*/
86+
87+
public int[] merge(int[] array1, int[] array2) {
88+
89+
return null;
90+
}
91+
92+
/**
93+
* 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size
94+
* 注意,老数组的元素在新数组中需要保持
95+
* 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为
96+
* [2,3,6,0,0,0]
97+
*
98+
* @param oldArray
99+
* @param size
100+
* @return
101+
*/
102+
public int[] grow(int[] oldArray, int size) {
103+
if (isEmptyOrNull(oldArray)) {
104+
return null;
105+
}
106+
int[] newArray = new int[oldArray.length + size];
107+
//solution 1 use System.arraycopy
108+
//System.arraycopy(oldArray,0,newArray,0, oldArray.length);
109+
110+
//solution 2 use loop
111+
for (int i = 0; i < oldArray.length; i++) {
112+
newArray[i] = oldArray[i];
113+
}
114+
return newArray;
115+
}
116+
117+
/**
118+
* 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列
119+
* 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13]
120+
* max = 1, 则返回空数组 []
121+
*
122+
* @param max
123+
* @return
124+
*/
125+
public int[] fibonacci(int max) {
126+
return null;
127+
}
128+
129+
/**
130+
* 返回小于给定最大值max的所有素数数组
131+
* 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19]
132+
*
133+
* @param max
134+
* @return
135+
*/
136+
public int[] getPrimes(int max) {
137+
return null;
138+
}
139+
140+
/**
141+
* 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3
142+
* 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数
143+
*
144+
* @param max
145+
* @return
146+
*/
147+
public int[] getPerfectNumbers(int max) {
148+
return null;
149+
}
150+
151+
/**
152+
* 用seperator 把数组 array给连接起来
153+
* 例如array= [3,8,9], seperator = "-"
154+
* 则返回值为"3-8-9"
155+
*
156+
* @param array
157+
* @param seperator 分隔符
158+
* @return
159+
*/
160+
public String join(int[] array, String seperator) {
161+
if (isEmptyOrNull(array)) {
162+
return "";
163+
}
164+
if (array.length < 2) {
165+
return array[0] + "";
166+
}
167+
StringBuffer stringBuffer = new StringBuffer();
168+
for (int i = 0; i < array.length - 1; i++) {
169+
stringBuffer.append(array[i] + seperator);
170+
}
171+
stringBuffer.append(array[array.length - 1]);
172+
return stringBuffer.toString();
173+
}
174+
175+
/**
176+
* 检查数组是否为空或者为null
177+
*
178+
* @param array
179+
* @return
180+
*/
181+
private boolean isEmptyOrNull(int[] array) {
182+
if (array == null || array.length == 0) {
183+
return true;
184+
}
185+
return false;
186+
}
187+
188+
public static void main(String[] args) {
189+
int[] a = {7,9,5};
190+
ArrayUtil arrayUtil = new ArrayUtil();
191+
arrayUtil.reverseArray(a);
192+
for (int i : a) {
193+
System.out.println(i);
194+
}
195+
}
196+
197+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package com.coding.basic;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* A Simple ArrayList
7+
*/
8+
public class ArrayList<E> implements List<E> {
9+
10+
/**
11+
* 当前list的元素个数
12+
*/
13+
private int size;
14+
15+
/**
16+
* 默认数组大小
17+
*/
18+
private static final int DEFAULT_CAPACITY = 10;
19+
20+
/**
21+
* 存储元素的数组
22+
*/
23+
private Object[] elementData;
24+
25+
/**
26+
* 追加一个元素
27+
*
28+
* @param e
29+
*/
30+
public void add(E e) {
31+
grow();
32+
elementData[size++] = e;
33+
}
34+
35+
private void grow() {
36+
if (elementData.length == size) {
37+
elementData = Arrays.copyOf(elementData, size + 10);
38+
}
39+
}
40+
41+
/**
42+
* 插入一个元素到指定位置
43+
*
44+
* @param index
45+
* @param e
46+
*/
47+
public void add(int index, E e) {
48+
if (index < 0 || index > size) {
49+
throw new IndexOutOfBoundsException("下标越界");
50+
}
51+
grow();
52+
int movedNum = size - index;
53+
if (movedNum > 0) {
54+
System.arraycopy(elementData, index, elementData, index + 1, movedNum);
55+
}
56+
elementData[index] = e;
57+
size++;
58+
}
59+
60+
/**
61+
* 获取指定位置的元素
62+
*
63+
* @param index
64+
* @return
65+
*/
66+
public E get(int index) {
67+
checkIndex(index);
68+
return getElement(index);
69+
}
70+
71+
72+
/**
73+
* 删除指定位置的元素
74+
*
75+
* @param index
76+
* @return
77+
*/
78+
public E remove(int index) {
79+
checkIndex(index);
80+
E delEle = getElement(index);
81+
int movedNum = size - index - 1;//是不是最后一个元素
82+
if (movedNum > 0) {
83+
System.arraycopy(elementData, index + 1, elementData, index, movedNum);
84+
}
85+
elementData[--size] = null;
86+
return delEle;
87+
}
88+
89+
private void checkIndex(int index) {
90+
if (index < 0 || index >= size) {
91+
throw new IndexOutOfBoundsException("下标越界");
92+
}
93+
}
94+
95+
private E getElement(int index) {
96+
return (E) elementData[index];
97+
}
98+
99+
/**
100+
* list中元素的个数
101+
*
102+
* @return
103+
*/
104+
public int size() {
105+
return this.size;
106+
}
107+
108+
109+
public ArrayList() {
110+
this.elementData = new Object[DEFAULT_CAPACITY];
111+
}
112+
113+
public Iterator<E> iterator() {
114+
return new ArrayListIterator();
115+
}
116+
117+
private class ArrayListIterator<E> implements Iterator<E> {
118+
119+
private int cursor;//游标
120+
121+
private int lastRet = -1;//可被删除元素下标
122+
123+
124+
@Override
125+
public boolean hasNext() {
126+
return cursor != size;
127+
}
128+
129+
@Override
130+
public E next() {
131+
int i = cursor;
132+
cursor++;
133+
return (E) elementData[lastRet = i];
134+
}
135+
136+
@Override
137+
public void remove() {
138+
if (lastRet < 0) {
139+
throw new IllegalStateException();
140+
}
141+
cursor = lastRet;//游标等于当前删除元素的下标 或者 cursor--;
142+
ArrayList.this.remove(lastRet);
143+
lastRet = -1;//重置可删元素下标
144+
145+
}
146+
}
147+
148+
}

0 commit comments

Comments
 (0)