查找和目标数最接近的或者相等的数

本文介绍了一种查找数组中最接近指定值的算法实现,并提供了两种不同的方法:使用二分搜索和通过差值映射排序来找到最接近的目标值。此外,还展示了如何求取数组中的最大值。

此文转自博客http://blog.csdn.net/huoyunshen88/article/details/8567970, 自己学习标记下,感谢博主。

/**
 * 查找出最接近目标值的数,并返回。
 * @param array
 * @param targetNum
 * @return
 */

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static int binarysearchKey(Object[] array, int targetNum) {  
  2.   
  3.     //Object[] array = temp.clone();  
  4.     Arrays.sort(array);  
  5.     for (int i = 0; i < array.length; i++) {  
  6.         System.out.println(array[i]);  
  7.     }  
  8.     int targetindex = 0;  
  9.     int left = 0,right = 0;   
  10.     for (right = array.length-1; left!=right;) {  
  11.     int midIndex = (right + left)/2;  
  12.     int mid = (right - left);  
  13.     int midValue = (Integer) array[midIndex];  
  14.     if (targetNum == midValue) {  
  15.         return midIndex;  
  16.     }  
  17.   
  18.        if(targetNum > midValue){  
  19.                left = midIndex;  
  20.            }else{  
  21.                right = midIndex;  
  22.            }  
  23.          
  24.        if (mid <=1) {  
  25.            break;  
  26.         }  
  27.     }  
  28.     int rightnum = ((Integer) array[right]).intValue();  
  29.     int leftnum = ((Integer) array[left]).intValue();  
  30.     int ret =  Math.abs((rightnum - leftnum)/2) > Math.abs(rightnum -targetNum) ? rightnum : leftnum;  
  31.     System.out.println("和要查找的数:"+targetNum+ "最接近的数:" + ret);  
  32.     return ret;  
  33.     }  

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static void main(String[] args) {  
  2.     ArrayList array = new ArrayList();  
  3.     array.add(26);  
  4.     array.add(76);  
  5.     array.add(46);  
  6.     array.add(76);  
  7.     array.add(54);  
  8.     array.add(94);  
  9.     array.add(24);  
  10.     array.add(34);  
  11.       
  12.     int targetNum74;  
  13.     System.out.println("和要查找的数:"+targetNum+ "最接近的数:"+binarysearchKey(array.toArray(), targetNum));  
  14.       
  15. }  



/**
 * 遍历数组和要查找的数值做差,以差:数组索引存为map,然后对以差为key的数组排序,拿到差最小的索引。
 * @param array
 * @param num
 * @return
 */

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static Integer searchKey(int[] array,int num) {    
  2.            
  3.        int[] temp = new int[array.length];  
  4.        Map<Integer,Integer> map = new TreeMap<Integer,Integer>();  
  5.        int disnum = 0;  
  6.        for (int i = 0; i < array.length; i++) {  
  7.         disnum = Math.abs(array[i] - num);  
  8. temp[i]=disnum;  
  9. map.put(disnum, i);  
  10. }  
  11.        System.out.println("数组和的差值和数组元素的差值相减的绝对值和位置的映射结果:" + map);  
  12.        Arrays.sort(temp);  
  13.        Integer index = map.get(temp[0]);    
  14.        System.out.println("数组和的差值和数组元素的差值相减的绝对值最小值:"+temp[0]+"最小值的key:" +  array[index]);  
  15.        return  (Integer) array[index];    
  16.    }  



/**
* 求一个数组的最大值
* @param array1
*/

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static void max1(int[] array1) {  
  2. int max = 0;  
  3. for (int i : array1) {  
  4. max = Math.max(max, i);  
  5. }  
  6. System.out.println("数组最大值是:"+max);  
  7. }  



/**
* 求一个数组的最大值
* @param array1
*/
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static void max2(int[] array1) {  
  2. int max = 0;  
  3. for (int i = 0; i < array1.length; i++) {  
  4. max = max > array1[i] ? max : array1[i];  
  5. }  
  6. System.out.println("数组最大值是:"+max);  
  7. }  


/**
* 求一个数组的最大值
* @param array1
*/
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static void max3(int[] array1) {  
  2. Arrays.sort(array1);  
  3. System.out.println("数组最大值是:"+array1[array1.length-1]);  
  4. }  
//计算最小值
    private BigDecimal calMinValue(BigDecimal[] arrays){
        BigDecimal temp = new BigDecimal("0");
        for(int i=arrays.length;--i>=0;){
            for(int j=0;j<i;j++){
                if(arrays[j].compareTo(arrays[j+1])==1)
                {
                    temp=arrays[j];
                    arrays[j]=arrays[j+1];
                    arrays[j+1]=temp;
                }
            }
        }
        for(int n=0;n<arrays.length;n++){
            temp = arrays[0];
            break;
        }
        return temp;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值