码迷,mamicode.com
首页 > 其他好文 > 详细

网易 2016 实习研发工程师 3道 编程题-1

时间:2016-08-02 22:27:16      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:records   网易   工程师   信息   矛盾   

小明陪小红去看钻石,他们从一堆钻石中随机抽取两颗并比较她们的重量。这些钻石的重量各不相同。在他们们比较了一段时间后,它们看中了两颗钻石g1和g2。现在请你根据之前比较的信息判断这两颗钻石的哪颗更重。

给定两颗钻石的编号g1,g2,编号从1开始,同时给定关系数组vector,其中元素为一些二元组,第一个元素为一次比较中较重的钻石的编号,第二个元素为较轻的钻石的编号。最后给定之前的比较次数n。请返回这两颗钻石的关系,若g1更重返回1,g2更重返回-1,无法判断返回0。输入数据保证合法,不会有矛盾情况出现。

测试样例:

2,3,[[1,2],[2,4],[1,3],[4,3]],4
返回: 1
import java.util.*;

public class Cmp {
    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
        int[][] records = { { 1, 2 }, { 2, 4 }, { 1, 3 }, { 4, 3 } };  
        System.out.println(cmp(2, 3, records, 4));  
    }  
    
    public static int cmp(int g1, int g2, int[][] records, int n) {
        // write code here
        HashSet<Integer> max = new HashSet<>();  
        HashSet<Integer> min = new HashSet<>();  
        for (int i = 0; i < n; i++) {  
            if (records[i][0] == g1) {  
                min.add(records[i][1]);  
            }  
            if (records[i][1] == g1) {  
                max.add(records[i][0]);  
            }  
        }  
  
        for (int i = 0; i < n; i++) {  
            if (records[i][0] != g1 && records[i][1] != g1) {  
                if (max.contains(records[i][1])) {  
                    max.add(records[i][0]);  
                }  
                if (min.contains(records[i][0])) {  
                    min.add(records[i][1]);  
                }  
            }  
        }  
  
        if (max.contains(g2) && !min.contains(g2)) {  
            return -1;  
        } else if (!max.contains(g2) && min.contains(g2)) {  
            return 1;  
        } else {  
            return 0;  
    }
   }
}


网易 2016 实习研发工程师 3道 编程题-1

标签:records   网易   工程师   信息   矛盾   

原文地址:http://codeli.blog.51cto.com/4264850/1833660

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!