Title: given an integer array nums And an integer target value, please find and as the target value target in the array That Two Integers and return their array subscripts.
You can assume that each input will correspond to only one answer. However, the same element in the array cannot be repeated in the answer.
You can return answers in any order.
Example 1:
Input: num = [2,7,11,15], target = 9
Output: [0,1]
Explanation: because num [0] + num [1] = = 9, return [0, 1].
Example 2:
Input: num = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: num = [3,3], target = 6
Output: [0,1]
Tips:
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
There will only be one valid answer
Advanced: can you think of an algorithm with a time complexity less than O(n2)?
Source: LeetCode
Link: https://leetcode-cn.com/problems/two-sum
The following answers are summarized by the leaders of LeetCode. I found several suitable for Xiaobai's understanding and modified them myself. Welcome to learn from them
Java version
Method 1: for loop -- the time complexity is O(n)*O(n)=O(n^2)
Idea:
Declare an int array: int [] ans = New Int [2], in the format of type [] variable name = new type [capacity],
We define an array of int type, named ans, with a capacity of 2. It can store two int values, ans[0] and ans[1], respectively.
For loop statement: for (int i = 0; I < n; I + +) {}
If we want to find two numbers so that their sum is equal to target, the simplest idea is to traverse all numbers, select two and check whether their sum is equal to target
class Solution { public int[] twoSum(int[] nums, int target) { //We need to find that the sum of the two numbers is equal to target //That is, we need to find num [i] + num [J] = = target, and return their subscripts, I and j, where I= j int[ ] ans = new int[2]; //Declare an array of size 2 to hold the results //We loop through all the numbers int n = nums.length; //Save the length of nums with a variable len //i is the subscript of the first number. nums has n numbers in total, so the value range of i is [0, n-1] for(int i = 0; i < n; i++){ //j is the subscript of the second number. for(int j = i + 1; j < n; j++){ //For each number nums[i], we verify that the sum of other numbers (nums[j]) and it is equal to target //If the conditions are met if(nums[i] + nums[j] == target){ //Writes the subscript to the return value array ans[0] = i; ans[1] = j; //return return ans; } } } //Default return value return new int[0]; } }
Method 2: HashMap -- time complexity O(n)
Idea:
import java.util.HashMap
HashMap is a data structure composed of key value pairs, which is equivalent to dict (Dictionary) in Python. Key is obtained by hash function,
The reading and writing efficiency is O(1), which is the most commonly used data structure to improve time efficiency.
Declaration: Map < Integer, Integer > hashtable = new HashMap < > (); The type is HashMap, < Integer, Integer > indicates that the categories of key and value are both Integer, and Integer is the wrapper class of int, which can be roughly regarded as the same as int. The variable name is hashtable. New means to create an object,
What is created is a HashMap. The following < > needs to be written as < integer, integer > according to the complete writing method, but now java has local type inference, so it can not be written.
class Solution { public int[] twoSum(int[] nums, int target) { //Declare a HashMap. The types of key and value are int, and the variable name is hashtable. Key and value record the value and subscript respectively Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>(); for (int i = 0; i < nums.length; i++) { //The containsKey method of HashMap determines whether this key exists in the HashMap //If there is num [J] + num [i] = = target, then num [J] = target - num [i] if (hashtable.containsKey(target - nums[i])) { //If nums[j] exists, the subscripts of nums[j] and nums[j] are returned return new int[]{hashtable.get(target - nums[i]), i}; } //Otherwise, save nums[i] into hashtable hashtable.put(nums[i], i); } return new int[0]; } }
Python version
Method 1: list
class Solution(object): def twoSum(self, nums, target): for i in range(len(nums)): j=target - nums[i] if j in nums: if i != nums.index(j): return [i,nums.index(j)]
Method 2: Dictionary
class Solution(object): def twoSum(self, nums, target): dict = {} for i in range(len(nums)): a = target - nums[i] if a in dict: return dict[a],i else: dict[nums[i]] = i
The C language version is too long. It's not very concise, so I don't organize it