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

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

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

算法实现的基本思路

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

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

 
 
  1. public class MinAbsoluteValue 
  2.     private static int getMinAbsoluteValue(int[] source) 
  3.     { 
  4.           
  5.         int index = 0
  6.         int result = 0;  
  7.         int startIndex = 0
  8.         int endIndex = source.length - 1
  9.         //  计算负数和正数分界点 
  10.         while(true
  11.         {              //  计算当前的索引 
  12.             index = startIndex + (endIndex - startIndex) / 2
  13.             result = source[index];<br>                //  如果等于0,就直接返回了,0肯定是绝对值最小的 
  14.             if(result==0
  15.             { 
  16.                 return 0
  17.             }              //  如果值大于0,处理当前位置左侧区域,因为负数肯定在左侧 
  18.             else if(result > 0
  19.             { 
  20.                 if(index == 0
  21.                 { 
  22.                     break
  23.                 } 
  24.                 if(source[index-1] >0
  25.                     endIndex = index - 1
  26.                 else if(source[index-1] ==0
  27.                     return 0
  28.                 else 
  29.                     break
  30.             }               //  如果小于0,处理当前位置右侧的区域,因为正数肯定在右侧的位置 
  31.             else 
  32.             { 
  33.                 if(index == endIndex) 
  34.                     break
  35.                 if(source[index + 1] < 0
  36.                     startIndex = index + 1
  37.                 else if(source[index + 1] == 0
  38.                     return 0
  39.                 else 
  40.                     break
  41.             } 
  42.         } 
  43.         //  根据分界点计算绝对值最小的数 
  44.         if(source[index] > 0
  45.         { 
  46.             if(index == 0 || source[index] < Math.abs(source[index-1])) 
  47.                 result= source[index]; 
  48.             else 
  49.                 result = source[index-1]; 
  50.         } 
  51.         else 
  52.         { 
  53.             if(index == source.length - 1 || Math.abs(source[index]) < source[index+1]) 
  54.                 result= source[index]; 
  55.             else 
  56.                 result = source[index+1]; 
  57.         } 
  58.           
  59.           
  60.         return result; 
  61.     } 
  62.     public static void main(String[] args) throws Exception 
  63.     { 
  64.           
  65.         int[] arr1 = new int[]{-23,-22,-3,-2,1,2,3,5,20,120}; 
  66.         int[] arr2 = new int[]{-23,-22,-12,-6,-4}; 
  67.         int[] arr3 = new int[]{1,22,33,55,66,333}; 
  68.         int value = getMinAbsoluteValue(arr1); 
  69.         System.out.println(value); 
  70.         value = getMinAbsoluteValue(arr2); 
  71.         System.out.println(value); 
  72.         value = getMinAbsoluteValue(arr3); 
  73.         System.out.println(value); 
  74.     } 

上面的代码分别输出1、-4和1

 

李宁的新浪微博 http://weibo.com/androidguy 欢迎关注

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值