如何使用哈希图计算数字对的绝对差?

我希望将整数数组转换为键值对,其中每个元素都是前一个元素的值和后一个元素的键(第一个和最后一个元素除外)。


将数组转换为哈希图后,我需要计算数组中整数之间的差距。我能够转换数组,但正在寻找更优雅的解决方案,并且仍然需要找到间隙。


public void calculateGap(int[] nums) {

    int l = nums.length;

    Map<Integer, Integer> dist = new HashMap<>();

        

    for(int i=0; i<l; i++) {

        if(i != l-1) {

            int val = nums[i+1];

            dist.put(nums[i], val);    

        }

        if(i != 0) {

            int key = nums[i-1];

            dist.put(key, nums[i]);

        }

    }

}

输入:[3,6,9,1]


作为哈希图输入:{3:6, 6:9, 9:1}


输出:[3,3,8]


达令说
浏览 53回答 4
4回答

小唯快跑啊

问:我想知道哈希图绕行的目的是什么,当人们可以直接计算距离时。public int[] calculateGap(int[] nums) {&nbsp; &nbsp; if (nums.length < 2) {&nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException();&nbsp; &nbsp; }&nbsp; &nbsp; int[] dist = new int[nums.length - 1];&nbsp; &nbsp; for (int i = 1; i < nums.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; dist[i - 1] = Math.abs(nums[i] - nums[i - 1]);&nbsp; &nbsp; }&nbsp; &nbsp; return dist;}

ABOUTYOU

问:为什么不这样做:public static Map<Integer, Integer> calculateGap(int[] nums) {&nbsp; &nbsp; Map<Integer, Integer> result = new HashMap<>();&nbsp; &nbsp; for(int i=0; i<nums.length - 1; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.put(nums[i], nums[i+1);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; return result;}&nbsp; &nbsp;&nbsp;...public static int gap ( Map<Integer, Integer> map, int idx) throws Exception {&nbsp; if (idx < 0 || idx >= map.size()) {&nbsp; &nbsp; &nbsp;throw new Exception ("index "+ idx " exceeds list bounds 0 .." + map.size() - 1);&nbsp; }&nbsp; return Math.abs(idx - map.get(idx);}

海绵宝宝撒

没有必要使用哈希图。您可以完全跳过它,只需将计算出的间隙存储在 int[] 或 ArrayList 中://using int[]public int[] calculateGap(int[] nums) {&nbsp; &nbsp; int len = nums.length;&nbsp; &nbsp; int [] dist = new int[len-1];&nbsp; &nbsp; for(int i = 1; i < len; i++) {&nbsp; &nbsp; &nbsp; &nbsp; dist[i-1] = Math.abs(nums[i] - nums[i-1]);&nbsp; &nbsp; }}//using ArrayListpublic ArrayList<Integer> calculateGap(int[] nums) {&nbsp; &nbsp; int len = nums.length;&nbsp; &nbsp; ArrayList<Integer> dist = new ArrayList<Integer>();&nbsp; &nbsp; for(int i = 1; i < len; i++) {&nbsp; &nbsp; &nbsp; &nbsp; dist.add(Math.abs(nums[i] - num[i-1]));&nbsp; &nbsp; }}但是如果你绝对想使用 Hashmap,这就是你可以在单个循环中完成它的方法:public static ArrayList<Integer> calculateGap(int[] nums) {&nbsp; &nbsp; int l = nums.length;&nbsp; &nbsp; HashMap<Integer, Integer> dist = new HashMap<>();&nbsp; &nbsp; ArrayList<Integer> gaps = new ArrayList<Integer>();&nbsp; &nbsp; int prev = 0;&nbsp; &nbsp; for(int i=1; i<l;i++) {&nbsp; &nbsp; &nbsp; &nbsp; prev = nums[i-1];&nbsp; &nbsp; &nbsp; &nbsp; dist.put(prev, nums[i]);&nbsp; &nbsp; &nbsp; &nbsp; gaps.add(Math.abs(dist.get(prev)-prev));&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; return gaps;}或者使用 int[] 输出:public static int[] calculateGap(int[] nums) {&nbsp; &nbsp; int l = nums.length;&nbsp; &nbsp; Map<Integer, Integer> dist = new HashMap<>();&nbsp; &nbsp; int[] gaps = new int[l-1];&nbsp; &nbsp; int prev = 0;&nbsp; &nbsp; for(int i=1; i<l;i++) {&nbsp; &nbsp; &nbsp; &nbsp; prev = nums[i-1];&nbsp; &nbsp; &nbsp; &nbsp; dist.put(prev, nums[i]);&nbsp; &nbsp; &nbsp; &nbsp; gaps[i-1] = (Math.abs(dist.get(prev)-prev));&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; return gaps;}显然,您不必使用 dist.get(prev) 因为您可以只使用 nums[i],但我假设由于某种原因您想使用 Hashmap 来获取间隙。

Qyouu

您可以执行此操作来填充 HashMap,因为它不需要特殊的边缘处理:for(int i=1; i<l;i++){&nbsp; &nbsp; dist.put(nums[i-1], nums[1]);}&nbsp;但是,如果您不想将其存储为哈希映射,则可以通过创建第二个名为 out put 的数组来对 dist 进行运行总计,然后执行以下操作:for(int i=2; i < l; i++){&nbsp; &nbsp; output[i-2] = Math.abs(nums[i]-nums[i-1]);}仅对数组进行一次迭代,它返回与您的输出相同的输出
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java