• 二分查找(Binary Search)


    • [专题1.二分查找](#001.Binary Search)
    • [有序中查找两个元素](# search for a range)

    // template 1
    // end the loop ,the  left==right
    int binarySearch(vector<int>& nums, int target){
        if(nums.size() == 0)
           return -1;
    
        int left = 0, right = nums.size();
        while(left < right){
        // Prevent (left + right) overflow
           int mid = left + (right - left) / 2;
           if(nums[mid] == target)
                { return mid; }
           else if(nums[mid] < target)
                { left = mid + 1; }
           else 
                { right = mid; }
        }
    
      // Post-processing:
      // End Condition: left == right
        if(left != nums.size() && nums[left] == target)
            return left;
        return -1;
    }
    
    
    // template 2
    // left+1==right
    // 这种方法应用到题目中就是不知道该怎么停止,确定就直接找到两个重复元素
    // 就是直接指针
    int binarySearch(vector<int>& nums, int target){
        if (nums.size() == 0)
            return -1;
    
        int left = 0, right = nums.size() - 1;
        while (left + 1 < right){
            // Prevent (left + right) overflow
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                left = mid;
            } else {
                right = mid;
            }
        }
    
        // Post-processing:
        // End Condition: left + 1 == right
        if(nums[left] == target) return left;
        if(nums[right] == target) return right;
        return -1;
    }
    

    search for a range

    // 我感觉题目有问题
    // 而且,这个代码还是不是理解的很透彻
    class Solution {
    public:
        vector<int> searchRange(vector<int>& nums, int target) {
            vector<int> res(2,-1);
            if(nums.size()<=0)
                return res;
            int start=0;
            int end=nums.size()-1;
            while(start+1<end){
                int mid=start+(end-start)/2;
                    
                if(nums[mid]<target)
                    start=mid+1;
                else
                    end=mid;
            }
            if(nums[start]==target && nums[end]==target)
            {
                res[0]=start;
                res[1]=end;
            }
               
            return res;
            
        }
    };
    

    三种模版之间的比较

    Note: The templates and their differences have been colored coded below.

    Some problems can be implemented using multiple templates, but as you practice more, you will notice that some templates are more suited for certain problems than others.

    These 3 templates differ by their:

    • left, mid, right index assignments
    • loop or recursive termination condition
    • necessity of post-processing
    不要用狭隘的眼光看待不了解的事物,自己没有涉及到的领域不要急于否定. 每天学习一点,努力过好平凡的生活.
  • 相关阅读:
    考虑浏览器兼容的文件上传(IE8不支持FormData)
    IDEA tomcat 部署WEB项目
    如何在springcloud分布式系统中实现分布式锁?
    ABAP DEMO33 选择周的搜索帮助
    ABAP函数篇1 日期函数
    ABAP函数篇2 测试DATE_CONVERT_TO_FACTORYDATE
    增强篇7 判断标准屏幕能否做屏幕增强
    增强篇6 CMOD增强删除
    ABAP DEMO 年月的搜索帮助
    HoloLens开发手记-配置开发环境 Install the tools
  • 原文地址:https://www.cnblogs.com/GeekDanny/p/10014825.html
Copyright © 2020-2023  润新知