Problem Description:

Given an array of integers nums and a target value target, find the two integers in the array and the target values and return their array subscripts. You can assume that there is only one answer for each type of input. However, you cannot reuse the same elements in this array.

Example:

Nums [0] + nums[1] = 2 + 7 = 9

Answer:

1. The method of violence

A two-level loop is used to find the two integers and the target values and return the subscripts.

2.HashMap two-layer loop

The values and subscripts in the array are first stored as keys and values in the HashMap, and then the array is iterated to see if the difference is in the HashMap. If so, the subscripts corresponding to the two target values are returned.

Note: there is a defect in the index obtained for the same element in the array. For example,

[3, 3] 6 the result should be [0,0], the actual value is [1,1]

3.HashMap loops once

Store the array elements and subscripts in turn into the HashMap, and find if the target subscript of the current element is already in the HashMap. If so, return the subscript of the target subscript and the current loop subscript.

Code display:

Java version:

import java.util.HashMap;

/ * * @lc app=leetcode.cn id=1 lang=java
 * * [1] Sum of two numbers* / class Solution {   //HashMap loops once  public int[] twoSum(int[] nums, int target) {  HashMap<Integer, Integer> map = new HashMap<>();   for(int j = 0; j < nums.length; j++){  int sub = target - nums[j];  if(map.containsKey(sub)){  return new int[]{map.get(sub), j};  }  map.put(nums[j], j);  }  return new int[2];  }   //HashMap loops twice  public int[] twoSum1(int[] nums, int target) {  HashMap<Integer, Integer> map = new HashMap<>();   for(int i = 0; i < nums.length; i++){  map.put(nums[i], i);  }   for(int j = 0; j < nums.length; j++){  int sub = target - nums[j];  if(map.containsKey(sub) && sub ! = nums[j]){ return new int[]{map.get(sub), j};  }  }  return new int[2];  }   / / method of violence  public int[] twoSum2(int[] nums, int target){  for(int i = 0; i < nums.length; i++){  for(int j = i+1; j < nums.length; j++){  if(nums[i] + nums[j] == target){  return new int[]{i, j};  }  }  }  return new int[2];  } } Copy the code

Go version:

package leetcode

/ / method of violence
func twoSum(nums []int, target int) []int {
    result := make([]int.2)
 for i := 0; i < len(nums); i++ {  for j := i + 1; j < len(nums); j++ {  if nums[i]+nums[j] == target {  result[0] = i  result[1] = j  return result  }  }  }  return result }  // HashMap loops once func twoSum2(nums []int, target int) []int {  result := make([]int.2)  m := make(map[int]int)   for j := 0; j < len(nums); j++ {  sub := target - nums[j]  v, ok := m[sub]  if ok {  result[0] = v  result[1] = j  return result  }  m[nums[j]] = j  }  return result }  // HashMap loops twice, has a bug func twoSum3(nums []int, target int) []int {  result := make([]int.2)  m := make(map[int]int)   for i := 0; i < len(nums); i++ {  m[nums[i]] = i  }   for j := 0; j < len(nums); j++ {  sub := target - nums[j]  v, ok := m[sub]  ifok && v ! = nums[j] { result[0] = v  result[1] = j  return result  }  }  return result } Copy the code

This article is formatted using MDNICE