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

}
相关推荐
dialect_5 小时前
KMP算法
开发语言·c++·算法
captain3765 小时前
MyBatis(入门5)其他查询操作
java·数据库·spring boot·mybatis
Novproxy_Z6 小时前
商用代理避坑:如何识别商家拿机房 IP 冒充住宅 IP
java·网络·tcp/ip
总要学点什么6 小时前
第89篇:三维特效-螺旋圆效果——让螺旋臂卷起一圈圈外扩的涟漪
开发语言·javascript·ecmascript·webgl·cesium·空间分析·1024程序员节
文丑颜不良啊6 小时前
LeetCode1481~1500题目解析
java·leetcode
2601_962885726 小时前
quotes 字段全解:涨跌幅、振幅、换手率的口径与单位
java·前端·spring
前端精髓7 小时前
调用openrouter的免费API模型
java·前端·javascript
步行cgn7 小时前
Spring MVC 拦截器 Interceptor 详解:从执行流程到实战应用
java·spring·mvc
文丑颜不良啊7 小时前
LeetCode1381~1400题目解析
java·leetcode
开开心心_Every7 小时前
快速隐藏程序工具自定义快捷键一键切换
java·开发语言·前端·spring·智能手机·word·intellij-idea