Leetcode 406. Queue Reconstruction by Height 排队重构 解题报告
2016-09-25 21:46
441 查看
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; } }
相关文章推荐
- Leetcode解题报告:406. Queue Reconstruction by Height
- 【LeetCode】406.Queue Reconstruction by Height(Medium)解题报告
- 【LeetCode】Queue Reconstruction by Height 解题报告
- LeetCode 406. Queue Reconstruction by Height
- Leetcode 406. Queue Reconstruction by Height
- LeetCode406. Queue Reconstruction by Height Add to List
- leetcode刷题之旅——406. Queue Reconstruction by Height
- [LeetCode]406. Queue Reconstruction by Height
- [LeetCode]406.Queue Reconstruction by Height
- [leetcode] 406. Queue Reconstruction by Height
- LeetCode 406. Queue Reconstruction by Height
- [leetcode]406. Queue Reconstruction by Height
- LeetCode 406. Queue Reconstruction by Height
- leetcode_middle_2_406. Queue Reconstruction by Height
- [Leetcode] 406. Queue Reconstruction by Height
- LeetCode406. Queue Reconstruction by Height
- Leetcode-406. Queue Reconstruction by Height
- LeetCode406. Queue Reconstruction by Height
- [LeetCode] 406. Queue Reconstruction by Height
- leetcode 406. Queue Reconstruction by Height