lintcode-medium-Nuts & Bolts Problem
2016-04-03 13:18
232 查看
Givenasetofnnutsofdifferentsizesandnboltsofdifferentsizes.Thereisaone-onemappingbetweennutsandbolts.Comparisonofanuttoanothernutorabolttoanotherboltisnotallowed.Itmeansnutcanonlybecomparedwithboltandboltcanonlybecomparedwithnuttoseewhichoneisbigger/smaller.
Wewillgiveyouacomparefunctiontocomparenutwithbolt.
Example
Givennuts=
Yourcodeshouldfindthematchingboltsandnuts.
oneofthepossiblereturn:
nuts=
wewilltellyouthematchcomparefunction.Ifwegiveyouanothercomparefunction.
thepossiblereturnisthefollowing:
nuts=
Soyoumustusethecomparefunctionthatwegivetodothesorting.
Wewillgiveyouacomparefunctiontocomparenutwithbolt.
Example
Givennuts=
['ab','bc','dd','gg'],bolts=
['AB','GG','DD','BC'].
Yourcodeshouldfindthematchingboltsandnuts.
oneofthepossiblereturn:
nuts=
['ab','bc','dd','gg'],bolts=
['AB','BC','DD','GG'].
wewilltellyouthematchcomparefunction.Ifwegiveyouanothercomparefunction.
thepossiblereturnisthefollowing:
nuts=
['ab','bc','dd','gg'],bolts=
['BC','AA','DD','GG'].
Soyoumustusethecomparefunctionthatwegivetodothesorting.
Theorderofthenutsorboltsdoesnotmatter.Youjustneedtofindthematchingboltforeachnut.
/**
*publicclassNBCompare{
*publicintcmp(Stringa,Stringb);
*}
*Youcanusecompare.cmp(a,b)tocomparenuts"a"andbolts"b",
*if"a"isbiggerthan"b",itwillreturn1,elseiftheyareequal,
*itwillreturn0,elseif"a"issmallerthan"b",itwillreturn-1.
*When"a"isnotanutor"b"isnotabolt,itwillreturn2,whichisnotvalid.
*/
publicclassSolution{
/**
*@paramnuts:anarrayofintegers
*@parambolts:anarrayofintegers
*@paramcompare:ainstanceofComparator
*@return:nothing
*/
publicvoidsortNutsAndBolts(String[]nuts,String[]bolts,NBComparatorcompare){
//writeyourcodehere
if(nuts==null||nuts.length==0||bolts==null||bolts.length==0||nuts.length!=bolts.length)
return;
sort(nuts,bolts,0,nuts.length-1,compare);
return;
}
publicvoidsort(String[]nuts,String[]bolts,intstart,intend,NBComparatorcompare){
if(start>=end)
return;
intpartition=partition(nuts,bolts[end],start,end,compare);
partition(bolts,nuts[partition],start,end,compare);
sort(nuts,bolts,start,partition-1,compare);
sort(nuts,bolts,partition+1,end,compare);
return;
}
publicintpartition(String[]array,Stringpivot,intstart,intend,NBComparatorcompare){
if(start>=end)
returnstart;
intleft=start;
intright=end;
Stringpivot_mirror=null;
for(inti=start;i<=end;i++)
if(compare.cmp(array[i],pivot)==0||compare.cmp(pivot,array[i])==0){
pivot_mirror=array[i];
break;
}
while(true){
while(left<right&&((compare.cmp(array[left],pivot)==-1)||(compare.cmp(pivot,array[left])==1)))
left++;
while(left<right&&((compare.cmp(array[right],pivot)==1)||compare.cmp(pivot,array[right])==-1))
right--;
if(left==right)
break;
swap(array,left,right);
}
array[left]=pivot_mirror;
returnleft;
}
publicvoidswap(String[]array,inti,intj){
Stringtemp=array[i];
array[i]=array[j];
array[j]=temp;
return;
}
};
相关文章推荐
- 使用secureCRT连接VMware-Ubuntukylin虚拟机
- require.js的使用的坑!
- 包贝尔婚礼事件中,有哪些嘈可以吐?
- tar.xz文件压缩和解压
- html <a>标签属性总结
- 数据仓库和数据集市的区别
- MFC-WM_CTLCOLOR消息绘制控件颜色
- 8. String to Integer (atop)【E】【67】【leetcode】
- UVa 872 - Ordering 输出全拓扑排序
- 磁盘阵列RAID
- wordpress保留数据重装并制作自己的模板
- goto gamedev blog
- 世界上最伟大的推销员
- SuferfaceView --- View的孪生兄弟
- js--函数--定义函数的方法
- MD5加密
- goto gamedev blog
- POJ2395 Out of Hay(最小生成树)
- 递归中关于递归语句后面内容的执行
- 高雄访谈