京东笔试编程题代码



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


#京东##笔试题目#
全部评论
第一题不用这么复杂,直接两个for循环就可以了。过一道可以进面试吗?
点赞 回复
分享
发布于 2018-09-09 23:22
/* 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
滴滴
校招火热招聘中
官网直投

相关推荐

点赞 评论 收藏
转发
点赞 评论 收藏
转发
点赞 4 评论
分享
牛客网
牛客企业服务