JZ37 Nombre de fois où le nombre apparaît dans le tableau trié

Description du titre

Comptez le nombre de fois qu'un nombre apparaît dans un tableau croissant.

Code

public class Solution {
    
    
    public int GetNumberOfK(int [] array , int k) {
    
    
        int low = 0;
        int high = array.length-1;
        int mid = (low+high)/2;
        int count = 0;
        while (high-low == 1 ){
    
    
            if (array[mid] > k) {
    
    
                high = mid;
            }else if (array[mid] < k){
    
    
                low = mid;
            }else {
    
    
                break;
            }
            mid = (low+high)/2;
        }
        for (int i = low; i <= high; i++) {
    
    
            if (array[i] == k) {
    
    
                count++;
            }
        }
            return count;
    }

}

Je suppose que tu aimes

Origine blog.csdn.net/qq_41620020/article/details/108493336
conseillé
Classement