# 从零打卡leetcode之day 1--两数之和

## 前言

#### 从零打卡leetcode之day 1

```题目描述:

```    //方法一：简单暴力，两个for循环搞定
public int[] twoSum1(int[] nums, int target) {
int[] temp = new int[2];//用来存要找的数的下标
for(int i = 0; i < nums.length; i++){
for(int j = i + 1; j < nums.length; j++){
if(nums[i] + nums[j] == target){
temp[0] = i;
temp[1] = j;
return temp;
}
}
}

return null;
}```

```public int[] twoSum2(int[] nums, int target){
int[] temp = new int[2];
Map<Integer,Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
map.put(nums[i], i);
}        //遍历查找
for(int i = 0; i < nums.length; i++){
int a = nums[i];
if(map.containsKey(target - a)){
temp[0] = i;
temp[1] = map.get(target - a);
return temp;
}
}
return null;
}```

```public int[] twoSum3(int[] nums, int target){
int[] temp = new int[2];
Map<Integer,Integer> map = new HashMap<>(); //遍历查找
for(int i = 0; i < nums.length; i++){
int a = nums[i];
if(map.containsKey(target - a)){
temp[0] = map.get(target - a);
temp[1] = i;
return temp;
}else {//如果找不到则存进去
map.put(nums[i], i);
}
}
return null;
}```

244 篇文章45 人订阅

0 条评论

9920

382100

31890

479120

14330

19670

46570

8100

482120

37250