The purpose is to understand Go basic syntax and algorithm brain training.
main.go
package main
import "fmt"
func main() {
l := []int{1, 2, 3, 4, 5}
m := 5
r := solve(l, m)
fmt.Printf("%v", r)
}
func solve(a []int, b int) string {
r := "NG"
for i := 0; i < len(a); i++ {
if a[i] == b {
r = "ok"
}
}
return r
}
l := []int{1, 2, 3, 4, 5}
m := 5
At the above time, I was able to confirm that the numerical value was included in the loop processing at the end of the for statement,
l := []int{1, 2, 3, 4, 5}
m := 1
In such a case, you are doing useless processing other than the first loop processing. Therefore, it is best to write as follows.
if a[i] == b {
r = "ok"
break
}
Recommended Posts