注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
http://www.lintcode.com/en/problem/nuts-bolts-problem/#
基本思路很简单,由于compare规定只能按(nut, bolt)的顺序放, 先从nut里选一个做pivot,按此pivot与bolts array进行比较并且进行partition; 而后返回pivot element的index,代表对应原先选择的nut的bolt,并且以此元素回到nuts array里面进行partition; 最后递归处理左右两边。
但是这题在第六个test case总挂,测试了好多遍也不清楚为什么,向论坛里各位请教下,谢谢!- /**
- * 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) {
- /*
- if(nuts[0].equals("ab")){
- for(String nut : nuts){
- for(String bolt : bolts){
- if(compare.cmp(nut, bolt) == 0)
- System.out.println(nut + " matches " + bolt);
- }
- }
- }
- */
- sortSubArray(nuts, bolts, 0, nuts.length - 1, compare);
- }
-
- private void sortSubArray(String[] nuts, String[] bolts, int start, int end, NBComparator compare){
- if(start >= end) return;
-
- int pivot = sortBolts(nuts[start], bolts, start, end, compare);
-
- sortNuts(nuts, bolts[pivot], start, end, compare);
-
- sortSubArray(nuts, bolts, start, pivot - 1, compare);
- sortSubArray(nuts, bolts, pivot + 1, end, compare);
-
- return;
- }
-
- private int sortBolts(String nut, String[] bolts, int start, int end, NBComparator compare){
-
- int left = start;
- int right = end;
-
- while(left < right){
- while(left < right && compare.cmp(nut, bolts[left]) < 0){
- left++;
- }
- while(left < right && compare.cmp(nut, bolts[right]) > 0){
- right--;
- }
-
- if(left < right) swap(bolts, left, right);
- }
-
- /*
- System.out.println("Current Bolts: ");
- for(String bolt : bolts){
- System.out.print(bolt + " ");
- }
- for(String bolt : bolts){
- System.out.print(compare.cmp(nut, bolt) + " ");
- }
- System.out.println("Given nut: " + nut);
- System.out.println("Pivot index: " + left);
- */
- for(int i = start; i <= end; i++ ){
- if(compare.cmp(nut, bolts[i]) == 0) left = i;
- }
- return left;
- }
-
- private void sortNuts(String[] nuts, String bolt, int start, int end, NBComparator compare){
- int left = start;
- int right = end;
-
- while(left < right){
- while(left < right && compare.cmp(nuts[left], bolt) > 0){
- left ++;
- }
- while(left < right && compare.cmp(nuts[right], bolt) < 0){
- right --;
- }
- if(left < right) swap(nuts, left, right);
- }
-
- /*
- System.out.println("Current Bolts: ");
- for(String nut : nuts){
- System.out.print(nut + " ");
- }
- for(String nut : nuts){
- System.out.print(compare.cmp(nut, bolt) + " ");
- }
- System.out.println("Given bolt: " + bolt);
- System.out.println("Pivot index: " + left);
- */
-
-
- return;
- }
-
- private void swap(String[] array, int a, int b){
- String temp = array[a];
- array[a] = array[b];
- array[b] = temp;
- }
- };
复制代码 |