|
| 1 | +package algorithm; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.BitSet; |
| 5 | +import java.util.Random; |
| 6 | + |
| 7 | +/** |
| 8 | + * Created by pengfei on 2017/9/18. |
| 9 | + */ |
| 10 | +public class A002 { |
| 11 | + |
| 12 | + public static void main(String[] args) { |
| 13 | + |
| 14 | + int array[]=new int []{3,6,18,23, 9,8,9,3}; |
| 15 | + BitSet set = new BitSet(); |
| 16 | + int count=0; |
| 17 | + |
| 18 | + for (int i = 0; i < array.length; i++) { |
| 19 | + if (set.get(array[i]) == false) |
| 20 | + set.set(array[i]); |
| 21 | + else |
| 22 | + count++; |
| 23 | + } |
| 24 | + |
| 25 | + System.out.println("duplicate:"+count); |
| 26 | + |
| 27 | + } |
| 28 | + |
| 29 | + public static void getUniqueArray(int[] array) { |
| 30 | + int i = 0; |
| 31 | + |
| 32 | + if (array.length <= 1) return; |
| 33 | + |
| 34 | + for (int j = i + 1; j < array.length; j++) { |
| 35 | + if (array[i] != array[j]) |
| 36 | + array[++i] = array[j]; |
| 37 | + } |
| 38 | + |
| 39 | + System.out.println(i + 1); |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + public static String strMul(String s1, String s2) { |
| 44 | + int a = Integer.valueOf(s1); |
| 45 | + int b = Integer.valueOf(s2); |
| 46 | + |
| 47 | + long result = a * b * 1L; |
| 48 | + System.out.println(result); |
| 49 | + |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + public static void printInfo(String s1, String s2, String base) { |
| 54 | + |
| 55 | + if (isEmpty(s1) && isEmpty(s2)) |
| 56 | + return; |
| 57 | + |
| 58 | + if (isEmpty(s1)) { |
| 59 | + System.out.println(base + s2); |
| 60 | + return; |
| 61 | + } |
| 62 | + if (isEmpty(s2)) { |
| 63 | + System.out.println(base + s1); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + printInfo(s1.substring(1), s2, base + s1.charAt(0)); |
| 69 | + |
| 70 | + printInfo(s1, s2.substring(1), base + s2.charAt(0)); |
| 71 | + |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + private static boolean isEmpty(String s) { |
| 76 | + if (s == null || s.length() == 0) |
| 77 | + return true; |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + private static void storeCode() { |
| 82 | + String s = "lilian"; |
| 83 | + s.substring(1); |
| 84 | + |
| 85 | + boolean[] check = new boolean[256]; |
| 86 | + |
| 87 | + for (int i = 0; i < s.length(); i++) { |
| 88 | + int index = s.charAt(i); |
| 89 | + |
| 90 | + if (check[index]) { |
| 91 | + System.out.println("false"); |
| 92 | + break; |
| 93 | + } else |
| 94 | + check[index] = true; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static void yihuo() { |
| 99 | + int arr[] = {3, 3, 5, 6, 6, 7, 8, 7, 8}; |
| 100 | + |
| 101 | + int temp = arr[0]; |
| 102 | + for (int i = 1; i < arr.length; i++) |
| 103 | + temp = temp ^ arr[i]; |
| 104 | + System.out.println(temp); |
| 105 | + } |
| 106 | + |
| 107 | + public static boolean checkPac(int[] array, int k) { |
| 108 | + if (k <= 0) |
| 109 | + return false; |
| 110 | + |
| 111 | + int[] mod = new int[k]; |
| 112 | + for (int num : array) |
| 113 | + mod[num % k]++; |
| 114 | + |
| 115 | + if (mod[0] % 2 != 0) |
| 116 | + return false; |
| 117 | + |
| 118 | + if (k % 2 == 0) { |
| 119 | + if (mod[k / 2] % 2 != 0) |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + for (int i = 1; i < k / 2; i++) { |
| 124 | + if (mod[i] != mod[k - i]) |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + public static int findRandom(int[] array, int max, int min) { |
| 132 | + Arrays.sort(array); |
| 133 | + int range = max - min + 1; |
| 134 | + |
| 135 | + Random random = new Random(); |
| 136 | + int result = random.nextInt(range) + min; |
| 137 | + while (Arrays.binarySearch(array, result) >= 0) |
| 138 | + result = random.nextInt(range) + min; |
| 139 | + |
| 140 | + return result; |
| 141 | + } |
| 142 | + |
| 143 | + public int binarySearch(int[] array, int key) { |
| 144 | + Arrays.sort(array); |
| 145 | + |
| 146 | + int start = 0, end = array.length - 1; |
| 147 | + while (start <= end) { |
| 148 | + int mid = (start + end) / 2; |
| 149 | + |
| 150 | + if (array[mid] == key) |
| 151 | + return mid; |
| 152 | + else if (array[mid] > key) { |
| 153 | + end = mid - 1; |
| 154 | + } else |
| 155 | + start = mid + 1; |
| 156 | + } |
| 157 | + |
| 158 | + return -1; |
| 159 | + } |
| 160 | + |
| 161 | + public void mergeArray(int[] arrayA, int[] arrayB) { |
| 162 | + int m = arrayA.length; |
| 163 | + int n = arrayB.length; |
| 164 | + |
| 165 | + while (n > 0) { |
| 166 | + if (arrayA[m - 1] < arrayB[n - 1] || m < 0) { |
| 167 | + arrayA[m + n - 1] = arrayB[n - 1]; |
| 168 | + n--; |
| 169 | + } else { |
| 170 | + arrayA[m + n - 1] = arrayA[m - 1]; |
| 171 | + m--; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + } |
| 176 | + |
| 177 | + public static int[] findArray(int[][] matrix, int m, int n) { |
| 178 | + int left = 0, right = n - 1, up = 0, down = m - 1; |
| 179 | + int count = m * n; |
| 180 | + int result[] = new int[count]; |
| 181 | + while (count > 0) { |
| 182 | + for (int j = left; j <= right; j++) { |
| 183 | + System.out.print(matrix[up][j] + " "); |
| 184 | + if (--count == 0) break; |
| 185 | + } |
| 186 | + up++; |
| 187 | + |
| 188 | + for (int j = up; j <= down; j++) { |
| 189 | + System.out.print(matrix[j][right] + " "); |
| 190 | + if (--count == 0) break; |
| 191 | + } |
| 192 | + right--; |
| 193 | + |
| 194 | + for (int j = right; j >= left; j--) { |
| 195 | + System.out.print(matrix[down][j] + " "); |
| 196 | + if (--count == 0) break; |
| 197 | + } |
| 198 | + down--; |
| 199 | + |
| 200 | + for (int j = down; j >= up; j--) { |
| 201 | + System.out.print(matrix[j][left] + " "); |
| 202 | + if (--count == 0) break; |
| 203 | + } |
| 204 | + left++; |
| 205 | + } |
| 206 | + |
| 207 | + return null; |
| 208 | + } |
| 209 | + |
| 210 | + public static void parseInt(int num) { |
| 211 | + while (num > 0) { |
| 212 | + int mod = num % 10; |
| 213 | + System.out.println(mod); |
| 214 | + |
| 215 | + num = (num - mod) / 10; |
| 216 | + } |
| 217 | + |
| 218 | + } |
| 219 | + |
| 220 | + public static void preOrder(BiTree root) { |
| 221 | + if (root != null) { |
| 222 | + visit(root); |
| 223 | + preOrder(root.left); |
| 224 | + preOrder(root.right); |
| 225 | + } |
| 226 | + |
| 227 | + } |
| 228 | + |
| 229 | + public static void midOrder(BiTree root) { |
| 230 | + if (root != null) { |
| 231 | + midOrder(root.left); |
| 232 | + visit(root); |
| 233 | + midOrder(root.right); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + public static void afterOrder(BiTree root) { |
| 238 | + if (root != null) { |
| 239 | + afterOrder(root.left); |
| 240 | + afterOrder(root.right); |
| 241 | + visit(root); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + |
| 246 | + public static void visit(BiTree node) { |
| 247 | + if (node != null) |
| 248 | + System.out.println(node.data); |
| 249 | + } |
| 250 | + |
| 251 | + public static int getDeep(BiTree root) { |
| 252 | + if (root == null) |
| 253 | + return 0; |
| 254 | + else { |
| 255 | + int d1 = getDeep(root.left); |
| 256 | + int d2 = getDeep(root.right); |
| 257 | + int max = Math.max(d1, d2) + 1; |
| 258 | + return max; |
| 259 | + } |
| 260 | + |
| 261 | + } |
| 262 | + |
| 263 | +// public static int getPathSum(BiTree root){ |
| 264 | +// if(root.left==null&&root.right==null) |
| 265 | +// return 0; |
| 266 | +// else{ |
| 267 | +// int d1=getPathSum(root.left)+1; |
| 268 | +// int d2=getPathSum(root.right)+1; |
| 269 | +// } |
| 270 | +// |
| 271 | +// |
| 272 | +// return 0; |
| 273 | +// } |
| 274 | + |
| 275 | +} |
| 276 | + |
| 277 | +class BiTree { |
| 278 | + public int data; |
| 279 | + public BiTree left; |
| 280 | + public BiTree right; |
| 281 | + |
| 282 | + public BiTree(int data){ |
| 283 | + this.data=data; |
| 284 | + } |
| 285 | +} |
0 commit comments