赞
踩
不想戳的看下图:
样例:
数据范围:
解题方法1:
代码如下(java):
class Solution { // 排序规则,按照x的坐标升序排列,若x坐标相同,右端点在前 class IntCompator implements Comparator<int[]> { @Override public int compare(int[] o1, int[] o2) { // TODO Auto-generated method stub if (o1[0] == o2[0]) return o2[1] - o1[1]; return o1[0] - o2[0]; } } public List<List<Integer>> getSkyline(int[][] buildings) { List<List<Integer>> list = new ArrayList<>(); int newbuild[][] = new int[2 * buildings.length][2]; // 将建筑物坐标化成[右端点,高度],[左端点,-高度],用高度的正负来判断是左端点还是右端点。 for (int i = 0; i < buildings.length; i++) { newbuild[2 * i][0] = buildings[i][0]; newbuild[2 * i][1] = buildings[i][2]; newbuild[2 * i + 1][0] = buildings[i][1]; newbuild[2 * i + 1][1] = -buildings[i][2]; } Arrays.sort(
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。