TwoSum

link : https://leetcode.com/problems/two-sum/description/

backgroud knowlege

  • use go build-in struture whitch that like map or slice

about problem

Give you two array of Integer and give you an integar target, you need to return a indices of the numbers such that they add up to target.

Example

1
2
3
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Code

1
2
3
4
5
6
7
8
9
10
11
12
func twoSum(nums []int, target int) []int {
temp_map := make(map[int]int)
for i:=0;i<len(nums);i++{
remainValue := target - nums[i]
value,ok := temp_map[remainValue]
if ok {
return []int{i,value}
}
temp_map[nums[i]] = i
}
return []int{0,0}
}

TwoSum
http://orikey0.github.io/2024/02/01/TwoSum/
作者
Howard Lee
发布于
2024年2月1日
许可协议