nuts 和 bolts 的问题
相关题目 Expand
8%
通过
给定一组 n 个不同大小的 nuts 和 n 个不同大小的 bolts。nuts 和 bolts 一一匹配。 不允许将 nut 之间互相比较,也不允许将 bolt 之间互相比较。也就是说,只许将 nut 与 bolt 进行比较, 或将 bolt 与 nut 进行比较。请比较 nut 与 bolt 的大小。
您在真实的面试中是否遇到过这个题?
Yes
哪家公司问你的这个题?
Airbnb Alibaba Amazon Apple Baidu Bloomberg Cisco Dropbox Ebay Facebook Google Hulu Intel Linkedin Microsoft NetEase Nvidia Oracle Pinterest Snapchat Tencent Twitter Uber Xiaomi Yahoo Yelp Zenefits
感谢您的反馈
样例
标签
Expand
Nuts 用一个整数数组表示 nuts [] = {1, 5, 8, 2}. Bolts 也用一个整数数组表示 bolts[] = {3, 6, 7, 9}. 我们将提供一个比较函数,以比较 nut 与 bolt 的大小。 将 nuts 进行升序排序,使得 nuts 与 bolts 位置对等。
相关题目 Expand
-
由题意可知,只需要nuts找到与之相对的bolts即可,不能用自己的对比函数,用题目所给的对比。ExampleGiven nuts = ['ab','bc','dd','gg'], bolts = ['AB','GG', 'DD', 'BC'].
Your code should find the matching bolts and nuts.
one of the possible return:
nuts = ['ab','bc','dd','gg'], bolts = ['AB','BC','DD','GG'].
we will tell you the match compare function.
If we give you another compare function.
the possible return is the following:
nuts = ['ab','bc','dd','gg'], bolts = ['BC','AA','DD','GG'].
So you must use the compare function that we give to do the sorting.
The order of the nuts or bolts does not matter.
You just need to find the matching bolt for each nut.
方法1:直接O(n^2)遍历,以nuts为基础,找到相对应的botls,交换。/** * public class NBCompare { * public int cmp(String a, String b); * } * You can use compare.cmp(a, b) to compare nuts "a" and bolts "b", * if "a" is bigger than "b", it will return 1, else if they are equal, * it will return 0, else if "a" is smaller than "b", it will return -1. * When "a" is not a nut or "b" is not a bolt, it will return 2, which is not valid. */ public class Solution { /** * @param nuts: an array of integers * @param bolts: an array of integers * @param compare: a instance of Comparator * @return: nothing */ public void sortNutsAndBolts(String[] nuts, String[] bolts, NBComparator compare) { // write your code here for(int i=0;i<nuts.length;i++){ for(int j=i;j<bolts.length;j++){ if(compare.cmp(nuts[i], bolts[j])==0){ String tmp = bolts[i]; bolts[i] = bolts[j]; bolts[j] = tmp; break; } } } } };
方法2:时间复杂度范围O(nlogn)---O(N^2)还可以用快排的思想,将nuts和bolts都排好序,这样相当于直接是相匹配的。nuts相当于螺母,bolts相当于螺帽步骤如下:1.拿一个螺帽
2. 把剩下的螺母分为2堆,一堆比第一步螺帽大,一堆比第一步螺帽小
3. 找出与第一步相对的螺母
4.把剩下的螺帽分为2堆,一堆比第3步的螺母大,一堆比第3步的螺母小/** * public class NBCompare { * public int cmp(String a, String b); * } * You can use compare.cmp(a, b) to compare nuts "a" and bolts "b", * if "a" is bigger than "b", it will return 1, else if they are equal, * it will return 0, else if "a" is smaller than "b", it will return -1. * When "a" is not a nut or "b" is not a bolt, it will return 2, which is not valid. */ public class Solution { /** * @param nuts: an array of integers * @param bolts: an array of integers * @param compare: a instance of Comparator * @return: nothing */ public void sortNutsAndBolts(String[] nuts, String[] bolts, NBComparator compare) { // write your code here if(null==nuts||0==nuts.length) return; if(nuts.length!=bolts.length) return; sortNB(nuts, bolts, compare, 0, nuts.length-1); } public void sortNB(String[] nuts,String[] bolts,NBComparator compare,int s,int e){ if(s>=e) return; int index = partion(nuts, bolts[0], compare, s, e); partion(bolts, nuts[index], compare, s, e); sortNB(nuts, bolts, compare, s, index-1); sortNB(nuts, bolts, compare, index+1, e); } public int partion(String[] strs,String pivot,NBComparator compare,int s,int e){ int m = s; for(int i=s+1;i<=e;i++){ if(compare.cmp(strs[i], pivot)==-1||compare.cmp( pivot,strs[i])==1){ m++; swap(strs, i, m); }else if(compare.cmp(strs[i],pivot)==0){ swap(strs, i, s); i--; } } swap(strs, m, s); return m; } private void swap(String[] str, int l, int r) { String temp = str[l]; str[l] = str[r]; str[r] = temp; } };