华为刷题: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;//有效长度
    }

}
相关推荐
FrankYoou15 分钟前
Jenkins 与 GitLab CI/CD 的核心对比
java·docker
麦兜*41 分钟前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
Coding小公仔1 小时前
C++ bitset 模板类
开发语言·c++
KK溜了溜了1 小时前
JAVA-springboot 整合Redis
java·spring boot·redis
小赖同学啊1 小时前
物联网数据安全区块链服务
开发语言·python·区块链
天河归来1 小时前
使用idea创建springboot单体项目
java·spring boot·intellij-idea
shimly1234561 小时前
bash 脚本比较 100 个程序运行时间,精确到毫秒,脚本
开发语言·chrome·bash
weixin_478689761 小时前
十大排序算法汇总
java·算法·排序算法
码荼1 小时前
学习开发之hashmap
java·python·学习·哈希算法·个人开发·小白学开发·不花钱不花时间crud