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

}
相关推荐
小叶学C++1 分钟前
【C++】类与对象(下)
java·开发语言·c++
ac-er88882 分钟前
PHP“===”的意义
开发语言·php
2401_854391085 分钟前
高效开发:SpringBoot网上租赁系统实现细节
java·spring boot·后端
Cikiss13 分钟前
微服务实战——SpringCache 整合 Redis
java·redis·后端·微服务
wxin_VXbishe14 分钟前
springboot合肥师范学院实习实训管理系统-计算机毕业设计源码31290
java·spring boot·python·spring·servlet·django·php
Cikiss15 分钟前
微服务实战——平台属性
java·数据库·后端·微服务
jk_10121 分钟前
MATLAB中decomposition函数用法
开发语言·算法·matlab
weixin_4640780721 分钟前
C#串口温度读取
开发语言·c#
无敌の星仔24 分钟前
一个月学会Java 第2天 认识类与对象
java·开发语言
OEC小胖胖28 分钟前
Spring Boot + MyBatis 项目中常用注解详解(万字长篇解读)
java·spring boot·后端·spring·mybatis·web