题解 | #明明的随机数#
明明的随机数
http://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0
使用map put值覆盖,得到一个不重复的数组
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()){ int total = sc.nextInt(); int arr[] = new int[total]; for (int i = 0; i <total ; i++) { arr[i] = sc.nextInt(); } int[] ints = replaceExixt(arr); int[] sort = quickSort(ints, 0, ints.length - 1); for (Integer value:sort){ System.out.println(value); } } } public static int[] replaceExixt(int arr[]){ Map<Integer,Integer> map = new HashMap(); for (int i = 0; i < arr.length; i++) { map.put(arr[i],arr[i]); } int temp[] = new int[map.size()]; int i =0; for (Integer key:map.keySet()) { temp[i++] = key; } return temp; } public static int[] quickSort(int arr[],int start,int end){ if (start<end){ int low =start; int stard = arr[start]; int high = end; while (low<high){ while (low<high&&stard<=arr[high]){ high--; } arr[low] = arr[high]; while (low<high&&arr[low]<=stard){ low++; } arr[high] = arr[low]; } arr[low] = stard; quickSort(arr,start,low); quickSort(arr,low+1,end); } return arr; }
}