题解 | #明明的随机数#

明明的随机数

https://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
        while (in.hasNext()){
            int num = in.nextInt();
            int[] arr = new int[num];
            for (int i = 0; i < num; i++) {
                arr[i] = in.nextInt();
            }
            quickSort(arr,0,arr.length-1);
            StringBuilder sb = new StringBuilder();
            for (int i : arr) {
                if (sb.length() >0 && sb.toString().contains(i+",")){
                    continue;
                }
                sb.append(i).append(",");
                System.out.println(i);
            }
        }

    }
    private static void quickSort(int[] arr, int start, int end) {
        int left = start;
        int right = end;
        int pivot = arr[start];
        while (left < right) {
            while (left < right && arr[right] > pivot) {
                right --;
            }
            while (left < right && arr[left] < pivot) {
                left ++ ;
            }
            if (left < right && arr[left] == arr[right]) {
                left ++;
            } else {
                int temp = arr[left];
                arr[left] = arr[right];
                arr[right] = temp;
            }
        }
        if (left - 1 > start) {
            quickSort(arr, start, left - 1);
        }
        if (right + 1 < end) {
            quickSort(arr, right + 1, end);
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务