今日头条--笔试
来源:互联网 发布:淘宝退款1%手续费退吗 编辑:程序博客网 时间:2024/04/28 23:11
package test2;import java.util.Arrays;import java.util.Comparator;import java.util.LinkedList;/** * p为给定的二维平面整数点集。定义P中某店X,如果x满足p中 * 任意点都不在x的右上方区域内(横纵坐标都大于x),则称其中 * 为“最大的”。求出所有“最大的”点的集合。(所有点的横坐标和纵坐标都不重复,坐标轴范围在[0,1e9)内) */public class Problem_01_RightCorners { public static class Node { public int x; public int y; public Node(int x, int y) { this.x = x; this.y = y; } } public static class MyComparator implements Comparator<Node> { @Override public int compare(Node o1, Node o2) { if (o1.x != o2.x) { return o1.x - o2.x; } else { return o2.y - o1.y; } } } public static LinkedList<Node> getRightCornerNodes1(int[] x, int[] y) { int size = x.length; LinkedList<Node> res = new LinkedList<>(); //定义一个node数组 Node[] nodes = new Node[size]; for (int i = 0; i < size; i++) { nodes[i] = new Node(x[i], y[i]); //放入所有的点 } Arrays.sort(nodes, new MyComparator()); for (int i = 0; i < size; i++) { boolean insert = true; for (int j = 0; j < size; j++) { if ((nodes[i].x < nodes[j].x) && (nodes[i].y < nodes[j].y)) { //这种方法比较麻烦! insert = false; } } if (insert) { res.add(nodes[i]); } } return res; } public static LinkedList<Node> getRightCornerNodes2(int[] x, int[] y) { int size = x.length; LinkedList<Node> res = new LinkedList<Node>(); Node[] nodes = new Node[size]; for (int i = 0; i < size; i++) { nodes[i] = new Node(x[i], y[i]); } Arrays.sort(nodes, new MyComparator()); res.add(nodes[size - 1]); int rightMaxY = nodes[size - 1].y; for (int i = size - 2; i >= 0; i--) { //从右往左遍历 如果当前Y是最大的Y则 右上方无最大的点,并且排序的时候已经说过X小往大排序,Y从大到小排序。那么X相等的时候,那么比较的Y一定是X点最小的Y; if (nodes[i].y >= rightMaxY) { res.addFirst(nodes[i]); } rightMaxY = Math.max(rightMaxY, nodes[i].y); } return res; } public static int[] generateRandomArray(int size) { int[] arr = new int[size]; for (int i = 0; i < size; i++) { arr[i] = (int) (Math.random() * 1000); } return arr; } public static boolean isEqual(LinkedList<Node> list1, LinkedList<Node> list2) { if (list1.size() != list2.size()) { return false; } while (!list1.isEmpty()) { Node node1 = list1.pollFirst(); Node node2 = list2.pollFirst(); if (node1.x != node2.x || node1.y != node2.y) { return false; } } return true; } public static void printArray(int[] x, int[] y) { for (int i = 0; i < x.length; i++) { System.out.print("(" + x[i] + "," + y[i] + ") "); } System.out.println(); } public static void printLinkedList(LinkedList<Node> list) { for (Node node : list) { System.out.print("(" + node.x + "," + node.y + ") "); } System.out.println(); } public static void main(String[] args) { int testTime = 3000000; for (int i = 0; i < testTime; i++) { int size = 3; int[] x = generateRandomArray(size); int[] y = generateRandomArray(size); LinkedList<Node> res1 = getRightCornerNodes1(x, y); LinkedList<Node> res2 = getRightCornerNodes2(x, y); if (!isEqual(res1, res2)) { printArray(x, y); printLinkedList(res1); printLinkedList(res2); break; } } }}
阅读全文
0 0
- 今日头条笔试
- 今日头条--笔试
- 今日头条笔试
- 今日头条笔试
- 今日头条笔试题
- 今日头条,笔试题
- 今日头条笔试题
- 今日头条笔试题
- 今日头条笔试题
- 今日头条笔试题
- [笔试练习]今日头条实习笔试
- 牛客网今日头条笔试详解
- 今日头条 笔试题 (位运算)
- 牛客《今日头条》笔试题
- 2016今日头条后端笔试题
- 异或今日头条笔试
- 今日头条笔试题 数列
- 今日头条2017实习生笔试题解
- Codeforces Round #433 (Div. 2)
- 关键词拍卖竞价机制,排序与计费规则
- Effective Java —— 考虑用静态工厂方法代替构造器
- 我的Ruby学习历程之初识变量
- 网络包几种格式struct ethhdr、ether_header、iphdr、tcphdr、udphdr
- 今日头条--笔试
- 基于微软案例数据挖掘之Microsoft 聚类分析算法
- 一个 11 行 Python 代码实现的神经网络
- 阿里云优惠券幸运券领取
- 阿婆私房菜项目知识总结
- [概率论与数理统计]随机事件及其概率运算
- linux中初学nodeJs遇到的一些问题
- 通过IP来查找对应城市信息(Kotlin+redis实现)
- linux 简单强大的命令