|
|
|
## twosum
|
|
|
|
|
|
|
|
### Instructions
|
|
|
|
|
Refactor & Beautify & destruction commit
return early, remove else branches, reorder conditions and top-level functions, remove empty lines, remove unnecessary append(), fix typos, stop using testing package, remove dead code, fix mistakes in subjects, tests and solutions, remove disclaimers, reformat comments, simplify solutions, tests, add more instructions to subjects, remove obsolete files, etc.
Some of the reasons behind those modifications will be added to good-practices.en.md
Some of the exercises are now broken, they will have to be fixed, most of them have a "TODO:" comment.
5 years ago
|
|
|
Given a slice of integers, return indexes of the two numbers such that they add up to a specific target.
|
|
|
|
|
|
|
|
If there are more than one solution, return the first one.
|
|
|
|
|
|
|
|
If there are no solutions, return nil.
|
|
|
|
|
|
|
|
Expected function :
|
|
|
|
|
|
|
|
```go
|
|
|
|
func TwoSum(nums []int, target int) []int {
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Here is a possible program to test your function :
|
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
Refactor & Beautify & destruction commit
return early, remove else branches, reorder conditions and top-level functions, remove empty lines, remove unnecessary append(), fix typos, stop using testing package, remove dead code, fix mistakes in subjects, tests and solutions, remove disclaimers, reformat comments, simplify solutions, tests, add more instructions to subjects, remove obsolete files, etc.
Some of the reasons behind those modifications will be added to good-practices.en.md
Some of the exercises are now broken, they will have to be fixed, most of them have a "TODO:" comment.
5 years ago
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
case1 := []int{1, 2, 3, 4}
|
|
|
|
out := TwoSum(case1, 5)
|
|
|
|
fmt.Println(out)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
And its output :
|
|
|
|
|
|
|
|
```console
|
|
|
|
student@ubuntu:~/[[ROOT]]/test$ go build
|
|
|
|
student@ubuntu:~/[[ROOT]]/test$ ./test
|
|
|
|
[0 3]
|
|
|
|
student@ubuntu:~/[[ROOT]]/test$
|
|
|
|
```
|