在排序数组中查找数字

本文介绍了一种改进的二分查找方法,用于在已排序的数组中统计特定数字出现的次数。通过查找数字的第一个和最后一个位置,可以快速计算出该数字在数组中的出现频率。例如,在数组{1, 2, 3, 3, 3, 3, 4, 5}中,数字3出现了4次。" 104896089,5774469,使用VisualSVN Server与TortoiseSVN搭建与管理SVN,"['版本控制', 'SVN', 'Visual SVN Server', 'Tortoise SVN']
摘要由CSDN通过智能技术生成

题目一:数字在排序数组中出现的次数。
统计一个数字在排序数组中出现的次数。例如,输入排序
数组{1,2,3,3,3,3,4,5}和数字3,由于3在这个数
组中出现了4次,因此输出4。
设计一个改进的二分查找法,
分别查找数组中第一个该数字和最后一个该数字,
找到之后;利用索引计算中间该数字的个数;

package helen.c;

public class ArrayFind {
    void test(String  testName, int data[],  int k, int expected){//{3, 3, 3, 3, 4, 5};
        if(testName != null)
            System.out.printf("%s begins: ", testName);

        int result = arrayFind(data, k);
        if(result == expected)
            System.out.printf("Passed.\n");
        else
            System.out.printf("Failed.\n");
    }

    // 查找的数字出现在数组的中间
    void test1(){
        int data[] = {1, 2, 3, 3, 3, 3, 4, 5};
        test("Test1", data, 3, 4);
    }

    // 查找的数组出现在数组的开头
    void test2(){
        int data[] = {3, 3, 3, 3, 4, 5};
        test
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值