Last active
September 5, 2015 08:05
-
-
Save taji-taji/366786225ab80e429016 to your computer and use it in GitHub Desktop.
【Go】Goのsort.SearchInts()はPHPのin_array()とは全然違った! ref: http://qiita.com/taji-taji/items/ce0ac16adb880fd807b5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sort" | |
) | |
func main() { | |
a := []int{1,9,3,7,5} | |
// まずソートする | |
sort.Ints(a) | |
fmt.Printf("%v\n", a) // [1 3 5 7 9] | |
// インデックス3が返ってくるはず... | |
b := sort.SearchInts(a, 7) | |
fmt.Printf("%v\n", b) // 3 | |
// スライスの中にないことが分かるような値が返ってきてほしい... | |
c := sort.SearchInts(a, 4) | |
fmt.Printf("%v\n", c) // 2 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c := sort.SearchInts(a, 4) | |
fmt.Printf("%v\n", c) // 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func SearchStrings(a []string, x string) int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c := sort.SearchInts(a, 4) | |
fmt.Printf("%v\n", c) // 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment