Nine-chapter algorithm surface question 53 first error code version

Source: Internet
Author: User

Nine Chapters count judges Net-original website


http://www.jiuzhang.com/problem/53/


Topics

In the early SVN code controller, the code version number was incremented from 1. One day you submit a bug code to the code base, which causes the unit test on this version to fail Tests. And unfortunately, because everyone who contributes code to this codebase is more independent, and you're on vacation, no one fixes the bug, which results in a single version of the unit test that doesn't work. If you say that the code version number is now N. Find out the first version of the code that went wrong (that is, the bug you submitted).


You can call a function isbadversion (ID) to test if the version number ID is a good version that can be tests through the unit. Your algorithm should call isbadversion this function as little as possible.
Online test

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

Answer

The simple dichotomy method can be used to solve the problem.

Python Code:

Start, end = 1, n

While start + 1 < end:

Mid = (start + end)/2

If Isbadversion (mid):

End = Mid

Else

Start = mid + 1

If Isbadversion (start):

return start

Return end

Nine-chapter algorithm surface question 53 first error code version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.