返回数组索引 结构体 二分查找 Arrays.sort hashmap(未使用) Scanner

本文介绍了一种使用结构体存储键值对<index, key>的方法,并按照key升序排序。在解决寻找数组中键存在的问题时,避免了HashMap可能导致的索引覆盖问题。通过Scanner获取用户输入,用结构体保存键值对,然后对数组进行排序。文章提到了可以使用二分查找法来高效地搜索键在数组中的索引。" 128394766,15017112,Active Directory 中删除对象的恢复方法,"['windows', 'AD', '系统管理']

结构体存储键值对<index,key>,并按照key升序排序

class S implements Comparable<S>{
    int index, key;

    public S(int index, int key) {
        this.index = index;
        this.key = key;
    }

    public int compareTo(S a) { //按数组值key升序排序
        return this.key - a.key;
    
}
S[] d = new S[n];
Arrays.sort(d,1,n);

注:需要在结构体中***重写***Arrays.sort()的compareTo()方法。

也可以写成下面这种形式:

不实现Comparable接口,后面直接在Arrays.sort()方法重写

class S {
    int index, key;

    public S(int index, int key) {
        this.index = index;
        this.key = key;
    }    
}
 S[] d = new S[n];
Arrays.sort(d, new Comparator<S>() {
            @Override
            public int compare(S o1, S o2) {
                return o1.key - o2.key;
            }
        });

一道题目:第1行输入数组容量n,第2行输入数组arr的值,第3行输入要比较的数组cpp的容量,接下来分行输入要比较的数组.若cpp中各元素在arr中存在,分别打印出索引,不存在就打印出-1;

input:

5
1 3 2 4 5
4
1 3 4 6

output:

0
1
3
-1

分析:一开始考虑到用hashmap存储,如果key存在重复的元素,则会导致索引值覆盖,于是尝试将数组的值放在hashmap的value,但是由value来查询索引key效率低而且不记得对应的函数,作罢。于是考虑用结构体。

  1. 用结构体实现键值对<index,key>

  2. 通过 Scanner 类来获取用户的输入,用结构体保存键值对<index,key>到d[]

    Scanner in = new Scanner(System.in);
    int n, i;
    n = in.nextInt();//输入n个数字
    S[] d = new S[n];
    for (i = 0; i < n; i++) {
       int k1 = in.nextInt();
            d[i] = new S(i, k1);
    }
    
  3. 对数组按照key值进行升序排序:

     Arrays.sort(d, new Comparator<S>() {
                @Override
                public int compare(S o1, S o2) {
                    return o1.key - o2.key;
                }
            });
    
  4. 二分查找是否有对应的key,

mid为数组中键值对的个数的一半,通过arr[mid].index和arr[mid].key分别获取对应的index和key 值。

代码为:

 int mid = low + (high - low) / 2;
            if (arr[mid].key == target)//如果key值找到返回对应的索引
                return arr[mid].index;
                ......

完整代码为:

import java.util.*;
class S {
    int index, key;

    public S(int index, int key) {
        this.index = index;
        this.key = key;
    }
}

public class Test {
    public static void main(String args[]) {
        Test test = new Test();
        Scanner in = new Scanner(System.in);
        int n, i;
        n = in.nextInt();//输入n个数字

        S[] d = new S[n];
        for (i = 0; i < n; i++) {
            int k1 = in.nextInt();
            d[i] = new S(i, k1);
        }
        Arrays.sort(d, new Comparator<S>() {
            @Override
            public int compare(S o1, S o2) {
                return o1.key - o2.key;
            }
        });

        int m = in.nextInt();//接下来判断m个数字
        for (int j = 0; j < m; j++) {//要比较的数字存入数组arr
            int k2 = in.nextInt();
            int ex = test.binarySearch(d, k2);
            if (ex >= 0) {
                System.out.println(ex);
            } else {
                System.out.println(-1);
            }
      }
    }

    //按照索引进行二分查找,不需要进行数组值的排序
    public int binarySearch(S[] arr, int target) {
        int low = 0, high = arr.length - 1;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (arr[mid].key == target)
                return arr[mid].index;
            if (target > arr[mid].key) {
                low = mid + 1;
            }
            if (target < arr[mid].key) {
                high = mid - 1;
            }
        }
        return -1;
    }
}            

当然,可以不用二分查找:

 for (int j = 0; j < m; j++) {//要比较的数字存入数组arr
            int k2 = in.nextInt();
            for(i=0; i<n; i++){
               if(k2==d[i].key){ System.out.println(d[i].index); }
                else { if(i==n-1&&k2!=d[i].key){//遍历到最后一个数字依然不相等,则判断不存在
                       System.out.println(-1); }
                        }
                }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值