??xml version="1.0" encoding="utf-8" standalone="yes"?>BlogJava-李宁的极客世?/title>http://www.dentisthealthcenter.com/nokiaguy/zh-cnThu, 07 Dec 2023 16:32:34 GMTThu, 07 Dec 2023 16:32:34 GMT60- Twitter法面试题详解(Java实现Q?/title>http://www.dentisthealthcenter.com/nokiaguy/archive/2013/11/03/405944.html银河使?/dc:creator>银河使?/author>Sun, 03 Nov 2013 10:03:00 GMThttp://www.dentisthealthcenter.com/nokiaguy/archive/2013/11/03/405944.htmlhttp://www.dentisthealthcenter.com/nokiaguy/comments/405944.htmlhttp://www.dentisthealthcenter.com/nokiaguy/archive/2013/11/03/405944.html#Feedback4http://www.dentisthealthcenter.com/nokiaguy/comments/commentRss/405944.htmlhttp://www.dentisthealthcenter.com/nokiaguy/services/trackbacks/405944.html阅读全文

]]> - 癑ֺ面试题:求绝对值最的?/title>http://www.dentisthealthcenter.com/nokiaguy/archive/2013/01/30/394920.html银河使?/dc:creator>银河使?/author>Wed, 30 Jan 2013 03:45:00 GMThttp://www.dentisthealthcenter.com/nokiaguy/archive/2013/01/30/394920.htmlhttp://www.dentisthealthcenter.com/nokiaguy/comments/394920.htmlhttp://www.dentisthealthcenter.com/nokiaguy/archive/2013/01/30/394920.html#Feedback10http://www.dentisthealthcenter.com/nokiaguy/comments/commentRss/394920.htmlhttp://www.dentisthealthcenter.com/nokiaguy/services/trackbacks/394920.html 有一个已l排序的数组Q升序)Q数l中可能有正数、负数或0Q求数组中元素的l对值最的敎ͼ要求Q不能用序比较的方法(复杂度需要小于OQnQ)Q可以用Q何语a实现
例如Q数l{-20Q?13Q?4, 6, 77,200} Q绝对值最的?4?/p>
法实现的基本思\
扑ֈ负数和正数的分界点,如果正好?是它了Q如果是正数Q再和左面相ȝ负数l对值比较,如果是负敎ͼ取取l对g右面正数比较。还要考虑数组只有正数或负数的情况?/p>
我根据这个思\用Java单实C一个算法?span style="color: #ff0000;">大家有更好的实现ҎƢ迎跟帖
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)
{ // 计算当前的烦?/span>
index = startIndex + (endIndex - startIndex) / 2;
result = source[index];<br> // 如果{于0Q就直接q回了,0肯定是绝对值最的
if(result==0)
{
return 0;
} // 如果值大?Q处理当前位|左侧区域,因ؓ负数肯定在左?/span>
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;
} // 如果于0Q处理当前位|右侧的区域Q因为正数肯定在右侧的位|?/span>
else
{
if(index == endIndex)
break;
if(source[index + 1] < 0)
startIndex = index + 1;
else if(source[index + 1] == 0)
return 0;
else
break;
}
}
// Ҏ分界点计绝对值最的?/span>
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[index+1])
result= source[index];
else
result = source[index+1];
}
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);
}
}

]]>- Android深度探烦Q卷1Q:安装C/C++交叉~译环境http://www.dentisthealthcenter.com/nokiaguy/archive/2013/01/28/394850.html银河使?/dc:creator>银河使?/author>Mon, 28 Jan 2013 15:28:00 GMThttp://www.dentisthealthcenter.com/nokiaguy/archive/2013/01/28/394850.htmlhttp://www.dentisthealthcenter.com/nokiaguy/comments/394850.htmlhttp://www.dentisthealthcenter.com/nokiaguy/archive/2013/01/28/394850.html#Feedback0http://www.dentisthealthcenter.com/nokiaguy/comments/commentRss/394850.htmlhttp://www.dentisthealthcenter.com/nokiaguy/services/trackbacks/394850.html阅读全文

]]> - Android开发(1Q:随机l制彩色实心?/title>http://www.dentisthealthcenter.com/nokiaguy/archive/2011/09/06/358115.html银河使?/dc:creator>银河使?/author>Tue, 06 Sep 2011 08:06:00 GMThttp://www.dentisthealthcenter.com/nokiaguy/archive/2011/09/06/358115.htmlhttp://www.dentisthealthcenter.com/nokiaguy/comments/358115.htmlhttp://www.dentisthealthcenter.com/nokiaguy/archive/2011/09/06/358115.html#Feedback2http://www.dentisthealthcenter.com/nokiaguy/comments/commentRss/358115.htmlhttp://www.dentisthealthcenter.com/nokiaguy/services/trackbacks/358115.html阅读全文

]]> - 赶紧升到Android 2.3.4Q体验最新的Android技?http://www.dentisthealthcenter.com/nokiaguy/archive/2011/04/29/349299.html银河使?/dc:creator>银河使?/author>Fri, 29 Apr 2011 14:20:00 GMThttp://www.dentisthealthcenter.com/nokiaguy/archive/2011/04/29/349299.htmlhttp://www.dentisthealthcenter.com/nokiaguy/comments/349299.htmlhttp://www.dentisthealthcenter.com/nokiaguy/archive/2011/04/29/349299.html#Feedback5http://www.dentisthealthcenter.com/nokiaguy/comments/commentRss/349299.htmlhttp://www.dentisthealthcenter.com/nokiaguy/services/trackbacks/349299.html阅读全文

]]> - 十个值得开发者投入的Ud应用创意http://www.dentisthealthcenter.com/nokiaguy/archive/2010/12/06/339933.html银河使?/dc:creator>银河使?/author>Mon, 06 Dec 2010 13:41:00 GMThttp://www.dentisthealthcenter.com/nokiaguy/archive/2010/12/06/339933.htmlhttp://www.dentisthealthcenter.com/nokiaguy/comments/339933.htmlhttp://www.dentisthealthcenter.com/nokiaguy/archive/2010/12/06/339933.html#Feedback3http://www.dentisthealthcenter.com/nokiaguy/comments/commentRss/339933.htmlhttp://www.dentisthealthcenter.com/nokiaguy/services/trackbacks/339933.html阅读全文

]]> - 乐博Android客户端(新浪微博Q?.01发布Q欢q各位童鞋试?/title>http://www.dentisthealthcenter.com/nokiaguy/archive/2010/11/17/338236.html银河使?/dc:creator>银河使?/author>Wed, 17 Nov 2010 03:17:00 GMThttp://www.dentisthealthcenter.com/nokiaguy/archive/2010/11/17/338236.htmlhttp://www.dentisthealthcenter.com/nokiaguy/comments/338236.htmlhttp://www.dentisthealthcenter.com/nokiaguy/archive/2010/11/17/338236.html#Feedback7http://www.dentisthealthcenter.com/nokiaguy/comments/commentRss/338236.htmlhttp://www.dentisthealthcenter.com/nokiaguy/services/trackbacks/338236.html 阅读全文

]]> - 关于Android中传递数据的一些讨?/title>http://www.dentisthealthcenter.com/nokiaguy/archive/2010/11/09/337636.html银河使?/dc:creator>银河使?/author>Tue, 09 Nov 2010 08:25:00 GMThttp://www.dentisthealthcenter.com/nokiaguy/archive/2010/11/09/337636.htmlhttp://www.dentisthealthcenter.com/nokiaguy/comments/337636.htmlhttp://www.dentisthealthcenter.com/nokiaguy/archive/2010/11/09/337636.html#Feedback1http://www.dentisthealthcenter.com/nokiaguy/comments/commentRss/337636.htmlhttp://www.dentisthealthcenter.com/nokiaguy/services/trackbacks/337636.html阅读全文

]]> - 张朝阻I腾讯抄袭和垄断做到极致http://www.dentisthealthcenter.com/nokiaguy/archive/2010/11/04/337201.html银河使?/dc:creator>银河使?/author>Thu, 04 Nov 2010 02:24:00 GMThttp://www.dentisthealthcenter.com/nokiaguy/archive/2010/11/04/337201.htmlhttp://www.dentisthealthcenter.com/nokiaguy/comments/337201.htmlhttp://www.dentisthealthcenter.com/nokiaguy/archive/2010/11/04/337201.html#Feedback0http://www.dentisthealthcenter.com/nokiaguy/comments/commentRss/337201.htmlhttp://www.dentisthealthcenter.com/nokiaguy/services/trackbacks/337201.html阅读全文

]]> - 《Android/OPhone 开发完全讲义》连载(10Q:Sqlite数据?/title>http://www.dentisthealthcenter.com/nokiaguy/archive/2010/10/28/336413.html银河使?/dc:creator>银河使?/author>Thu, 28 Oct 2010 14:16:00 GMThttp://www.dentisthealthcenter.com/nokiaguy/archive/2010/10/28/336413.htmlhttp://www.dentisthealthcenter.com/nokiaguy/comments/336413.htmlhttp://www.dentisthealthcenter.com/nokiaguy/archive/2010/10/28/336413.html#Feedback0http://www.dentisthealthcenter.com/nokiaguy/comments/commentRss/336413.htmlhttp://www.dentisthealthcenter.com/nokiaguy/services/trackbacks/336413.html阅读全文

]]> 久久一级片