Lintcode 846. 多关键字排序 C++

多关键字排序给定 n 个学生的学号(从 1 到 n 编号)以及他们的考试成绩,表示为(学号,考试成绩),请将这些学生按考试成绩降序排序,若考试成绩相同,则按学号升序排序。样例1输入: array = [[2,50],[1,50],[3,100]]输出: [[3,100],[1,50],[2,50]]样例2输入: array = [[2,50],[1,50],[3,50]]输...
摘要由CSDN通过智能技术生成

846. 多关键字排序

给定 n 个学生的学号(从 1 到 n 编号)以及他们的考试成绩,表示为(学号,考试成绩),请将这些学生按考试成绩降序排序,若考试成绩相同,则按学号升序排序。

样例

样例1
输入: array = [[2,50],[1,50],[3,100]]
输出: [[3,100],[1,50],[2,50]]

样例2
输入: array = [[2,50],[1,50],[3,50]]
输出: [[1,50],[2,50],[3,50]]

这个是一个很典型的关键字排序题
用sort函数可以方便解决
只要自己编写一个比较函数即可
代码如下

class Solution {
   
public:
    /**
     * @param array: the input array
     * @return: the sorted array
     */
     
    static bool 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Python中,我们可以通过自定义类来实现多关键字排序。 首先,我们可以在自定义类的构造函数中设置需要排序关键字。假设我们需要按照学生的年龄(age)和成绩(score)两个关键字进行排序,我们可以这样定义一个Student类: ```python class Student: def __init__(self, name, age, score): self.name = name self.age = age self.score = score ``` 接下来,我们可以使用内置的`sorted()`函数来进行排序。为了实现多关键字排序,我们可以使用`key`参数传入一个函数,该函数可以根据我们的需求来定义排序的规则。 例如,如果我们想要按照年龄升序、成绩降序的方式对学生列表进行排序,可以定义一个排序函数: ```python def sort_key(student): return (student.age, -student.score) ``` 接下来,我们可以使用`sorted()`函数进行排序: ```python students = [ Student("Tom", 20, 80), Student("Alice", 18, 90), Student("Bob", 20, 70) ] sorted_students = sorted(students, key=sort_key) ``` 执行后,`sorted_students`列表将按照我们定义的排序规则进行排序。 在这个例子中,`sort_key`函数返回一个元组,元组中的第一个元素是年龄,第二个元素是成绩的负值。这样可以保证年龄相同时,按照成绩的降序进行排序。 以上便是使用自定义类实现多关键字排序的方法。通过定义排序函数,我们可以根据需要对多个关键字进行排序操作。 ### 回答2: 在Python中,我们可以使用多关键字排序来对自定义类进行排序。 首先,我们需要定义一个自定义类,该类可以包含多个属性(关键字)。假设我们的自定义类为Person,具有属性name、age和salary。 我们可以通过重写类的特殊方法__lt__(小于)、__eq__(等于)和__gt__(大于)来实现对类对象的排序。这些方法分别表示对象之间的小于、等于和大于的比较操作。在这些方法中,我们可以根据需要指定对象之间的比较条件。 以下是一个简单的示例代码: ```python class Person: def __init__(self, name, age, salary): self.name = name self.age = age self.salary = salary def __lt__(self, other): if self.name != other.name: return self.name < other.name if self.age != other.age: return self.age < other.age return self.salary < other.salary def __eq__(self, other): return self.name == other.name and self.age == other.age and self.salary == other.salary def __gt__(self, other): return not self.__lt__(other) and not self.__eq__(other) # 创建Person对象的列表 persons = [Person("张三", 25, 5000), Person("李四", 25, 6000), Person("王五", 30, 5000)] # 根据多个关键字进行排序 persons.sort() # 输出排序结果 for person in persons: print(person.name, person.age, person.salary) ``` 以上代码创建了一个Person类,其中定义了__lt__、__eq__和__gt__方法,用于实现对象之间的比较操作。然后,我们创建了一个Person对象的列表,并使用sort方法对其进行排序。最后,我们按照指定的关键字顺序输出排序结果。 这样,就可以使用多关键字对自定义类进行排序了。 ### 回答3: 在自定义类中,可以通过重写比较方法来实现多关键字排序。 首先,需要定义一个包含多个属性的类,每个属性都代表了一个关键字。然后,在该类中重写比较方法,使其按照指定的排序顺序进行比较。 例如,假设我们要定义一个Person类,包含姓名(name)、年龄(age)和身高(height)三个属性。我们可以按照姓名升序、年龄降序、身高降序的顺序来排序。 首先,我们定义Person类: ```java public class Person { private String name; private int age; private double height; // 构造方法、getter和setter方法省略 // 重写比较方法 public int compareTo(Person other) { // 按照姓名升序排序 int result = this.name.compareTo(other.name); if (result != 0) { return result; } // 按照年龄降序排序 result = other.age - this.age; if (result != 0) { return result; } // 按照身高降序排序 return (int) (other.height - this.height); } } ``` 接着,我们可以使用该自定义类进行排序。例如,创建一个Person对象的列表,然后使用Collections.sort()方法进行排序: ```java List<Person> personList = new ArrayList<>(); personList.add(new Person("Tom", 25, 180)); personList.add(new Person("Jerry", 30, 170)); personList.add(new Person("Alice", 20, 160)); Collections.sort(personList); for (Person person : personList) { System.out.println(person.getName()); } ``` 以上代码会按照姓名升序、年龄降序、身高降序的顺序输出人员的姓名: ``` Alice Jerry Tom ``` 这样,我们就通过重写比较方法,在自定义类中实现了多关键字排序

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值