当前位置 : 主页 > 编程语言 > c++ >

HashMap应用

来源:互联网 收集:自由互联 发布时间:2021-07-03
gistfile1.txt import java.util.ArrayList;import java.util.Scanner;import java.util.*;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] array = {2, 7, 11, 5}; int t = sc.nextInt(); System
gistfile1.txt
import java.util.ArrayList;
import java.util.Scanner;
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] array = {2, 7, 11, 5};
        int t = sc.nextInt();

        System.out.println(Arrays.toString(twoSum(array, t)));
    }

    public static int[] twoSum(int[] numbers, int target) {
        int[] a = new int[2];
        HashMap
 
   map = new HashMap
  
   (); for (int i = 0; i < numbers.length; i++) { if (map.containsKey(numbers[i])) { a[0] = map.get(numbers[i]); a[1] = i ; return a; } //存储差值为key,下标为value map.put(target - numbers[i], i ); } return a; } }
  
 
上一篇:ShortcutBuilder
下一篇:echars图表动态赋值
网友评论