File tree Expand file tree Collapse file tree 1 file changed +28
-9
lines changed Expand file tree Collapse file tree 1 file changed +28
-9
lines changed Original file line number Diff line number Diff line change 1+ package main
2+
3+ import "fmt"
4+
15/**
26 * Forward declaration of isBadVersion API.
37 * @param version your guess about first bad version
711 */
812
913func search (start , end int ) int {
10- half := int ((end - start )/ 2 ) + start
11- if ! isBadVersion (half ) {
12- return search (half + 1 , end )
13- }
14+ half := int ((end - start )/ 2 ) + start
15+ if ! isBadVersion (half ) {
16+ return search (half + 1 , end )
17+ }
1418
15- if isBadVersion (half - 1 ) {
16- return search (start , half )
17- }
19+ if isBadVersion (half - 1 ) {
20+ return search (start , half )
21+ }
1822
19- return half
23+ return half
2024
2125}
2226
27+ func isBadVersion (v int ) bool {
28+ switch v {
29+ case 8 , 9 , 10 :
30+ return true
31+ default :
32+ return false
33+ }
34+
35+ return false
36+ }
37+
2338func firstBadVersion (n int ) int {
24- return search (1 , n + 1 )
39+ return search (1 , n + 1 )
40+ }
41+
42+ func main () {
43+ fmt .Println (firstBadVersion (10 ))
2544}
You can’t perform that action at this time.
0 commit comments