2 Star 0 Fork 0

taotechip/CodeForInterview

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Q20.md 621 Bytes
一键复制 编辑 原始数据 按行查看 历史
Outliwer 提交于 7年前 . 剑指offer部分

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

  • 注意二分搜索的返回值
public class Solution {
    public int GetNumberOfK(int [] array , int k) {
       return findTheWay(array,k+0.5) - findTheWay(array,k-0.5);
    }
    
    private int findTheWay(int [] array , double k){
        int start = 0;
        int end = array.length - 1;
        int index = 0;
        while (start <= end){
            index = (start + end) / 2;
            if (array[index] > k){
                end = index - 1;
            } else {
                start = index + 1;
            }
        }
        return start;
    }
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/taotechip/code-for-interview.git
git@gitee.com:taotechip/code-for-interview.git
taotechip
code-for-interview
CodeForInterview
master

搜索帮助