p149 第一个出错的版本(leetcode 278)
作者:互联网
一:解题思路
采用二分搜索的思想来做这道题目。Time:O(log(n)),Space:O(1)
二:完整代码示例 (C++版和Java版)
C++:
class Solution { public: int firstBadVersion(int n) { int low = 1; int high = n; while (low < high) { int mid = low + (high-low) / 2; if (!isBadVersion(mid)) low = mid + 1; else high = mid; } return low; } };
Java:
public class Solution extends VersionControl { public int firstBadVersion(int n) { int low=1,high=n; while (low<high) { int mid=low+(high-low)/2; if(!isBadVersion(mid)) low=mid+1; else high=mid; } return low; } }
标签:Java,int,mid,public,high,p149,low,278,leetcode 来源: https://www.cnblogs.com/repinkply/p/12743043.html