399.Nuts & Bolts Problem-Nuts 和 Bolts 的问题(中等题)
来源:互联网 发布:淘宝童装店铺取名 编辑:程序博客网 时间:2024/05/14 00:42
Nuts 和 Bolts 的问题
题目
给定一组 n 个不同大小的 nuts 和 n 个不同大小的 bolts。nuts 和 bolts 一一匹配。 不允许将 nut 之间互相比较,也不允许将 bolt 之间互相比较。也就是说,只许将 nut 与 bolt 进行比较, 或将 bolt 与 nut 进行比较。请比较 nut 与 bolt 的大小。
样例
给出 nuts=[‘ab’,’bc’,’dd’,’gg’], bolts=[‘AB’,’GG’, ‘DD’, ‘BC’]
你的程序应该找出bolts和nuts的匹配。
一组可能的返回结果是:
nuts=[‘ab’,’bc’,’dd’,’gg’], bolts=[‘AB’,’BC’,’DD’,’GG’]
我们将给你一个匹配的比较函数,如果我们给你另外的比较函数,可能返回的结果是:
nuts=[‘ab’,’bc’,’dd’,’gg’], bolts=[‘BC’,’AB’,’DD’,’GG’]因此的结果完全取决于比较函数,而不是字符串本身。
因为你必须使用比较函数来进行排序。
各自的排序当中nuts和bolts的顺序是无关紧要的,只要他们一一匹配就可以。题解
/** * 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 == null || bolts == null || nuts.length != bolts.length) { return; } qsort(nuts, bolts, compare, 0, nuts.length - 1); } private void qsort(String[] nuts, String[] bolts, NBComparator compare, int l, int u) { if (l >= u) { return; } int part_inx = partition(nuts, bolts[l], compare, l, u); partition(bolts, nuts[part_inx], compare, l, u); qsort(nuts, bolts, compare, l, part_inx - 1); qsort(nuts, bolts, compare, part_inx + 1, u); } private int partition(String[] str, String pivot, NBComparator compare, int l, int u) { for (int i = l; i <= u; i++) { if (compare.cmp(str[i], pivot) == 0 || compare.cmp(pivot, str[i]) == 0) { swap(str, i, l); break; } } String now = str[l]; int left = l; int right = u; while (left < right) { while (left < right && (compare.cmp(str[right], pivot) == -1 || compare.cmp(pivot, str[right]) == 1)) { right--; } str[left] = str[right]; while (left < right && (compare.cmp(str[left], pivot) == 1 || compare.cmp(pivot, str[left]) == -1)) { left++; } str[right] = str[left]; } str[left] = now; return left; } private void swap(String[] str, int l, int r) { String temp = str[l]; str[l] = str[r]; str[r] = temp; }};
Last Update 2016.11.14
0 0
- 399.Nuts & Bolts Problem-Nuts 和 Bolts 的问题(中等题)
- 题目:nuts 和 bolts 的问题
- lintcode(399)Nuts 和 Bolts 的问题
- 事务处理的nuts和bolts
- [LintCode] Nuts and Bolts
- 螺丝和螺帽Nuts and bolts 《算法》2.3.15
- Matching Nuts and Bolts 螺母螺帽的匹配
- 《算法》第二章——Nuts and bolts(螺丝和螺帽)
- uva 11138 - Nuts and Bolts(二分匹配)
- UVa11138 - Nuts and Bolts(二分匹配)
- R Programming: Part 1 - Nuts and Bolts
- Hardware Virtualization: the Nuts and Bolts / EPT / VPID
- Nuts
- Android 下的 bolts tasks
- 任务处理类Bolts的使用(可以代替AsyncTask)
- UVA 10944 Nuts for nuts.. (状态压缩dp)
- Bolts介绍
- bolts-android
- js重构与优化(一)
- Dreamweaver CC 2017 for Mac
- Flume内部原理
- OJ_1165.导弹终结版
- iOS开发中通知(Notification)快速入门及推送通知实现教程
- 399.Nuts & Bolts Problem-Nuts 和 Bolts 的问题(中等题)
- SqlServer按字母分区表
- Java实现同步的几种方式
- iOS NSNotificationCenter 使用姿势详解
- CountDownLatch的学习和图解
- OJ_1188我们的征途是星辰大海
- iOS中通知的使用NSNotificationCenter
- 【BZOJ2844】albus就是要第一个出场——线性基
- Navicat Premium for Mac