74. First Bad Version 【medium】

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

You can call isBadVersion to help you determine which version is the first bad one. The details interface can be found in the code's annotation part.

Notice

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

Example

Given n = 5:

  1. isBadVersion(3) -> false
  2. isBadVersion(5) -> true
  3. isBadVersion(4) -> true

Here we are 100% sure that the 4th version is the first bad version.

Challenge

You should call isBadVersion as few as possible.

解法一:

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

74. First Bad Version 【medium】的更多相关文章

  1. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  2. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  3. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  4. 61. Search for a Range【medium】

    61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...

  5. 62. Search in Rotated Sorted Array【medium】

    62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...

  6. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  7. 159. Find Minimum in Rotated Sorted Array 【medium】

    159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...

  8. Java for LeetCode 207 Course Schedule【Medium】

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  9. 114. Flatten Binary Tree to Linked List【Medium】【将给定的二叉树转化为“只有右孩子节点”的链表(树)】

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...

随机推荐

  1. 【推导】【贪心】Codeforces Round #402 (Div. 2) E. Bitwise Formula

    按位考虑,每个变量最终的赋值要么是必为0,要么必为1,要么和所选定的数相同,记为2,要么和所选定的数相反,记为3,一共就这四种情况. 可以预处理出来一个真值表,然后从前往后推导出每个变量的赋值. 然后 ...

  2. WPF 中依赖属性的继承(Inherits)

    WPF中依赖属性的值是是可以设置为可继承(Inherits)的,这种模式下,父节点的依赖属性会将其值传递给子节点.例如,数据绑定中经常使用的DataContextProperty: var host ...

  3. winform groupbox控件放到窗体中间位置

    1. 在Form中放一个控件,让其在启动时始终居中 int gLeft = this.Width / 2 - groupControl1.Width / 2; int gTop = this.Heig ...

  4. activemq 5.13.2 jdbc 数据库持久化 异常 找不到驱动程序

    原文:https://my.oschina.net/u/2284972/blog/662033 摘要: activemq jdbc 数据库持久化 异常 找不到驱动程序 Caused by: java. ...

  5. Redis编程实践【pub/sub】

    原文:http://shift-alt-ctrl.iteye.com/blog/1867454 Redis或许已经在很多企业开始推广并试水,本文也根据个人的实践,简单描述一下Redis在实际开发过程中 ...

  6. java源码阅读LinkedList

    1类签名与注释 public class LinkedList<E> extends AbstractSequentialList<E> implements List< ...

  7. 解决防火墙限制远程连接MySQL(导致错误10060可能之一)

    打开windows防火墙,打开高级设置 1. 入站规则设置 ① 选择入站规则,然后新建规则,选择端口,然后下一步 ② 选择TCP,选择特定端口,然后输入端口,如有多个端口需要用逗号隔开了 例如: 33 ...

  8. 阿里云部署Java web项目初体验

    林炳文Evankaka原创作品. 转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了怎样在阿里云上安装JDK.Tomcat以及其配置过程. 最后以一个实例来演 ...

  9. 炫酷的sublimeText开发工具 快捷键总结

     sublimeText3绝对是写前端程序的利器.占内存小.启动快.代码提示功能齐全,界面酷炫并且再也不怕断电.了 网上搜罗一些快捷键总结一下.先来张图: (事实上sublimeText3也能够写 ...

  10. iOS 带IAP提交注意事项及无法submit for review的解决方案

    原地址:http://blog.sina.com.cn/s/blog_71ce775e0101dl4a.html 最近项目接触到了苹果的程序内购(IAP),碰到不少问题,参考了很多帖子才得以解决.在此 ...