package com.coding2017.array;
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
* @return
*/
public void reverseArray(int[] origin) {
if (nullOrEmpty(origin) || origin.length == 1) {
return;
}
int length = origin.length;
for (int i = 0; i < length / 2; i++) {
swap(origin, i, length - 1 - i);
}
}
private void swap(int[] array, int index1, int index2) {
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
/**
* 现在有如下的一个数组: 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 int[] removeZero(int[] oldArray) {
if (nullOrEmpty(oldArray)) {
return new int[0];
}
int[] tempArray = new int[oldArray.length];
int newLength = 0;
for (int e : oldArray) {
if (e != 0) {
tempArray[newLength++] = e;
}
}
int[] newArray = new int[newLength];
System.arraycopy(tempArray, 0, newArray, 0, newLength);
return newArray;
}
/**
* 给定两个已经排序好的整形数组, 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 int[] merge(int[] array1, int[] array2) {
if (nullOrEmpty(array1)) {
return array2;
}
if (nullOrEmpty(array2)) {
return array1;
}
int index1 = 0;
int index2 = 0;
int length1 = array1.length;
int length2 = array2.length;
int[] tempArray = new int[length1 + length2];
int pos = 0;
while (index1 < length1 && index2 < length2) {
if (array1[index1] == array2[index2]) {
// 元素相同情况下, 忽略其中一个
index1++;
} else if (array1[index1] < array2[index2]) {
tempArray[pos++] = array1[index1++];
} else {
tempArray[pos++] = array2[index2++];
}
}
while (index1 < length1) {
tempArray[pos++] = array1[index1++];
}
while (index2 < length2) {
tempArray[pos++] = array2[index2++];
}
if (pos == tempArray.length) {
return tempArray;
}
int[] result = new int[pos];
System.arraycopy(tempArray, 0, result, 0, pos);
return result;
}
private boolean nullOrEmpty(int[] array) {
return array == null || array.length == 0;
}
/**
* 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size =
* 3,则返回的新数组为 [2,3,6,0,0,0]
*
* @param oldArray
* @param size
* @return
*/
public int[] grow(int[] oldArray, int size) {
assert oldArray != null;
assert size >= 0;
int[] result = new int[oldArray.length + size];
System.arraycopy(oldArray, 0, result, 0, oldArray.length);
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 int[] fibonacci(int max) {
if (max <= 1) {
return new int[0];
}
int f1 = 1;
int f2 = 1;
// 超过max的那个pos, 也就是最终长度
int maxPos = 2;
while (true) {
int curData = f1 + f2;
if (curData > max) {
break;
}
f1 = f2;
f2 = curData;
maxPos++;
}
int[] result = new int[maxPos];
f1 = 1;
f2 = 1;
for (int i = 0; i < maxPos; i++) {
if (i == 0 || i == 1) {
result[i] = 1;
} else {
result[i] = f1 + f2;
f1 = f2;
f2 = result[i];
}
}
return result;
}
/**
* 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19]
*
* @param max
* @return
*/
public int[] getPrimes(int max) {
if (max <= 2) {
return new int[0];
}
boolean[] data = new boolean[max];
// 1. 把2放置为true
data[2] = true;
// 2. 把奇数首先初始化为true
for (int i = 3; i < max; i += 2) {
data[i] = true;
}
// 3. 从3开始到max/2检查素数, 然后把这个数的倍数全都置为false
for (int i = 3; i < max / 2; i += 2) {
data[i] = isPrime(i);
for (int j = 3; i * j < max; j += 2) {
data[i * j] = false;
}
}
// 4. 查一共多少个
int primeCount = 0;
for (int i = 0; i < max; i++) {
if (data[i]) {
primeCount++;
}
}
// 5. 构造结果
int[] result = new int[primeCount];
int pos = 0;
for (int i = 0; i < max; i++) {
if (data[i]) {
result[pos++] = i;
}
}
return result;
}
private boolean isPrime(int n) {
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
/**
* 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数
*
* @param max
* @return
*/
public int[] getPerfectNumbers(int max) {
boolean[] data = new boolean[max];
int count = 0;
for (int i = 0; i < max; i++) {
if (isPerfectNumber(i)) {
data[i] = true;
count++;
}
}
int[] result = new int[count];
int pos = 0;
for (int i = 0; i < max; i++) {
if (data[i]) {
result[pos++] = i;
}
}
return result;
}
private boolean isPerfectNumber(int n) {
if (n <= 0) {
return false;
}
int temp = 0;
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0) {
temp += i;
}
}
return temp == n;
}
/**
* 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9"
*
* @param array
* @param seperator
* @return
*/
public String join(int[] array, String seperator) {
if (nullOrEmpty(array)) {
return "";
}
StringBuilder builder = new StringBuilder(String.valueOf(array[0]));
for (int i = 1; i < array.length; i++) {
builder.append(seperator).append(array[i]);
}
return builder.toString();
}
}