|
| 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 | +} |
0 commit comments