Lintcode: First Bad Version 解题报告

First Bad Version

http://lintcode.com/en/problem/first-bad-version

The code base version is an integer and start from 1 to n. One day, someone commit a bad version in the code case, so it caused itself and the following versions are all failed in the unit tests.

You can determine whether a version is bad by the following interface:

Java:    public VersionControl {        boolean isBadVersion(int version);    }C++:    class VersionControl {    public:        bool isBadVersion(int version);    };Python:    class VersionControl:        def isBadVersion(version)

Find the first bad version.

Note

You should call isBadVersion as few as possible.

Please read the annotation in code area to get the correct way to call isBadVersion in different language. For example, Java is VersionControl.isBadVersion.

Example

Given n=5

Call isBadVersion(3), get false

Call isBadVersion(5), get true

Call isBadVersion(4), get true

return 4 is the first bad version

Challenge

Do not call isBadVersion exceed O(logn) times.

Tags Expand

SOLUTION 1:

九章算法模板解法,注意,一定要使用left + 1 < right 作为while的条件,这样子不会产生死循环和越界的情况。

 1 /**
 2  * public class VersionControl {
 3  *     public static boolean isBadVersion(int k);
 4  * }
 5  * you can use VersionControl.isBadVersion(k) to judge wether
 6  * the kth code version is bad or not.
 7 */
 8 class Solution {
 9     /**
10      * @param n: An integers.
11      * @return: An integer which is the first bad version.
12      */
13     public int findFirstBadVersion(int n) {
14         // write your code here
15         if (n == 1) {
16             return 1;
17         }
18
19         int left = 1;
20         int right = n;
21
22         while (left + 1 < right) {
23             int mid = left + (right - left) / 2;
24             if (VersionControl.isBadVersion(mid)) {
25                 right = mid;
26             } else {
27                 left = mid;
28             }
29         }
30
31         if (VersionControl.isBadVersion(left)) {
32             return left;
33         }
34
35         return right;
36     }
37 }

SOLUTION 2:

也可以简化一点儿:

 1 /**
 2  * public class VersionControl {
 3  *     public static boolean isBadVersion(int k);
 4  * }
 5  * you can use VersionControl.isBadVersion(k) to judge wether
 6  * the kth code version is bad or not.
 7 */
 8 class Solution {
 9     /**
10      * @param n: An integers.
11      * @return: An integer which is the first bad version.
12      */
13     public int findFirstBadVersion(int n) {
14         // write your code here
15         if (n == 1) {
16             return 1;
17         }
18
19         int left = 1;
20         int right = n;
21
22         while (left < right) {
23             int mid = left + (right - left) / 2;
24             if (VersionControl.isBadVersion(mid)) {
25                 right = mid;
26             } else {
27                 left = mid + 1;
28             }
29         }
30
31         return right;
32     }
33 }

时间: 2024-10-14 17:11:28

Lintcode: First Bad Version 解题报告的相关文章

Lintcode: Sort Colors II 解题报告

Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k. 注意

Lintcode: Majority Number II 解题报告

Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array. Find it. Note There is only one majority number in the arra

Lintcode:Longest Common Subsequence 解题报告

Longest Common Subsequence Given two strings, find the longest comment subsequence (LCS). Your code should return the length of LCS. 样例For "ABCD" and "EDCA", the LCS is "A" (or D or C), return 1 For "ABCD" and "

Lintcode: Kth Largest Element 解题报告

Kth Largest Element Find K-th largest element in an array. Note You can swap elements in the array Example In array [9,3,2,4,8], the 3th largest element is 4 Challenge O(n) time, O(1) space 原题链接: http://www.lintcode.com/en/problem/kth-largest-element

pat1001. Battle Over Cities - Hard Version 解题报告

/**题目:删去一个点,然后求出需要增加最小代价的边集合生成连通图思路:prim+最小堆1.之前图中未破坏的边必用,从而把两两之间可互达的点集合 合并成一个点2.求出不同点集合的最短距离,用prim+最小堆求出最小生成树 kruskal1.之前图中未破坏的边必用,全部加到图中2.途中被破坏的边按照边权从小到大的顺序依次加入图中,直到图变为连通图 两个方法的对应一个点的最小生成树的复杂度都是nlogm,第二个方法较好写 优化:1.未破坏的边直接加入图中2.当有n-2条边(当前删去一个点后,图中n-

解题报告【pat-1076】

最近一直在忙项目都没时间好好总结写博客,说起来真实惭愧啊. 下面就把自己最近做的几题好好总结一下,主要记录一些注意点,以防以后遇到再犯. 1076. Forwards on Weibo (30) 时间限制 3000 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Weibo is known as the Chinese version of Twitter.  One user on Weibo may have many

LeetCode: Search in Rotated Sorted Array II 解题报告

Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the arr

解题报告 之 POJ3057 Evacuation

解题报告 之 POJ3057 Evacuation Description Fires can be disastrous, especially when a fire breaks out in a room that is completely filled with people. Rooms usually have a couple of exits and emergency exits, but with everyone rushing out at the same time

hdu 1541 Stars 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目意思:有 N 颗星星,每颗星星都有各自的等级.给出每颗星星的坐标(x, y),它的等级由所有比它低层(或者同层)的或者在它左手边的星星数决定.计算出每个等级(0 ~ n-1)的星星各有多少颗. 我只能说,题目换了一下就不会变通了,泪~~~~ 星星的分布是不是很像树状数组呢~~~没错,就是树状数组题来滴! 按照题目输入,当前星星与后面的星星没有关系.所以只要把 x 之前的横坐标加起来就可以了