zoukankan      html  css  js  c++  java
  • LeetCode 第 155 场周赛

    一、最小绝对差(LeetCode-5189)

    1.1 题目描述

    1.2 解题思路

    数组排好序,获取最小的差值即可。

    1.3 解题代码

    
    public class Solution {
    
        class Test {
            public List<Integer> list;
            public Integer num;
        }
    
        public List<List<Integer>> minimumAbsDifference(int[] arr) {
            
            int len = arr.length;
            List<Test> testlist = new ArrayList<>();
            List<List<Integer>> res = new ArrayList<>();
            //数组排好序
            Arrays.sort(arr);
            int last = arr[0];
            int minRes = Integer.MAX_VALUE;
            for (int i = 1; i < len; i++) {
    
                List<Integer> list = new ArrayList<>();
                list.add(last);
                list.add(arr[i]);
    
                Test test = new Test();
                test.list = list;
                test.num = arr[i] - last;
                //获取最小差值
                minRes = test.num < minRes ? test.num : minRes;
                testlist.add(test);
                last = arr[i];
            }
            //取出等于最小差值的数组
            for (Test test : testlist) {
                if (test.num == minRes) {
                    res.add(test.list);
                }
            }
            return res;
        }
    }
    
    
    
    
  • 相关阅读:
    .NET Framework 概述
    .Net笔试(二)
    EF CodeFirst 创建数据库
    C#中的继承
    SqlHelper 基类
    在C#中实现OOP概念
    索引器、委托和事件
    .Net笔试(一)
    HTML标签速记整理W3C
    Java函数调用总结
  • 原文地址:https://www.cnblogs.com/fonxian/p/11566933.html
Copyright © 2011-2022 走看看