题目
题目链接:
https://www.nowcoder.com/practice/e160b104354649b69600803184094adb
思路
直接看代码,不难
Java代码
import java.util.*;public class Solution {/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param schedule int整型二维数组 * @return bool布尔型*/public boolean hostschedule (ArrayList<ArrayList<Integer>> ll) {int size = ll.size();if(size ==1) return true;int[][] schedule = new int[size][2];for(int i=0;i<size;i++){schedule[i][0] = ll.get(i).get(0);schedule[i][1] = ll.get(i).get(1);}Arrays.sort(schedule,(a,b)->{if(a[0]==b[0]){return a[1]-b[1];}else{return a[0]-b[0];}});for (int i = 1; i <schedule.length ; i++) {if(schedule[i][0]<schedule[i-1][1]) return false;}return true;}
}
Go代码
package mainimport "sort"/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可*** @param schedule int整型二维数组* @return bool布尔型*/
func hostschedule(schedule [][]int) bool {if len(schedule) == 1 {return true}sort.Slice(schedule, func(i, j int) bool {if schedule[i][0] == schedule[j][0] {return schedule[i][1] <= schedule[j][1]} else {return schedule[i][0] < schedule[j][0]}})for i := 1; i < len(schedule); i++ {if schedule[i][0] < schedule[i-1][1] {return false}}return true
}
C++代码
bool compareColumn(const std::vector<int>& a, vector<int>& b) {if (a[0] == b[0]) {return a[1] <= b[1];}return a[0] < b[0];
}
class Solution {public:/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可*** @param schedule int整型vector<vector<>>* @return bool布尔型*/bool hostschedule(vector<vector<int> >& schedule) {std::sort(schedule.begin(), schedule.end(), compareColumn);for (int i = 1; i < schedule.size(); i++) {if (schedule[i][0] < schedule[i - 1][1]) return false;}return true;}
};