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

}
相关推荐
晚烛20 小时前
CANN 模型热更新:不停机模型切换与无缝更新实战指南
开发语言·python
谢白羽20 小时前
agent memory论文解析一:解析项目(a-mem)
开发语言·php·论文·agent·a-mem·实际项目
迷渡20 小时前
用 Rust 重写的 Bun 有 13365 个 unsafe!
开发语言·后端·rust
九皇叔叔20 小时前
高斯性能分析【第一天】单表执行计划分析
java·数据库·性能分析·执行计划·gauss
苦逼的猿宝20 小时前
基于springboot的社区团购系统设计(源码+论文)
java·毕业设计·springboot·计算机毕业设计
电魂泡哥20 小时前
RocketMQ Dledger 集群与 Raft 协议
java·rocketmq·java-rocketmq
吃好睡好便好20 小时前
在Matlab中绘制质点三维运动轨迹图
开发语言·学习·matlab·信息可视化
代码村新手20 小时前
C++-多态
开发语言·c++
行走的蜗牛20 小时前
【springai】 Model层设计与实现
java·ai编程
认真的薛薛20 小时前
Linux基础:GitOps发布流程
java·linux·运维