5

JZ-037-数字在排序数组中出现的次数

 2 years ago
source link: https://segmentfault.com/a/1190000041180392
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

JZ-037-数字在排序数组中出现的次数

发布于 12 月 26 日

数字在排序数组中出现的次数

统计一个数字在升序数组中出现的次数。

题目链接: 数字在排序数组中出现的次数

/**
 * 标题:数字在排序数组中出现的次数
 * 题目描述
 * 统计一个数字在升序数组中出现的次数。
 * 题目链接:
 * 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) {
        
    }
}

【每日寄语】 向前走吧,沿着你的道路,鲜花将不断绽放。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK