leetcode解題模板 —— 二分查詢

不放棄的蝸牛發表於2020-12-13

1.模板

		vector<int>& nums
        int left = 0;//左邊界
        int right = nums.size()-1;//右邊界
        int mid = 0;
        while(left <= right) {
            mid = (left + right) / 2;
            if(target == nums[mid]) {/*操作*/ }
            if(target < nums[mid]) {
                right = mid-1;
            }
            else {
                left = mid+1;
            }
        }

2.實戰
leetcode 35. 搜尋插入位置

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        if(nums.size() == 0) return 0;
        int left = 0;
        int right = nums.size()-1;
        if(nums[left] >= target) {return 0;}
        if(nums[right] < target) {return nums.size();}
        int mid = 0;
        while(left <= right) {
            mid = (left + right) / 2;
            if(target == nums[mid]) { return mid;}
            if(target < nums[mid]) {
                right = mid-1;
            }
            else {
                left = mid+1;
            }
        }
        return left;
    }
};

leetcode 74. 搜尋二維矩陣

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        if(matrix.size() == 0 || matrix[0].size() == 0) {return false;}
        int n = matrix[0].size()-1;
        int m = matrix.size()-1;
        if(matrix[0][0] > target || matrix[m][n] < target) {return false;}
        int left = 0;
        int right = m;
        int mid = 0;
        while(left <= right) {
            mid = (left + right) / 2;
            if(matrix[mid][0] == target) {return true;}
            if(target < matrix[mid][0]){
                right = mid -1;
            }
            else {
                left = mid + 1;
            }
        }
        int a = left-1;
        left = 0;
        right = n;
        while(left <= right) {
            mid = (left + right) / 2;
            if(matrix[a][mid] == target) {return true;}
             if(target < matrix[a][mid]){
                right = mid -1;
            }
            else {
                  left = mid + 1;
             }
        }
        return false;  
    }
};

leetcode 1351. 統計有序矩陣中的負數

class Solution {
public:
    int countNegatives(vector<vector<int>>& grid) {
        if(grid.size() == 0 || grid[0].size() == 0) {
            return 0;
        }
        int m = grid.size() - 1;
        int n = grid[0].size() - 1;
        int ans = 0;
        for(int i = 0; i <= m; i++) {
           
            if(grid[i][0] < 0) {
                ans += n + 1;
                continue;
            }
            if(grid[i][n] >= 0) {continue;}
            int left = 0;
            int right = n;
            int mid = 0;
            while(left < right) {
                mid = (left + right) / 2;
                if(grid[i][mid] == 0) {
                    while(mid + 1 <= n && grid[i][mid + 1] == 0) {mid++;}
                    left = mid +1;
                }
                if(grid[i][mid] > 0) {
                    left = mid + 1;
                }
                else {
                    right = mid - 1;
                }
           }
           if(grid[i][left]<0){
               ans += n - left + 1;
           }else {
               ans += n - left;
           }
        }
        return ans;
    }
};

leetcode解題模板 —— 目錄

相關文章