京东笔试编程题代码



第一题,刚看到第一题的时候没有思路,直接去做了第二题,第二题昨晚只剩不到半个小时,
虽然有了一点思路,但是没写完,现在又花了一个小时才把自己的思路实现,用题目中给的
用例测试是对的,可惜没办法验证是否AC
package jingDong;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Main {
public static void main(String[] args) {
Scanner in = null;
try {
in = new Scanner(System.in);
int T = in.nextInt();
for (int t = 0; t < T; t++) {
int N = in.nextInt(), M = in.nextInt();
Map<Integer, Set<Integer>> map = new HashMap<>();
for (int j = 0; j < M; j++) {
int val1 = in.nextInt(), val2 = in.nextInt();
if (!map.containsKey(val1)) {
map.put(val1, new HashSet<>());
}
map.get(val1).add(val2);
if (!map.containsKey(val2)) {
map.put(val2, new HashSet<>());
}
map.get(val2).add(val1);
}
int[] groupArr = new int[N];
Set<Integer> grouperSet = new HashSet<>();
for (int i = 0; i < N; i++) {
grouperSet.clear();
if (groupArr[i] == 0) {
grouperSet.add(i);
for (int j = i + 1; j < N; j++) {
if (groupArr[j] == 0) {
boolean flag1 = map.get(i + 1).containsAll(map.get(j + 1));
boolean flag2 = map.get(j + 1).containsAll(map.get(i + 1));
if (flag1 && flag2) {
grouperSet.add(j);
}
}
}
if (grouperSet.size() + map.get(i + 1).size() == N) {
for (Integer node : grouperSet) {
groupArr[node] = grouperSet.size();
}
}
}
}
for (int i = 0; i < groupArr.length; i++) {
if (groupArr[i] == 0) {
System.out.println("false");
return;
}
}
System.out.println("true");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
}
}


第二题
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main2 {
public static void main(String[] args) {
Scanner in = null;
try {
in = new Scanner(System.in);
int n = in.nextInt(), count = 0;
;
List<Long> list1 = new ArrayList<>(), list2 = new ArrayList<>(), list3 = new ArrayList<>();
int[] counted = new int[n];
for (int i = 0; i < n; i++) {
long temp1 = in.nextLong(), temp2 = in.nextLong(), temp3 = in.nextLong();
if (list1.size() > 0) {
for (int j = 0; j < list1.size(); j++) {
if (counted[j] == 0 && list1.get(j) < temp1 && list2.get(j) < temp2 && list3.get(j) < temp3) {
count++;
counted[j] = 1;
}
if (list1.get(j) > temp1 && list2.get(j) > temp2 && list3.get(j) > temp3) {
count++;
counted[i] = 1;
break;
}
}
}
list1.add(temp1);
list2.add(temp2);
list3.add(temp3);
}
System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
}
}


#京东##笔试题目#
全部评论
/* JD 多部图 AC 100  找一个节点,和它不相连的节点都划到一个集合里面,然后验证一下这个集合和剩下的节点之间是否满足要求,如果满足,在考虑剩下的节点,先选一个出来,不相连的划分到一个集合中...循环操作直到所有的节点都划分完就可以了 */ import java.util.*; public class JD1 {     public static class Node{         public int value;         public ArrayList<Node> nexts;         public boolean pass;         public Node(int value){             this.value = value;             nexts = new ArrayList<>();             pass = false;         }     }     public static void process(Scanner in){         int n = in.nextInt();         int m = in.nextInt();         HashMap<Integer, Node> map = new HashMap<>();         for(int i = 0; i < n; i++){             map.put(i+1, new Node(i+1));         }         for(int i = 0; i < m; i++){             int f = in.nextInt();             int s = in.nextInt();             Node nf = map.get(f);             Node ns = map.get(s);             nf.nexts.add(ns);             ns.nexts.add(nf);         }         Node n1 = map.get(1);         map.remove(1);         List<Node> xl = n1.nexts;         List<Node> nxl = new ArrayList<>();         for (Map.Entry<Integer, Node> entry : map.entrySet()) {             if(!xl.contains(entry.getValue())){                 nxl.add(entry.getValue());             }         }         boolean pan = false;         for(Node node : nxl){             for(Node node1: xl){                 if(!node.nexts.contains(node1)){                     pan = true;                     break;                 }             }         }         if(pan){             System.out.println("No");         }else{             System.out.println("Yes");         }     }     public static void main(String[] args){         Scanner in = new Scanner(System.in);         int n = in.nextInt();         for(int i = 0; i < n; i++){             process(in);         }     } }
点赞 回复 分享
发布于 2018-09-10 07:07
第一题不用这么复杂,直接两个for循环就可以了。过一道可以进面试吗?
点赞 回复 分享
发布于 2018-09-09 23:22

相关推荐

选钝角的小学生很热爱...:佬,今天收到的嘛?我三面结束二十天了,没人联系😅。请问你base哪里啊
点赞 评论 收藏
分享
真tmd的恶心,1.面试开始先说我讲简历讲得不好,要怎样讲怎样讲,先讲背景,再讲技术,然后再讲提升多少多少,一顿说教。2.接着讲项目,我先把背景讲完,开始讲重点,面试官立即打断说讲一下重点,无语。3.接着聊到了项目的对比学习的正样本采样,说我正样本采样是错的,我解释了十几分钟,还是说我错的,我在上一家实习用这个方法能work,并经过市场的检验,并且是顶会论文的复现,再怎么不对也不可能是错的。4.面试官,说都没说面试结束就退出会议,把面试者晾在会议里面,丝毫不尊重面试者难受的点:1.一开始是讲得不好是欣然接受的,毕竟是学习。2.我按照面试官的要求,先讲背景,再讲技术。当我讲完背景再讲技术的时候(甚至已经开始蹦出了几个技术名词),凭什么打断我说讲重点,是不能听出人家重点开始了?这也能理解,每个人都有犯错,我也没放心上。3.我自己做过的项目,我了解得肯定比他多,他这样贬低我做过的项目,说我的工作是错误的,作为一个技术人员,我是完全不能接受的,因此我就和他解释,但无论怎么解释都说我错。凭什么,作为面试官自己不了解相关技术,别人用这个方式work,凭什么还认为这个方法是错的,不接受面试者的解释。4.这个无可厚非,作为面试官,不打招呼就退出会议,把面试者晾着,本身就是有问题。综上所述,我现在不觉得第一第二点也是我的问题,面试官有很大的问题,就是专门恶心人的,总结面试官说教,不尊重面试者,打击面试者,不接受好的面试者,技术一般的守旧固执分子。有这种人部门有这种人怎么发展啊。最后去查了一下,岗位关闭了。也有可能是招到人了来恶心人的,但是也很cs
牛客20646354...:招黑奴啊,算法工程师一天200?
点赞 评论 收藏
分享
评论
点赞
4
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务