forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from yanghaitao0410/master
Master
- Loading branch information
Showing
17 changed files
with
730 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
2 changes: 1 addition & 1 deletion
2
...85822/src/com/coding/basic/ArrayList.java → ...m/coding/basic/homework_01/ArrayList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.coding.basic; | ||
package com.coding.basic.homework_01; | ||
|
||
import java.util.Arrays; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
.../src/com/coding/basic/BinaryTreeNode.java → ...ing/basic/homework_01/BinaryTreeNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.coding.basic; | ||
package com.coding.basic.homework_01; | ||
|
||
public class BinaryTreeNode { | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...385822/src/com/coding/basic/Iterator.java → ...om/coding/basic/homework_01/Iterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.coding.basic; | ||
package com.coding.basic.homework_01; | ||
|
||
public interface Iterator { | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...5822/src/com/coding/basic/LinkedList.java → .../coding/basic/homework_01/LinkedList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.coding.basic; | ||
package com.coding.basic.homework_01; | ||
|
||
|
||
public class LinkedList implements List{ | ||
|
2 changes: 1 addition & 1 deletion
2
...1454385822/src/com/coding/basic/List.java → ...rc/com/coding/basic/homework_01/List.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.coding.basic; | ||
package com.coding.basic.homework_01; | ||
|
||
public interface List { | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...454385822/src/com/coding/basic/Queue.java → ...c/com/coding/basic/homework_01/Queue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.coding.basic; | ||
package com.coding.basic.homework_01; | ||
|
||
public class Queue { | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...454385822/src/com/coding/basic/Stack.java → ...c/com/coding/basic/homework_01/Stack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
218 changes: 218 additions & 0 deletions
218
group06/1454385822/src/com/coding/basic/homework_02/array/ArrayUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
package com.coding.basic.homework_02.array; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayUtil { | ||
|
||
/** | ||
* 给定一个整形数组a,对该数组的值进行置换 | ||
* 例如:a = [7, 9, 30, 3] , 置换后为[3, 30, 9, 7] | ||
* 如果 a = [7, 9, 30, 3, 4] , 置换后为[4, 3, 30, 9, 7] | ||
* @param origin | ||
*/ | ||
public static void reverseArray(int [] origin){ | ||
if(origin.length > 1){ // 数组的长度大于1置换才有意义 | ||
int tem; | ||
for(int i = 0; i < origin.length / 2; i++){ | ||
tem = origin[i]; | ||
origin[i] = origin[origin.length - 1 - i]; | ||
origin[origin.length - 1 - i] = tem; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 现在有如下的一个数组:int oldArr[] = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} | ||
* 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: | ||
* {1,3,4,5,6,6,5,4,7,6,7,5} | ||
* @param oldArray | ||
* @return | ||
*/ | ||
public static int[] removeZero(int[] oldArray){ | ||
int len = 0; | ||
for(int i : oldArray){ | ||
if(i == 0) | ||
continue; | ||
len++; | ||
} | ||
int[] result = new int[len]; | ||
len = 0; | ||
for(int i = 0; i < oldArray.length; i++){ | ||
if(oldArray[i] == 0) | ||
continue; | ||
result[len++] = oldArray[i]; | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* 给定两个已经排序好的整形数组 a1,a2,创建一个新的数组a3,使得a3包含a1和a2的所有元素并集 | ||
* 例如a1 = [3,5,7,8] , a2 = [4,5,6,7] 则a3为[3,4,5,6,7,8] | ||
* @param array1 | ||
* @param array2 | ||
* @return | ||
*/ | ||
public static int [] merge(int[] array1, int [] array2){ | ||
if(array1.length == 0) | ||
return array2; | ||
if(array2.length == 0) | ||
return array1; | ||
if(array1.length == 0 && array2.length == 0) | ||
return null; | ||
int arrLen1 = 0; | ||
int arrLen2 = 0; | ||
int arrLen3 = 0; | ||
int []result = new int[array1.length + array2.length]; | ||
while(arrLen1 < array1.length || arrLen2 < array2.length){ | ||
if(arrLen1 >= array1.length){ //数组1已经没了 | ||
result[arrLen3++] = array2[arrLen2++]; | ||
continue; | ||
} | ||
if(arrLen2 >= array2.length){ //数组2已经没了 | ||
result[arrLen3++] = array1[arrLen1++]; | ||
continue; | ||
} | ||
if(array1[arrLen1] > array2[arrLen2]){ | ||
result[arrLen3++] = array2[arrLen2++]; | ||
}else if(array1[arrLen1] < array2[arrLen2]){ | ||
result[arrLen3++] = array1[arrLen1++]; | ||
}else{ | ||
result[arrLen3++] = array1[arrLen1++]; | ||
arrLen2++; | ||
} | ||
} | ||
result = Arrays.copyOf(result, arrLen3); | ||
return result; | ||
} | ||
|
||
/** | ||
* 把一个已经存满数据的数组oldArray的容量进行扩容,扩容后的新数据大小为oldArray.length * 2 | ||
* 注意,老数组的元素在新数组中需要保持 | ||
* 例如oldArray = {2,3,6}, size = 3,则返回的新数组为{2,3,6,0,0,0} | ||
* @param oldArray | ||
* @param size | ||
* @return | ||
*/ | ||
public static int[] grow(int[] oldArray, int size){ | ||
int[] result = new int [size * 2]; | ||
for(int i = 0; i < oldArray.length; i++){ | ||
result[i] = oldArray[i]; | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* 斐波那契数列为:1, 1, 2, 3, 5, 8, 13, 21...... ,给定一个最大值,返回小于该值的数列 | ||
* 例如:max = 15, 则返回的数组应该为{1, 1, 2, 3, 5, 8, 13} | ||
* max = 1, 则返回空数组{} | ||
* @param max | ||
* @return | ||
*/ | ||
public static int[] fibonacci(int max){ | ||
if(max == 1) | ||
return new int[]{}; | ||
int[] result = new int[10]; | ||
int reLen = 2; | ||
result[0] = result[1] = 1; | ||
while(result[reLen - 1] + result[reLen -2] < max){ | ||
if(reLen >= result.length) | ||
result = grow(result, result.length); | ||
result[reLen] = result[reLen - 1] + result[reLen -2]; | ||
reLen++; | ||
} | ||
result = Arrays.copyOf(result, reLen); | ||
|
||
return result; | ||
} | ||
|
||
|
||
/** | ||
* 返回小于给定最大值max的所有素数数组 | ||
* 例如max = 23,返回的数组为{2,3,5,7,11,13,17,19} | ||
* @param max | ||
* @return | ||
*/ | ||
public static int[] getPrimes(int max){ | ||
if(max <= 2){ | ||
return null; | ||
} | ||
int[] result = new int[10]; | ||
int len = 0; | ||
boolean flag = true; | ||
result[0] = 2; | ||
if(max == 3) | ||
return result; | ||
len++; | ||
for(int i = 3; i < max; i++){ | ||
flag = true; | ||
if(len >= result.length) | ||
result = grow(result, result.length); | ||
for(int j = 2; j < i; j++){ | ||
if(i % j == 0){ | ||
flag = false; | ||
} | ||
} | ||
if(flag == true){ | ||
result[len++] = i; | ||
} | ||
|
||
} | ||
result = Arrays.copyOf(result, len); | ||
return result; | ||
} | ||
|
||
/** | ||
* 所谓"完数",是指这个数恰好等于他的因子之和,例如6 = 1+2+3 | ||
* 给定一个最大值max,返回一个数组,数组中是小于max的所有完数 | ||
* @param max | ||
* @return | ||
*/ | ||
public static int[] getPerfectNumbers(int max){ | ||
int sum = 1; | ||
int [] result = new int[10]; | ||
int len = 0; | ||
for(int i = 2;i < max; i++){ | ||
sum = 1; | ||
if(len >= result.length) | ||
result = grow(result, result.length); | ||
for(int j = 2; j < i; j++){ | ||
if(i % j == 0) | ||
sum += j; | ||
} | ||
if(sum == i) | ||
result[len++] = sum; | ||
} | ||
result = Arrays.copyOf(result, len); | ||
return result; | ||
} | ||
|
||
|
||
/** | ||
* 用seperator 把数组array给连接起来 | ||
* 例如array = [3,8,9] ,seperator = "-" | ||
* 则返回值为"3-8-9" | ||
* @param array | ||
* @param seperator | ||
* @return | ||
*/ | ||
public static String join(int[]array, String seperator){ | ||
String result = new String(); | ||
int len = 0; | ||
result = new Integer(array[0]).toString(); | ||
for(int i = 1; i < array.length; i++){ | ||
result += seperator; | ||
result += array[i]; | ||
} | ||
return result; | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
93 changes: 93 additions & 0 deletions
93
group06/1454385822/src/com/coding/basic/homework_02/array/ArrayUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.coding.basic.homework_02.array; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
|
||
public class ArrayUtilTest { | ||
int[] array; | ||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testReverseArray() { | ||
array = new int[]{7, 9, 30, 3}; | ||
// array = new int[]{7, 9, 30, 3, 4}; | ||
ArrayUtil.reverseArray(array); | ||
Assert.assertArrayEquals(new int[]{3, 30, 9, 7}, array); | ||
// Assert.assertArrayEquals(new int[]{4, 3, 30, 9, 7}, array); | ||
} | ||
|
||
@Test | ||
public void testRemoveZero(){ | ||
array = new int[]{1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; | ||
Assert.assertArrayEquals(new int[]{1,3,4,5,6,6,5,4,7,6,7,5}, ArrayUtil.removeZero(array)); | ||
} | ||
|
||
@Test | ||
public void testMerge(){ | ||
array = new int[]{3,5,7,8}; | ||
int [] array2 = new int[]{4,5,6,7}; | ||
Assert.assertArrayEquals(new int[]{3,4,5,6,7,8}, ArrayUtil.merge(array, array2)); | ||
} | ||
|
||
@Test | ||
public void testGrow(){ | ||
array = new int[]{2, 3, 6}; | ||
Assert.assertArrayEquals(new int[]{2, 3, 6, 0, 0, 0} ,ArrayUtil.grow(array, 3)); | ||
} | ||
|
||
@Test | ||
public void testFibonacci(){ | ||
Assert.assertArrayEquals(new int[]{1, 1, 2, 3, 5, 8, 13}, ArrayUtil.fibonacci(15)); | ||
// array = ArrayUtil.fibonacci(15); | ||
// for(int i: array) | ||
// System.out.println(i); | ||
} | ||
|
||
|
||
@Test | ||
public void testPrimes(){ | ||
Assert.assertArrayEquals(new int[]{2,3,5,7,11,13,17,19}, ArrayUtil.getPrimes(23)); | ||
// array = ArrayUtil.getPrimes(23); | ||
// for(int i : array){ | ||
// System.out.print(i + " "); | ||
// } | ||
|
||
} | ||
|
||
@Test | ||
public void testPerfectNumbers(){ | ||
// Assert.assertArrayEquals(new int[]{6}, ArrayUtil.getPerfectNumbers(7)); | ||
array = ArrayUtil.getPerfectNumbers(100); | ||
for(int i : array){ | ||
System.out.print(i + " "); | ||
} | ||
} | ||
|
||
@Test | ||
public void testJoin(){ | ||
Assert.assertEquals("3-8-9", ArrayUtil.join(new int[]{3,8,9}, "-")); | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.