中级农民
- 积分
- 155
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-4-17
- 最后登录
- 1970-1-1
|
我以前看到讨论说是用list就行,不过我觉得用list的话,时间复杂度还是O(n^2)啊- public class HeightSort {
- private static class Person {
- String name;
- int height;
- int tallCount;
- Person(int h, int t, String n) {
- name = n;
- height = h;
- tallCount = t;
- }
- @Override
- public String toString() {
- return "<" + name + "," + height + "," + tallCount + ">";
- }
- }
- static class HeightComparator implements Comparator<Person> {
- public int compare(Person p1, Person p2) {
- int ret = p2.height - p1.height;
- return (ret == 0) ? p2.tallCount - p1.tallCount : ret;
- }
- }
- public static List<Person> orderByHeightAndTaller(Person[] people) {
- Arrays.sort(people, new HeightComparator());
- LinkedList<Person> ret = new LinkedList<>();
- for (Person p : people) {
- ret.add(p.tallCount, p);
- }
- return ret;
- }
- public static void main(String[] args) {
- Person[] people = { new Person(6, 2, "A"), new Person(1, 4, "B"),
- new Person(11, 0, "C"), new Person(5, 1, "D"),
- new Person(5, 3, "K"), new Person(10, 0, "E"),
- new Person(4, 0, "F")
- };
- System.out.println(orderByHeightAndTaller(people));
- }
- }
复制代码 |
|