华为刷题:HJ3明明随机数

java 复制代码
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        int[] arr = new int[N];
        for (int i = 0; i < N; i++) {
            int n = scan.nextInt();
            arr[i] = n;
        }
        int count = getSortArr(arr);
        int i = 0;
        while (i < count) {
            System.out.println(arr[i]);
            i++;
        }
    }

    public static int getSortArr(int[]src) {
        //插入排序
        for (int i = 1; i < src.length; i++) {
            int temp = src[i];
            int j = 0;
            for (j = i - 1; j >= 0 && temp < src[j]; j--) {
                src[j + 1] = src[j];
            }
            src[j + 1] = temp;
        }
        //双指针去重
        int slow = 0, fast = 1;
        while (slow < fast && fast < src.length) {
            if (src[slow] == src[fast]) {
                fast++;
                continue;
            }
            slow++;
            src[slow] = src[fast];
            fast++;
        }
        return slow + 1;//有效长度
    }

}
相关推荐
桦说编程11 分钟前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
躺平大鹅2 小时前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者3 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺3 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart4 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
NE_STOP5 小时前
MyBatis-mybatis入门与增删改查
java
孟陬8 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端
想用offer打牌8 小时前
一站式了解四种限流算法
java·后端·go
华仔啊9 小时前
Java 开发千万别给布尔变量加 is 前缀!很容易背锅
java