码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-05-08 15:06:36      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

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

思路:最简单的思路就是遍历统计,时间复杂度是O(n)。但是既然是排序好的,怎么也得用一下二分吧。时间复杂度O(logn)

O(n)实现代码:

public class Solution {
    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(array[i] == k)
                cnt ++;
        }
        return cnt;
    }
}

 

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

标签:

原文地址:http://www.cnblogs.com/wxisme/p/5470488.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!