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;
        }
    }
    
    
    
    
  • 相关阅读:
    Gym
    HDU
    HDU
    POJ
    洛谷P3690 Link Cut Tree (动态树)
    Gym
    P4294 [WC2008]游览计划 (斯坦纳树)
    洛谷P3264 [JLOI2015]管道连接 (斯坦纳树)
    HDU
    Controller调试接口
  • 原文地址:https://www.cnblogs.com/fonxian/p/11566933.html
Copyright © 2011-2022 走看看