注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
本帖最后由 我一辈子赖美帝 于 2019-10-30 00:11 编辑
第二问:
{{A,B},{C,D},{B,C},{E,F},{D,E},{F,G}}
A B C D E F G middle one:D
第三题: 第二题的follow up,假设每门课程可以有多门先修课,找出所有path修到一半课程的名称(出自其他面经).--
[i][i][i][i][i][i][i][i][i][i][i][i][i][i][i][i][i].google и
.google и public static void main(String[] args) {. 1point 3acres
// TODO Auto-generated method stub
String[][] student_course_pairs_1 = {
{"58", "Software Design"},
{"58", "Linear Algebra"},
{"94", "Art History"},
{"94", "Operating Systems"},. 1point 3acres
{"17", "Software Design"},
{"58", "Mechanics"},
{"58", "Economics"}, ..
{"17", "Linear Algebra"},
{"17", "Political Science"},
{"94", "Economics"},.google и
{"25", "Economics"},
};
String[] s1={"58", "17"};. 1point3acres.com
String[] s2={"58", "94"};
String[] s3={"58", "25"};
String[] s4={"94", "25"};
String[] s5={"17", "25"};
String[] s6={"17", "94"};
List<String> res1=shareClass(student_course_pairs_1,s1);
List<String> res2=shareClass(student_course_pairs_1,s2);
List<String> res3=shareClass(student_course_pairs_1,s3); ..
List<String> res4=shareClass(student_course_pairs_1,s4);
List<String> res5=shareClass(student_course_pairs_1,s5);. 1point 3acres
List<String> res6=shareClass(student_course_pairs_1,s6);
//System.out.println(res1);
//System.out.println(res2);
//System.out.println(res3);. Waral dи,
//System.out.println(res4);
//System.out.println(res5);
//System.out.println(res6);
- static List<String> findOrder(String[][] pre) {
- HashSet<String> courseCount=new HashSet<>();
- for(String[]s :pre) {
- courseCount.add(s[0]);
- courseCount.add(s[1]);. check 1point3acres for more.
- }
- int len=courseCount.size();. ----
- System.out.println(len);
- Map<String,Integer> indegree=new HashMap<>();
- List<String> res=new ArrayList<>();.
- //把先修课放在每个课程的list中+统计入度
- Map<String,List<String>> map=new HashMap<>();.
- for(int i=0;i<pre.length;i++){
- if(map.containsKey(pre[i][1])){.1point3acres
- map.get(pre[i][1]).add(pre[i][0]);
- }else{
- List<String> list=new ArrayList<>();
- list.add(pre[i][0]);. 1point 3acres
- map.put(pre[i][1],list);
- }.--
- indegree.put(pre[i][0], indegree.getOrDefault(pre[i][0],0)+1);
- }
- //BFS. 1point3acres.com
- Queue<String> queue=new LinkedList<>();
- for(int i=0;i<pre.length;i++){. ----
- if(indegree.get(pre[i][0])==0)queue.offer(pre[i][0]);
- }
- int count=0; ..
- while(!queue.isEmpty()){
- String course=queue.poll();
- count++;
- res.add(course);
- List<String> list=map.get(course);
- if(list!=null){. 1point3acres
- int n=list.size();
- for(int i=0;i<n;i++){
- String subCourse=map.get(course).get(i);
- indegree.put(subCourse, indegree.getOrDefault(subCourse,0)-1);
- if(indegree.get(subCourse)==0)queue.offer(subCourse);
- }
- }
- }
- if(count==len)return res;. check 1point3acres for more.
- else return new ArrayList<>();
- }
- String[][] pre={{"B","A"},{"D","C"},{"C","B"},{"F","E"},{"E","D"},{"G","F"}};
- List<String> resll=findOrder(pre);
- System.out.println(resll);. 1point 3 acres
复制代码
}-baidu 1point3acres
[/i][/i][/i][/i][/i][/i][/i][/i]
.google и
[/i][/i][/i][/i][/i][/i][/i][/i][/i] |