blogjava-凯发k8网页登录

blogjava-凯发k8网页登录http://www.blogjava.net/nokiaguy/zh-cnsat, 08 apr 2023 20:35:06 gmtsat, 08 apr 2023 20:35:06 gmt60twitter算法面试题详解(java实现)http://www.blogjava.net/nokiaguy/archive/2013/11/03/405944.html银河使者银河使者sun, 03 nov 2013 10:03:00 gmthttp://www.blogjava.net/nokiaguy/archive/2013/11/03/405944.htmlhttp://www.blogjava.net/nokiaguy/comments/405944.htmlhttp://www.blogjava.net/nokiaguy/archive/2013/11/03/405944.html#feedback4http://www.blogjava.net/nokiaguy/comments/commentrss/405944.htmlhttp://www.blogjava.net/nokiaguy/services/trackbacks/405944.html阅读全文

银河使者 2013-11-03 18:03 发表评论
]]>
百度面试题:求绝对值最小的数http://www.blogjava.net/nokiaguy/archive/2013/01/30/394920.html银河使者银河使者wed, 30 jan 2013 03:45:00 gmthttp://www.blogjava.net/nokiaguy/archive/2013/01/30/394920.htmlhttp://www.blogjava.net/nokiaguy/comments/394920.htmlhttp://www.blogjava.net/nokiaguy/archive/2013/01/30/394920.html#feedback10http://www.blogjava.net/nokiaguy/comments/commentrss/394920.htmlhttp://www.blogjava.net/nokiaguy/services/trackbacks/394920.html    有一个已经排序的数组(升序),数组中可能有正数、负数或0,求数组中元素的绝对值最小的数,要求,不能用顺序比较的方法(复杂度需要小于o(n)),可以使用任何语言实现

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

 

    算法实现的基本思路

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

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

public class minabsolutevalue
{
    
private static int getminabsolutevalue(int[] source)
    {
         
        
int index = 0;
        
int result = 0
        
int startindex = 0;
        
int endindex = source.length - 1;
        
//  计算负数和正数分界点
        while(true)
        {
                //  计算当前的索引
            index = startindex  (endindex - startindex) / 2;
            result 
= source[index];<br>                //  如果等于0,就直接返回了,0肯定是绝对值最小的
            if(result==0)
            {
                
return 0;
            }
               //  如果值大于0,处理当前位置左侧区域,因为负数肯定在左侧
            else if(result > 0)
            {
                
if(index == 0)
                {
                    
break;
                }
                
if(source[index-1>0)
                    endindex 
= index - 1;
                
else if(source[index-1==0)
                    
return 0;
                
else
                    
break;
            }
            //  如果小于0,处理当前位置右侧的区域,因为正数肯定在右侧的位置
            else
            {
                
if(index == endindex)
                    
break;
                
if(source[index  1< 0)
                    startindex 
= index  1;
                
else if(source[index  1== 0)
                    
return 0;
                
else
                    
break;
            }
        }
        
//  根据分界点计算绝对值最小的数
        if(source[index] > 0)
        {
            
if(index == 0 || source[index] < math.abs(source[index-1]))
                result
= source[index];
            
else
                result 
= source[index-1];
        }
        
else
        {
            
if(index == source.length - 1 || math.abs(source[index]) < source[index1])
                result
= source[index];
            
else
                result 
= source[index1];
        }
         
         
        
return result;
    }
    
public static void main(string[] args) throws exception
    {
         
        
int[] arr1 = new int[]{-23,-22,-3,-2,1,2,3,5,20,120};
        
int[] arr2 = new int[]{-23,-22,-12,-6,-4};
        
int[] arr3 = new int[]{1,22,33,55,66,333};
        
int value = getminabsolutevalue(arr1);
        system.out.println(value);
        value 
= getminabsolutevalue(arr2);
        system.out.println(value);
        value 
= getminabsolutevalue(arr3);
        system.out.println(value);
    }
}


银河使者 2013-01-30 11:45 发表评论
]]>
android深度探索(卷1):安装c/c 交叉编译环境http://www.blogjava.net/nokiaguy/archive/2013/01/28/394850.html银河使者银河使者mon, 28 jan 2013 15:28:00 gmthttp://www.blogjava.net/nokiaguy/archive/2013/01/28/394850.htmlhttp://www.blogjava.net/nokiaguy/comments/394850.htmlhttp://www.blogjava.net/nokiaguy/archive/2013/01/28/394850.html#feedback0http://www.blogjava.net/nokiaguy/comments/commentrss/394850.htmlhttp://www.blogjava.net/nokiaguy/services/trackbacks/394850.html阅读全文

银河使者 2013-01-28 23:28 发表评论
]]>
android开发(1):随机绘制彩色实心圆http://www.blogjava.net/nokiaguy/archive/2011/09/06/358115.html银河使者银河使者tue, 06 sep 2011 08:06:00 gmthttp://www.blogjava.net/nokiaguy/archive/2011/09/06/358115.htmlhttp://www.blogjava.net/nokiaguy/comments/358115.htmlhttp://www.blogjava.net/nokiaguy/archive/2011/09/06/358115.html#feedback2http://www.blogjava.net/nokiaguy/comments/commentrss/358115.htmlhttp://www.blogjava.net/nokiaguy/services/trackbacks/358115.html阅读全文

银河使者 2011-09-06 16:06 发表评论
]]>
赶紧升级到android 2.3.4,体验最新的android技术 http://www.blogjava.net/nokiaguy/archive/2011/04/29/349299.html银河使者银河使者fri, 29 apr 2011 14:20:00 gmthttp://www.blogjava.net/nokiaguy/archive/2011/04/29/349299.htmlhttp://www.blogjava.net/nokiaguy/comments/349299.htmlhttp://www.blogjava.net/nokiaguy/archive/2011/04/29/349299.html#feedback5http://www.blogjava.net/nokiaguy/comments/commentrss/349299.htmlhttp://www.blogjava.net/nokiaguy/services/trackbacks/349299.html阅读全文

银河使者 2011-04-29 22:20 发表评论
]]>
十个值得开发者投入的移动应用创意http://www.blogjava.net/nokiaguy/archive/2010/12/06/339933.html银河使者银河使者mon, 06 dec 2010 13:41:00 gmthttp://www.blogjava.net/nokiaguy/archive/2010/12/06/339933.htmlhttp://www.blogjava.net/nokiaguy/comments/339933.htmlhttp://www.blogjava.net/nokiaguy/archive/2010/12/06/339933.html#feedback3http://www.blogjava.net/nokiaguy/comments/commentrss/339933.htmlhttp://www.blogjava.net/nokiaguy/services/trackbacks/339933.html阅读全文

银河使者 2010-12-06 21:41 发表评论
]]>
乐博android客户端(新浪微博)1.01发布,欢迎各位童鞋试用http://www.blogjava.net/nokiaguy/archive/2010/11/17/338236.html银河使者银河使者wed, 17 nov 2010 03:17:00 gmthttp://www.blogjava.net/nokiaguy/archive/2010/11/17/338236.htmlhttp://www.blogjava.net/nokiaguy/comments/338236.htmlhttp://www.blogjava.net/nokiaguy/archive/2010/11/17/338236.html#feedback7http://www.blogjava.net/nokiaguy/comments/commentrss/338236.htmlhttp://www.blogjava.net/nokiaguy/services/trackbacks/338236.html  阅读全文

银河使者 2010-11-17 11:17 发表评论
]]>
关于android中传递数据的一些讨论http://www.blogjava.net/nokiaguy/archive/2010/11/09/337636.html银河使者银河使者tue, 09 nov 2010 08:25:00 gmthttp://www.blogjava.net/nokiaguy/archive/2010/11/09/337636.htmlhttp://www.blogjava.net/nokiaguy/comments/337636.htmlhttp://www.blogjava.net/nokiaguy/archive/2010/11/09/337636.html#feedback1http://www.blogjava.net/nokiaguy/comments/commentrss/337636.htmlhttp://www.blogjava.net/nokiaguy/services/trackbacks/337636.html阅读全文

银河使者 2010-11-09 16:25 发表评论
]]>
张朝阳:腾讯将抄袭和垄断做到极致http://www.blogjava.net/nokiaguy/archive/2010/11/04/337201.html银河使者银河使者thu, 04 nov 2010 02:24:00 gmthttp://www.blogjava.net/nokiaguy/archive/2010/11/04/337201.htmlhttp://www.blogjava.net/nokiaguy/comments/337201.htmlhttp://www.blogjava.net/nokiaguy/archive/2010/11/04/337201.html#feedback0http://www.blogjava.net/nokiaguy/comments/commentrss/337201.htmlhttp://www.blogjava.net/nokiaguy/services/trackbacks/337201.html阅读全文

银河使者 2010-11-04 10:24 发表评论
]]>
《android/ophone 开发完全讲义》连载(10):sqlite数据库http://www.blogjava.net/nokiaguy/archive/2010/10/28/336413.html银河使者银河使者thu, 28 oct 2010 14:16:00 gmthttp://www.blogjava.net/nokiaguy/archive/2010/10/28/336413.htmlhttp://www.blogjava.net/nokiaguy/comments/336413.htmlhttp://www.blogjava.net/nokiaguy/archive/2010/10/28/336413.html#feedback0http://www.blogjava.net/nokiaguy/comments/commentrss/336413.htmlhttp://www.blogjava.net/nokiaguy/services/trackbacks/336413.html阅读全文

银河使者 2010-10-28 22:16 发表评论
]]>
网站地图