百度面试题:求绝对值最小的数

有一个已经排序的数组(升序),数组中可能有正数、负数或0,求数组中元素的绝对值最小的数,要求,不能用顺序比较的方法(复杂度需要小于O(n)),可以使用任何语言实现

例如,数组{-20,-13,-4, 6, 77,200} ,绝对值最小的是-4。


算法实现的基本思路

找到负数和正数的分界点,如果正好是0就是它了,如果是正数,再和左面相邻的负数绝对值比较,如果是负数,取取绝对值与右面正数比较。还要考虑数组只有正数或负数的情况。


我根据这个思路用Java简单实现了一个算法。大家有更好的实现方法欢迎跟帖

[java]  view plain copy
  1. public class MinAbsoluteValue   
  2. {  
  3.     private static int getMinAbsoluteValue(int[] source)  
  4.     {  
  5.           
  6.         int index = 0;  
  7.         int result = 0;    
  8.         int startIndex = 0;  
  9.         int endIndex = source.length - 1;  
  10.         //  计算负数和正数的分界点  
  11.         while(true)  
  12.         {  
  13.             index = startIndex + (endIndex - startIndex) / 2;  
  14.             result = source[index];  
  15.             if(result==0)  
  16.             {  
  17.                 return 0;  
  18.             }  
  19.             else if(result > 0)  
  20.             {  
  21.                 if(index == 0)  
  22.                 {  
  23.                     break;  
  24.                 }  
  25.                 if(source[index-1] >0)  
  26.                     endIndex = index - 1;  
  27.                 else if(source[index-1] ==0)  
  28.                     return 0;  
  29.                 else  
  30.                     break;  
  31.             }  
  32.             else  
  33.             {  
  34.                 if(index == endIndex)  
  35.                     break;  
  36.                 if(source[index + 1] < 0)  
  37.                     startIndex = index + 1;  
  38.                 else if(source[index + 1] == 0)  
  39.                     return 0;  
  40.                 else   
  41.                     break;  
  42.             }  
  43.         }  
  44.         //  根据分界点计算绝对值最小的数  
  45.         if(source[index] > 0)  
  46.         {  
  47.             if(index == 0 || source[index] < Math.abs(source[index-1]))  
  48.                 result= source[index];  
  49.             else  
  50.                 result = source[index-1];  
  51.         }  
  52.         else  
  53.         {  
  54.             if(index == source.length - 1 || Math.abs(source[index]) < source[index+1])  
  55.                 result= source[index];  
  56.             else  
  57.                 result = source[index+1];  
  58.         }  
  59.           
  60.           
  61.         return result;  
  62.     }  
  63.     public static void main(String[] args) throws Exception  
  64.     {  
  65.           
  66.         int[] arr1 = new int[]{-23,-22,-3,-2,1,2,3,5,20,120};  
  67.         int[] arr2 = new int[]{-23,-22,-12,-6,-4};  
  68.         int[] arr3 = new int[]{1,22,33,55,66,333};  
  69.         int value = getMinAbsoluteValue(arr1);  
  70.         System.out.println(value);  
  71.         value = getMinAbsoluteValue(arr2);  
  72.         System.out.println(value);  
  73.         value = getMinAbsoluteValue(arr3);  
  74.         System.out.println(value);  
  75.     }  
  76. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值