Leetcode 406. Queue Reconstruction by Height 排队重构 解题报告

1 解题思想

这道题的是让你重新排列一个序列。
总的来说,他给了你很多个打乱的

2 原题

Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h. Write an algorithm to reconstruct the queue. 
Note:
The number of people is less than 1,100. 
Example 
Input:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]

Output:
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

3 AC解

public class Solution {
    private void swap(int[][] people,int a,int b){
        int t1=people[a][0],t2=people[a][1];
        people[a][0] = people[b][0];
        people[a][1] = people[b][1];
        people[b][0] = t1;
        people[b][1] = t2;

    }
    public int[][] reconstructQueue(int[][] people) {
        //java 排序不方便,我这里就直接暴力排序了
        //让身高按照降序排列,高的在前面,同身高的情况下让要求前面人数人少的在前面,就可以了
        int n = people.length;
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                if(people[i][0] < people[j][0])
                    swap(people,i,j);
                else if(people[i][0] == people[j][0] && people[i][1] > people[j][1])
                    swap(people,i,j);
            }
        }
        //按照顺序插入
        List<Integer> la = new ArrayList<Integer>();
        List<Integer> lb = new ArrayList<Integer>();
        for(int i=0;i<n;i++){
            la.add(people[i][1],people[i][0]);
            lb.add(people[i][1],people[i][1]);
        }
        for(int i=0;i<n;i++){
            people[i][0]=la.get(i);
            people[i][1]=lb.get(i);
        }
        return people;

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值