# LeetCode 1. Sum of two numbers C / C + + / Python 3 / Java [forgot to learn the code again]

Given an integer array nums and an integer target value target, please find the two integers with and as the target value target in the array 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.

Brute force O(n^2)
Hash table 2 times, 1 time

Make force buckle for the first time

Compilation error: char 1: error: control reaches end of non void function [- werror = return type] [solution. C]
The last line of force deduction must be a return statement

## C language

The first reaction is to write, brute force cracking, no bending

```int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int i,j;
int *result=(int *)malloc(sizeof(int)*2);
*returnSize=2;              //returnSize is the size of the returned array, which is constant to 2. No error is reported
for(i=0;i<numsSize-1;i++)
for(j=i+1;j<numsSize;j++)
if(nums[i]+nums[j]==target){
result=i;
result=j;
return result;
}
return result;
}
```

## C++

Knowledge points:
(1) A vector is a dynamic array that can hold any type. The continuous storage space is used to store elements, and subscripts can be used to access the elements of the vector
(2)MapName[n[i]]=i； When the key repeats, update the value
(3) Mapname. Insert(), keep value when the key is repeated
In parentheses:
pair<int,string>(1,"aaa")
make_pair(1,"aaa")
map<int,string>::value_type(1,"aaa")
(4) Each class in STL has a value_type is the type of data contained in the container
(5) :: table scope a. reference static member functions or defined functions outside the class b. namespace scope STD:: cout C.:: Val table global variables

Store the value of array nums as the key and the subscript as the value in the map
If there is a key with the current num[i] and target in the hash, and the value of the key is not itself, find the understanding

Suppose the input is [3,7,3,15] and the target value is 6. During the first traversal, when the second 3 appears, the value of hash is updated from 0 to 2. In the second traversal, when i==0, the value of hash is 2, indicating that two 3 are not the same 3, so find the solution.

Similarly, it is the same without updating the value value. When the solution is two numbers a1 and a2 with the same value, not updating the value is equivalent to storing the subscript b1 of a1. When running to a1, it is the same number, and it is not required to continue running. Run to a2 in turn to obtain the lost subscript of a2. At this time, the requirement that it is not the same number is met and the answer is obtained.

```class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int,int> hash;
int n=nums.size();
for(int i=0;i<n;i++)
hash[nums[i]]=i;
for(int i=0;i<n;i++)
if(hash.count(target-nums[i])==1&&hash[target-nums[i]]!=i)
return {i,hash[target-nums[i]]};
return {};
}

};
```

## Python3

Immutable types: approximation passing

```a=5
a=10 #5 discarded and regenerated a
```

Variable types: reference passing

```a=[1,2,3]
a=5 #a fixed, value modified
```

The key value pair is inserted into the dictionary dict after the judgment statement, so each number is found in front of it, and the range does not include itself, so there is no need to consider the case that the solution is 2a and the current value is a.

When searching whether the current a2 is a solution, at this time {a2 ':'b2'} has not been inserted into the dictionary, and the subscript of data a1 will not be lost.
Because it's from back to front, the current i must be the second number, but the title doesn't require the order

```class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
dict={}
for i in range (0,len(nums)):
if (target-nums[i]) in dict:
return [dict[target-nums[i]],i]
dict[nums[i]]=i
```

## Java

This is the code of a big brother in the comment area. I wrote python according to this idea
Array copy (not used here)
1.clone, inherited from the object class. Basic data types are available, and string type values are available only when they remain unchanged.
2.System.arraycopy(a1,1,a2,3,3) (original array, original start position, target array, target start position, number of copies)
3.a2=Arrays.copyof(a1,3) (original array, number of copies) System.arraycopy for the bottom layer
4. Deep copy: implement clonable interface and rewrite clone

```class Solution {
public int[] twoSum(int[] nums, int target) {
// int[] a=nums.clone();
int[] indexs = new int;
HashMap<Integer,Integer> hash=new  HashMap<Integer,Integer>();
for(int i = 0; i < nums.length; i++){
if(hash.containsKey(nums[i])){
indexs = i;
indexs = hash.get(nums[i]);
return indexs;
}
hash.put(target-nums[i],i);
}
return indexs;
}
}
```

The first "simple" question is so hard. I really don't remember what I learned before, ORZ

Tags: C C++ leetcode

Posted on Wed, 27 Oct 2021 09:18:23 -0400 by Unipus