File tree Expand file tree Collapse file tree 5 files changed +181
-28
lines changed
solution/0200-0299/0278.First Bad Version Expand file tree Collapse file tree 5 files changed +181
-28
lines changed Original file line number Diff line number Diff line change 2525
2626<!-- 这里可写通用的实现逻辑 -->
2727
28+ 二分查找。
29+
2830<!-- tabs:start -->
2931
3032### ** Python3**
3133
3234<!-- 这里可写当前语言的特殊实现逻辑 -->
3335
3436``` python
35-
37+ # The isBadVersion API is already defined for you.
38+ # @param version, an integer
39+ # @return an integer
40+ # def isBadVersion(version):
41+
42+ class Solution :
43+ def firstBadVersion (self , n ):
44+ """
45+ :type n: int
46+ :rtype: int
47+ """
48+ low, high = 1 , n
49+ while low < high:
50+ mid = low + ((high - low) >> 1 )
51+ if isBadVersion(mid):
52+ high = mid
53+ else :
54+ low = mid + 1
55+ return low
3656```
3757
3858### ** Java**
3959
4060<!-- 这里可写当前语言的特殊实现逻辑 -->
4161
4262``` java
63+ /* The isBadVersion API is defined in the parent class VersionControl.
64+ boolean isBadVersion(int version); */
65+
66+ public class Solution extends VersionControl {
67+ public int firstBadVersion (int n ) {
68+ int low = 1 , high = n;
69+ while (low < high) {
70+ int mid = low + ((high - low) >> 1 );
71+ if (isBadVersion(mid)) high = mid;
72+ else low = mid + 1 ;
73+ }
74+ return low;
75+ }
76+ }
77+ ```
4378
79+ ### ** JavaScript**
80+
81+ ``` js
82+ /**
83+ * Definition for isBadVersion()
84+ *
85+ * @param {integer} version number
86+ * @return {boolean} whether the version is bad
87+ * isBadVersion = function(version) {
88+ * ...
89+ * };
90+ */
91+
92+ /**
93+ * @param {function} isBadVersion ()
94+ * @return {function}
95+ */
96+ var solution = function (isBadVersion ) {
97+ /**
98+ * @param {integer} n Total versions
99+ * @return {integer} The first bad version
100+ */
101+ return function (n ) {
102+ let low = 1 ,
103+ high = n;
104+ while (low < high) {
105+ const mid = low + ((high - low) >> 1 );
106+ if (isBadVersion (mid)) {
107+ high = mid;
108+ } else {
109+ low = mid + 1 ;
110+ }
111+ }
112+ return low;
113+ };
114+ };
44115```
45116
46117### ** ...**
Original file line number Diff line number Diff line change @@ -37,13 +37,82 @@ Then 4 is the first bad version. </code>
3737### ** Python3**
3838
3939``` python
40-
40+ # The isBadVersion API is already defined for you.
41+ # @param version, an integer
42+ # @return an integer
43+ # def isBadVersion(version):
44+
45+ class Solution :
46+ def firstBadVersion (self , n ):
47+ """
48+ :type n: int
49+ :rtype: int
50+ """
51+ low, high = 1 , n
52+ while low < high:
53+ mid = low + ((high - low) >> 1 )
54+ if isBadVersion(mid):
55+ high = mid
56+ else :
57+ low = mid + 1
58+ return low
4159```
4260
4361### ** Java**
4462
4563``` java
64+ /* The isBadVersion API is defined in the parent class VersionControl.
65+ boolean isBadVersion(int version); */
66+
67+ public class Solution extends VersionControl {
68+ public int firstBadVersion (int n ) {
69+ int low = 1 , high = n;
70+ while (low < high) {
71+ int mid = low + ((high - low) >> 1 );
72+ if (isBadVersion(mid)) high = mid;
73+ else low = mid + 1 ;
74+ }
75+ return low;
76+ }
77+ }
78+ ```
4679
80+ ### ** JavaScript**
81+
82+ ``` js
83+ /**
84+ * Definition for isBadVersion()
85+ *
86+ * @param {integer} version number
87+ * @return {boolean} whether the version is bad
88+ * isBadVersion = function(version) {
89+ * ...
90+ * };
91+ */
92+
93+ /**
94+ * @param {function} isBadVersion ()
95+ * @return {function}
96+ */
97+ var solution = function (isBadVersion ) {
98+ /**
99+ * @param {integer} n Total versions
100+ * @return {integer} The first bad version
101+ */
102+ return function (n ) {
103+ let low = 1 ,
104+ high = n;
105+ while (low < high) {
106+ const mid = low + ((high - low) >> 1 );
107+ if (isBadVersion (mid)) {
108+ high = mid;
109+ } else {
110+ low = mid + 1 ;
111+ }
112+ }
113+ return low;
114+ };
115+ };
47116```
48117
49118### ** ...**
Original file line number Diff line number Diff line change 33
44public class Solution extends VersionControl {
55 public int firstBadVersion (int n ) {
6- int low = 1 , high = n ;
7- while (low < high ) {
8- int mid = low + (high - low ) / 2 ;
9- if (isBadVersion (mid )) {
10- high = mid ;
11- } else {
12- low = mid + 1 ;
13- }
14- }
15- return low ;
6+ int low = 1 , high = n ;
7+ while (low < high ) {
8+ int mid = low + ((high - low ) >> 1 );
9+ if (isBadVersion (mid )) high = mid ;
10+ else low = mid + 1 ;
11+ }
12+ return low ;
1613 }
1714}
Original file line number Diff line number Diff line change 11/**
22 * Definition for isBadVersion()
3- *
3+ *
44 * @param {integer } version number
55 * @return {boolean } whether the version is bad
66 * isBadVersion = function(version) {
1212 * @param {function } isBadVersion()
1313 * @return {function }
1414 */
15- const solution = function ( isBadVersion ) {
15+ var solution = function ( isBadVersion ) {
1616 /**
1717 * @param {integer } n Total versions
1818 * @return {integer } The first bad version
1919 */
20- return function ( n ) {
21- if ( n === 1 ) return n ;
22- let left = 1 ,
23- right = n ;
24- while ( left < right ) {
25- let mid = left + Math . floor ( ( right - left ) / 2 ) ;
26- if ( isBadVersion ( mid ) ) {
27- if ( mid === left ) return mid ;
28- else right = mid ;
29- } else {
30- if ( isBadVersion ( mid + 1 ) ) return mid + 1 ;
31- else left = mid + 1 ;
20+ return function ( n ) {
21+ let low = 1 , high = n ;
22+ while ( low < high ) {
23+ const mid = low + ( ( high - low ) >> 1 ) ;
24+ if ( isBadVersion ( mid ) ) {
25+ high = mid ;
26+ } else {
27+ low = mid + 1 ;
28+ }
3229 }
33- }
30+ return low ;
3431 } ;
35- } ;
32+ } ;
Original file line number Diff line number Diff line change 1+ # The isBadVersion API is already defined for you.
2+ # @param version, an integer
3+ # @return an integer
4+ # def isBadVersion(version):
5+
6+ class Solution :
7+ def firstBadVersion (self , n ):
8+ """
9+ :type n: int
10+ :rtype: int
11+ """
12+ low , high = 1 , n
13+ while low < high :
14+ mid = low + ((high - low ) >> 1 )
15+ if isBadVersion (mid ):
16+ high = mid
17+ else :
18+ low = mid + 1
19+ return low
You can’t perform that action at this time.
0 commit comments