• Guess Number Higher or Lower && 九章二分法模板


    We are playing the Guess Game. The game is as follows:

    I pick a number from 1 to n. You have to guess which number I picked.

    Every time you guess wrong, I'll tell you whether the number is higher or lower.

    You call a pre-defined API guess(int num) which returns 3 possible results (-11, or 0):

    -1 : My number is lower
     1 : My number is higher
     0 : Congrats! You got it!
    

    Example:

    n = 10, I pick 6.
    
    Return 6.
    
    Show Company Tags
    Show Tags
    Show Similar Problems
     

    binary search

    /* The guess API is defined in the parent class GuessGame.
       @param num, your guess
       @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
          int guess(int num); */
    
    public class Solution extends GuessGame {
        public int guessNumber(int n) {
            int start = 1;
            int end = n;
            int res;
            while(start<end)
            {
                int mid = start + (end-start)/2;
                if(guess(mid)==0)
                {
                    return mid;
                }
                else if(guess(mid)==-1)
                {
                    end = mid-1;
                }
                else
                {
                    start = mid+1;
                }
            }
            if(guess(start)==0) return start;
            return end;
        
        }
    }

    九章二分法模板

     主页
    课程
    讲座
    问答
    面经
    LeetCode答案
    FAQ
    注册 登录
     Binary Search    当前位置 首页 LeetCode 答案查询 / LintCode 答案查询  查看详情
    http://www.lintcode.com/problem/binary-search/
    
    Binary search is a famous question in algorithm. For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) time complexity. If the target number does not exist in the array, return -1. Example If the array is [1, 2, 3, 3, 4, 5, 10], for given target 3, return 2.
    Java
    C++
    Python
    /**
     * 本代码由九章算法编辑提供。没有版权欢迎转发。
     * - 九章算法致力于帮助更多中国人找到好的工作,教师团队均来自硅谷和国内的一线大公司在职工程师。
     * - 现有的面试培训课程包括:九章算法班,系统设计班,九章强化班,Java入门与基础算法班,
     * - 更多详情请见官方网站:http://www.jiuzhang.com/
     */
    
    // version 1: with jiuzhang template
    public class Solution {
        /**
         * @param A an integer array sorted in ascending order
         * @param target an integer
         * @return an integer
         */
        public int findPosition(int[] nums, int target) {
            if (nums == null || nums.length == 0) {
                return -1;
            }
            
            int start = 0, end = nums.length - 1;
            while (start + 1 < end) {
                int mid = start + (end - start) / 2;
                if (nums[mid] == target) {
                    return mid;
                } else if (nums[mid] < target) {
                    start = mid;
                } else {
                    end = mid;
                }
            }
            
            if (nums[start] == target) {
                return start;
            }
            if (nums[end] == target) {
                return end;
            }
            return -1;
        }
    }
    
    // version 2: without jiuzhang template
    public class Solution {
        /**
         * @param A an integer array sorted in ascending order
         * @param target an integer
         * @return an integer
         */
        public int findPosition(int[] nums, int target) {
            if (nums == null || nums.length == 0) {
                return -1;
            }
            
            int start = 0, end = nums.length - 1;
            while (start < end) {
                int mid = start + (end - start) / 2;
                if (nums[mid] == target) {
                    return mid;
                } else if (nums[mid] < target) {
                    start = mid + 1;
                } else {
                    end = mid - 1;
                }
            }
            
            if (nums[start] == target) {
                return start;
            }
            return -1;
        }
    }
     
     © Jiu Zhang 2013-2016. All rights reserved.
  • 相关阅读:
    Redis 持久化
    Redis 事务
    select poll和 epoll
    jdk信任证书
    Java中的锁分类
    mysql触发器同步远程服务器上数据库
    正则表达式
    mysql主从同步
    MySQL逗号分割字段的行列转换技巧
    Mysql中文排序
  • 原文地址:https://www.cnblogs.com/hygeia/p/5702770.html
Copyright © 2020-2023  润新知