Skip to content

Commit 20d4c93

Browse files
committed
week2 homework arrayUtil
1 parent 2d61c08 commit 20d4c93

File tree

6 files changed

+337
-0
lines changed

6 files changed

+337
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Coderising</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
package com.Array;
2+
3+
import java.util.Arrays;
4+
5+
public class ArrayUtil {
6+
7+
/**
8+
* 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a =
9+
* [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7]
10+
*
11+
* @param origin
12+
* @return
13+
*/
14+
public void reverseArray(int[] origin) {
15+
if (origin == null)
16+
return;
17+
int head = 0;
18+
int tail = origin.length - 1;
19+
int tmp;
20+
while (head < tail) {
21+
tmp = origin[tail];
22+
origin[tail] = origin[head];
23+
origin[head] = tmp;
24+
head++;
25+
tail--;
26+
}
27+
}
28+
29+
/**
30+
* 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}
31+
* 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5}
32+
*
33+
* @param oldArray
34+
* @return
35+
*/
36+
37+
public int[] removeZero(int[] oldArray) {
38+
int nonZeroNum = 0;
39+
for (int i = 0; i < oldArray.length; i++) {
40+
if (oldArray[i] != 0) {
41+
nonZeroNum++;
42+
}
43+
}
44+
int cnt = 0;
45+
int[] newArray = new int[nonZeroNum];
46+
for (int i = 0; i < oldArray.length; i++) {
47+
if (oldArray[i] != 0) {
48+
newArray[cnt++] = oldArray[i];
49+
}
50+
}
51+
return newArray;
52+
}
53+
54+
/**
55+
* 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的
56+
* 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复
57+
* @param array1
58+
* @param array2
59+
* @return
60+
*/
61+
62+
public int[] merge(int[] array1, int[] array2){
63+
int[] resultArray = new int[array1.length + array2.length];
64+
int cnt1 = 0;
65+
int cnt2 = 0;
66+
int cntResult = 0;
67+
resultArray[cntResult++] = array1[0]<array2[0]?array1[0]:array2[0];
68+
//将array1[0]和array2[0]中最小值放入resultArray,不用考虑时候已经存在
69+
//避免出现第一个值为0而与resultArray[0]的初始值相同的情况
70+
while(cnt1 < array1.length && cnt2 < array2.length){
71+
if(array1[cnt1] > array2[cnt2]){
72+
if(resultArray[cntResult-1] != array2[cnt2]){
73+
//array2[cnt2]中的数小,且没有重复
74+
//加入resultArray中
75+
resultArray[cntResult++] = array2[cnt2++];
76+
}else{
77+
//array2[cnt2]中的数小,但是重复
78+
cnt2++;
79+
}
80+
}else{
81+
if(resultArray[cntResult-1] != array1[cnt1]){
82+
//array1[cnt1]中的数小,且没有重复
83+
//加入resultArray中
84+
resultArray[cntResult++] = array1[cnt1++];
85+
}else{
86+
//array1[cnt1]中的数小,但是重复
87+
cnt1++;
88+
}
89+
}
90+
}
91+
92+
//将为遍历完的数组的剩余部分放入resultArray
93+
if(cnt1 == array1.length){
94+
//array2有剩余
95+
while(cnt2 < array2.length){
96+
//将array2中剩余的数,无重复地加入resultArray中
97+
if(resultArray[cntResult-1] != array2[cnt2]){
98+
resultArray[cntResult++] = array2[cnt2++];
99+
}else{
100+
cnt2++;
101+
}
102+
}
103+
}else{
104+
while(cnt1 < array1.length){
105+
//将array1中剩余的数,无重复地加入resultArray中
106+
if(resultArray[cntResult-1] != array1[cnt1]){
107+
resultArray[cntResult++] = array1[cnt1++];
108+
}else{
109+
cnt1++;
110+
}
111+
}
112+
}
113+
return Arrays.copyOf(resultArray, cntResult);
114+
}
115+
116+
/**
117+
* 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size
118+
* 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为
119+
* [2,3,6,0,0,0]
120+
*
121+
* @param oldArray
122+
* @param size
123+
* @return
124+
*/
125+
public int[] grow(int[] oldArray, int size) {
126+
int[] newArray = new int[oldArray.length + size];
127+
for(int i=0; i<oldArray.length; i++){
128+
newArray[i] = oldArray[i];
129+
}
130+
return newArray;
131+
}
132+
133+
/**
134+
* 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 ,
135+
* 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 []
136+
*
137+
* @param max
138+
* @return
139+
*/
140+
public int[] fibonacci(int max) {
141+
if(max <= 1) return null;
142+
int[] result = new int[1000];
143+
result[0] = 1;
144+
result[1] = 1;
145+
int index = 2;//index of result[]
146+
int tmpResult = 2;//current result
147+
while(tmpResult < max){
148+
result[index++] = tmpResult;
149+
tmpResult = result[index-2] + result[index-1];
150+
}
151+
return Arrays.copyOf(result, index);
152+
}
153+
154+
/**
155+
* 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19]
156+
*
157+
* @param max
158+
* @return
159+
*/
160+
public int[] getPrimes(int max) {
161+
if(max <= 1) return null;
162+
int[] result = new int[max];
163+
int index = 0;
164+
for(int currentNum=2; currentNum<max; currentNum++){
165+
boolean isPrime = true;
166+
for(int j = 2; j<currentNum; j++){
167+
if(currentNum%j == 0){
168+
//i is not prime
169+
isPrime = false;
170+
break;
171+
}
172+
}
173+
if(isPrime){
174+
result[index++] = currentNum;
175+
}
176+
}
177+
return Arrays.copyOf(result, index);
178+
}
179+
180+
/**
181+
* 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数
182+
*
183+
* @param max
184+
* @return
185+
*/
186+
public int[] getPerfectNumbers(int max) {
187+
if(max < 1) return null;
188+
int[] result = new int[max];
189+
int index = 0;
190+
for(int currentNum=1; currentNum<max; currentNum++){
191+
int sum = 0;
192+
for(int i=1; i<currentNum; i++){
193+
//get sum of factor for each number
194+
if(isFactor(i, currentNum)){
195+
sum +=i;
196+
}
197+
}
198+
if(sum == currentNum){
199+
result[index++] = currentNum;
200+
}
201+
}
202+
return Arrays.copyOf(result, index);
203+
}
204+
205+
public boolean isFactor(int a, int num){
206+
return num%a == 0;
207+
}
208+
209+
/**
210+
* 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9"
211+
*
212+
* @param array
213+
* @param s
214+
* @return
215+
*/
216+
public String join(int[] array, String seperator) {
217+
if(array == null || seperator == null) return null;
218+
StringBuilder sb = new StringBuilder();
219+
sb.append(Integer.toString(array[0]));
220+
for(int i=1; i<array.length; i++){
221+
sb.append(seperator);
222+
sb.append(Integer.toString(array[i]));
223+
}
224+
return sb.toString();
225+
}
226+
227+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.Array;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
public class ArrayUtilTest {
10+
ArrayUtil au;
11+
12+
@Before
13+
public void setUp() throws Exception {
14+
au = new ArrayUtil();
15+
}
16+
17+
@After
18+
public void tearDown() throws Exception {
19+
}
20+
21+
@Test
22+
public void testReverseArray() {
23+
int[] origin1 = { 1, 2, 3, 4, 5, 6, 7, 8 };
24+
int[] origin2 = { 1, 2, 3, 4, 5 };
25+
au.reverseArray(origin1);
26+
au.reverseArray(origin2);
27+
assertArrayEquals(new int[] { 8, 7, 6, 5, 4, 3, 2, 1 }, origin1);
28+
assertArrayEquals(new int[] { 5, 4, 3, 2, 1 }, origin2);
29+
}
30+
31+
@Test
32+
public void testRemoveZero() {
33+
int[] oldArray = { 1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 };
34+
assertArrayEquals(new int[] { 1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5 }, au.removeZero(oldArray));
35+
}
36+
37+
@Test
38+
public void testMerge() {
39+
int[] array1 = {0, 5, 7, 8};
40+
int[] array2 = {4, 5, 8, 9};
41+
assertArrayEquals(new int[]{0,4,5,7,8, 9}, au.merge(array1, array2));
42+
43+
}
44+
45+
@Test
46+
public void testGrow() {
47+
int[] oldArray = {2,3,6};
48+
assertArrayEquals(new int[]{2,3,6,0,0,0}, au.grow(oldArray, 3));
49+
}
50+
51+
@Test
52+
public void testFibonacci() {
53+
assertArrayEquals(new int[]{1,1,2,3,5,8,13}, au.fibonacci(15));
54+
assertEquals(null, au.fibonacci(1));
55+
}
56+
57+
@Test
58+
public void testGetPrimes() {
59+
assertArrayEquals(new int[]{2,3,5,7,11,13,17,19}, au.getPrimes(23));
60+
assertEquals(null, au.getPrimes(1));
61+
}
62+
63+
@Test
64+
public void testGetPerfectNumbers() {
65+
assertArrayEquals(new int[]{6, 28, 496}, au.getPerfectNumbers(1000));
66+
}
67+
68+
@Test
69+
public void testJoin() {
70+
assertEquals("3-4-5", au.join(new int[]{3,4,5}, "-"));
71+
assertEquals("3|4|5", au.join(new int[]{3,4,5}, "|"));
72+
}
73+
74+
}

0 commit comments

Comments
 (0)