4
头图

Number of times the number appears in the sorted array

Title description

Count the number of times a number appears in the ascending array.

title link : number of times the number appears in the sorted array

Code

/**
 * 标题:数字在排序数组中出现的次数
 * 题目描述
 * 统计一个数字在升序数组中出现的次数。
 * 题目链接:
 * https://www.nowcoder.com/practice/70610bf967994b22bb1c26f9ae901fa2?tpId=13&&tqId=11190&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
 */
public class Jz37 {

    /**
     * 暴力法
     *
     * @param array
     * @param k
     * @return
     */
    public int getNumberOfK(int[] array, int k) {
        if (array == null || array.length == 0) {
            return 0;
        }
        int cnt = 0;
        for (int i = 0; i < array.length; i++) {
            if (k == array[i]) {
                cnt++;
            }
        }
        return cnt;
    }

    public static void main(String[] args) {
        
    }
}
[Daily Message] Go ahead, along your path, flowers will continue to bloom.

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!