京东笔试大家觉得怎么样

感觉编程题有点难?是我想复杂了吗。。
有没有大佬指点一下
一个逃生问题,一个匹配子序列问题
#京东##笔试题目##实习##春招#
全部评论
逃生并查集,字符串kmp加区间贪心
点赞 回复 分享
发布于 2019-04-14 00:49
import java.util.*; public class Two { static class line{ int from; int to; boolean use=true; public line(int from, int to) { this.from = from; this.to = to; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n= scanner.nextInt(); scanner.nextLine(); String[] arr=new String[n]; for (int i = 0; i <n ; i++) { arr[i]=scanner.nextLine(); } String T= scanner.nextLine(); List<line>list=new ArrayList<>(); for (int i = 0; i <n ; i++) { int index=T.indexOf(arr[i],0); while (index!=-1) { list.add(new line(index,index+arr[i].length())); index=T.indexOf(arr[i],index+1); } } Collections.sort(list, new Comparator<line>() { @Override public int compare(line o1, line o2) { return o1.to>o2.to?1:-1; } }); int num=0; for (int i = 0; i < list.size(); i++) { if (!list.get(i).use)continue; num++; for (int j = i+1; j <list.size() ; j++) { if (list.get(j).from<list.get(i).to)list.get(j).use=false; } } System.out.println(num); } }
点赞 回复 分享
发布于 2019-04-13 21:59
逃生问题,建一棵多叉树,分别求根节点的所有子数的节点数,其中节点数最大值就是答案
点赞 回复 分享
发布于 2019-04-13 21:21
64 82凉了
点赞 回复 分享
发布于 2019-04-13 23:32
import java.util.*; public class One { static class TreeNode { int val; TreeNode left; TreeNode right; public TreeNode(int x) { val = x; } public void TreeNodeNext(TreeNode a){ if(left==null)left=a; else right=a; } } static int getNodeNum(TreeNode root) { if (root == null) { return 0; } return getNodeNum(root.left) + getNodeNum(root.right) + 1; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n= scanner.nextInt(); HashMap<Integer,TreeNode>hashMap=new HashMap<>(); for (int i = 0; i <n-1 ; i++) { int a= scanner.nextInt(); int b= scanner.nextInt(); if(!hashMap.containsKey(a))hashMap.put(a,new TreeNode(a)); if(!hashMap.containsKey(b))hashMap.put(b,new TreeNode(b)); hashMap.get(b).TreeNodeNext(hashMap.get(a));//我这默认的是例如3-2 2是父节点 3是子节点 题目给的测试用例是这样,可能其他的例子不是 } TreeNode root=hashMap.get(1); int sumleft=0,sumright=0; if (root.left!=null) sumleft=getNodeNum(root.left); if (root.right!=null) sumright=getNodeNum(root.right); System.out.println(Math.max(sumleft,sumright)); } }
点赞 回复 分享
发布于 2019-04-13 21:58
作者:Ms.lin 链接:https://www.nowcoder.com/discuss/177870 来源:牛客网 逃生问题: 思路:根结点左右子树的最大结点数。本地测试通过,提交不通过,不知道哪错了,大佬们帮忙看看 import collections import sys n = int(input()) edges = [] while True:     a = []     s = input()     if s != "":         for x in s.split():             a.append(int(x))         edges.append(a)     else:         break graph = collections.defaultdict(set) for u, v in edges:     graph[u].add(v)     graph[v].add(u) def dfs(graph, node, visited):     count = 1     visited[node] = True     for child in graph[node]:         if not visited[child]:             count += dfs(graph, child, visited)     return count res = 0 visited = [False] * (n + 1) visited[1] = True for child in graph[1]:     nodeNums = dfs(graph, child, visited)     if res < nodeNums:         res = nodeNums sys.stdout.write(str(res)+"\n")
点赞 回复 分享
发布于 2019-04-13 21:48
逃生的代码 AC #include<iostream> #include<unordered_map> #include<algorithm> using namespace std; struct treeNode{     int value;     unordered_map<int, treeNode*> map;     treeNode(int value){         this->value = value;     } }; int getNum(treeNode* nodes[], int i){     // cout << nodes[1]->value;     int size = nodes[i]->map.size();     int result = 0;     unordered_map<int, treeNode*>::iterator ite = nodes[i]->map.begin();     if(ite == nodes[i]->map.end()){         // cout << "***"<<endl;         return 1;     }     if(i == 1){         while(ite != nodes[i]->map.end()){             int help = ite->second->value;             result  = max(result,getNum(nodes, help));             ite++;         }         return result;     }else{         while(ite != nodes[i]->map.end()){             int help = ite->second->value;         // result  = max(result,getNum(nodes, help));             result += getNum(nodes,help);             ite++;         }     }          // cout << size;     return result+1; } int main(){     int n;     cin >> n;     treeNode* nodes[n+1];     // 建立n个节点     for(int i=1;i<=n;i++){         nodes[i] = new treeNode(i);     }     int from,to;     //连接n 条边     for(int i=0;i<n-1;i++){         cin >> to >>from;         nodes[from]->map[to] = nodes[to];     }     cout << getNum(nodes,1);     return 0; }
点赞 回复 分享
发布于 2019-04-13 21:38
东哥开始找兄弟了。
点赞 回复 分享
发布于 2019-04-13 21:30
第一题左右子树的最大节点数,第二题最多不相交线断数贪心法(区间可以用暴力indexof)
点赞 回复 分享
发布于 2019-04-13 21:28
我不配跟强哥做兄弟😭
点赞 回复 分享
发布于 2019-04-13 21:23
求代码。。。用的什么数据结构啊
点赞 回复 分享
发布于 2019-04-13 21:23
我也觉得有点难,写完没法ac又找不到错误
点赞 回复 分享
发布于 2019-04-13 21:20

相关推荐

敢逐云霄志:你打招呼语怎么能这么长,hr都没看下去的欲望,简明扼要说重点,就读于某某学校某某专业,26届应届毕业生,学信网可查,先后在某某公司实习过(如有),然后做过什么项目,想找一份什么样的工作,可实习几个月以上,期待您的回复。
点赞 评论 收藏
分享
评论
点赞
13
分享

创作者周榜

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