(LeetCode) Java to solve the sum of two numbers

1. Problem solution

Given an integer array nums and a target value target, please find the two integers whose sum is the target value in the array and return their array subscripts.

You can assume that each input will only correspond to one answer. However, the same element in the array cannot be used twice.

Example:

Given nums = [2, 7, 11, 15], target = 9
because nums[0] + nums[1] = 2 + 7 = 9,
so return [0, 1]

Simple violent solutions will not be introduced

This problem can make use of the hash table, for each element in the array x, can make use of the hash table containsKeyto determine whether there is target - x, then x is inserted into the hash table, x and guaranteed not to let their own match

Two, the code

public class Test {
    
    
    public static void main(String[] args) {
    
    
        int[] a = {
    
    3, 4};
        int[] b = twoSum(a, 6);
        System.out.println(Arrays.toString(b));
    }

    public static int[] twoSum(int[] nums, int target) {
    
    
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; ++i) {
    
    
            if (map.containsKey(target - nums[i])) {
    
    
                //注意这里返回的是个数组
                return new int[]{
    
    map.get(target - nums[i]), i};
            }
            //注意这个入 map 操作只能放这
            map.put(nums[i], i);
        }
        return new int[0];
    }
}

Three, summary

(1) Initialization of the array

int[] score = null;
score = new int[3];
等价于
int score[] = new int[10]; 
数据类型[] 数组名 = {
    
    初值0,初值1,....初值n}
例如:
int[] score = {
    
    32,45,67,90};
数据类型[][] 数组名 = new 数据类型[行的个数][列的个数];
示例:
int[][] score = new int[3][4];//声明整型数组score,同时为其开辟一块内存空间

(2) HashMap: Stored in disorder, the key is not allowed to be repeated

Map<String,String > map = new HashMap<String, String>();//实例化 map
//增加key-value
map.put("1","Java");
//根据 key 求出 value
String val = map.get("2");
//判断是否存在 key-value
if (map.containsKey("1")) {
    
    
   System.out.println("搜索的 key 存在!");
}
if (map.containsValue("Java")) {
    
    
   System.out.println("搜索的 Value 存在!");
}
//输出全部的key
Set<String > keys = map.keySet();//得到全部的 key
Iterator<String> iter = keys.iterator();
System.out.print("全部的 key:");
while (iter.hasNext()){
    
    
   String  str = iter.next();
   System.out.print(str + "、");
}
//输出全部的value
Collection<String > values = map.values();//得到全部的 key
Iterator<String> iter = values.iterator();
System.out.print("全部的 value:");
while (iter.hasNext()){
    
    
    String  str = iter.next();
    System.out.print(str + "、");
}
//或者使用 foreach 输出 Map 实例
for (Map.Entry<String ,String> me:map.entrySet()){
    
    //输出 Set 集合
      System.out.println(me.getKey() + "-->" + me.getValue());
}

(3) This way of outputting an array is very powerful:

//注意这里返回的是个数组
return new int[]{
    
    map.get(target - nums[i]), i};

Guess you like

Origin blog.csdn.net/nanhuaibeian/article/details/109299086