NetEase 2016 Internship Research Engineer 3 programming Questions-1

Source: Internet
Author: User

Xiao Ming accompanied Xiao Red to see the diamonds, they from a bunch of diamonds randomly extracted two and compare their weight. The weight of these diamonds varies. After they had been comparing for some time, they had a fancy for two diamonds G1 and G2. Now, please judge which of the two diamonds is heavier according to the information you have previously compared.

Given the number of two diamonds g1,g2, the number starts at 1, given the relationship array vector, where the elements are some binary groups, the first element is the number of the heavier diamond in a comparison, and the second element is the number of the lighter diamond. The last number of comparisons before given N. Please return the relationship between the two diamonds, if G1 more return 1,g2 more return-1, can not be judged to return 0. The input data is guaranteed to be legal and there will be no contradiction.

Test examples:

2,3,[[1,2],[2,4],[1,3],[4,3]],4
Returns: 1
import java.util.*;p ublic 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));      }   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;STATIC&NBSP;INT&NBSP;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]&NBSP;==&NBSP;G1)  {                   min.add (records[i][1 ]);              }               if  (RECORDS[I][1]&NBSP;==&NBSP;G1)  {                   max.add (records[i][0]);               }    &nbSp;     }            for   (int i = 0; i < n; i++)  {               if  (records[i][0] != g1 &&  RECORDS[I][1]&NBSP;!=&NBSP;G1)  {                   if  (Max.contains (records[i][1))  {                       max.add ( Records[i][0]);                   }                   if  (Min.contains (records[i][0))  {            &nBsp;          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;      }   }} 


NetEase 2016 Internship Research Engineer 3 programming Questions-1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.