当前位置:网站首页>Sword finger offer 53 - I. find the number I in the sorted array

Sword finger offer 53 - I. find the number I in the sorted array

2022-07-05 05:28:00 ThE wAlkIng D

Title Description

![ Insert picture description here ](https://img-blog.csdnimg.cn/47a5ebd2365c4f788ce77d9cdd5ae0e7.png
)

Problem analysis

The two methods
The first one is : Violent settlement .
The second kind : Use bisection search , Because it is a sorted array , For the value found , Let him judge whether it is equal , If equal, let count+1;

Code instance

class Solution {
    
    public int search(int[] nums, int target) {
    
        int lefet = 0,right = nums.length()-1,count = 0;
        while(left < right){
    
        	int mid = (left + right)/2;
        	if(nums[mid] >= target){
    
        		right = middle - 1;
        	}
        	if(nums[mid] < target){
    
        		left = middle + 1;
        	}
        }
        while(left < nums.length&&nums[left++] == target){
    
        	count++;
        }
        return count;
    }
}
原网站

版权声明
本文为[ThE wAlkIng D]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050525449073.html