注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
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] ..
. 1point 3acres
public static void main(String[] args) {
// TODO Auto-generated method stub
String[][] student_course_pairs_1 = {. Waral dи,
{"58", "Software Design"},
{"58", "Linear Algebra"},
{"94", "Art History"},
{"94", "Operating Systems"},
{"17", "Software Design"},. 1point3acres
{"58", "Mechanics"},
{"58", "Economics"},. ----
{"17", "Linear Algebra"},
{"17", "Political Science"},
{"94", "Economics"},
{"25", "Economics"},
};
String[] s1={"58", "17"};
String[] s2={"58", "94"};
String[] s3={"58", "25"};
String[] s4={"94", "25"};
String[] s5={"17", "25"};-baidu 1point3acres
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);
List<String> res6=shareClass(student_course_pairs_1,s6);
//System.out.println(res1);
//System.out.println(res2);
//System.out.println(res3);. .и
//System.out.println(res4);
//System.out.println(res5);
//System.out.println(res6);. 1point 3acres
- static List<String> findOrder(String[][] pre) {
- HashSet<String> courseCount=new HashSet<>();
- for(String[]s :pre) {
- courseCount.add(s[0]);
- courseCount.add(s[1]);
- }
- int len=courseCount.size();
- System.out.println(len);
- Map<String,Integer> indegree=new HashMap<>();
- List<String> res=new ArrayList<>();
- //把先修课放在每个课程的list中+统计入度. 1point3acres
- Map<String,List<String>> map=new HashMap<>();. Χ
- for(int i=0;i<pre.length;i++){
- if(map.containsKey(pre[i][1])){. 1point 3acres
- map.get(pre[i][1]).add(pre[i][0]);.1point3acres
- }else{. 1point 3acres
- List<String> list=new ArrayList<>();
- list.add(pre[i][0]);
- map.put(pre[i][1],list);
- }
- indegree.put(pre[i][0], indegree.getOrDefault(pre[i][0],0)+1);
- }
- . 1point 3acres
- //BFS
- 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]);. From 1point 3acres bbs
- }
- int count=0;
- while(!queue.isEmpty()){.1point3acres
- String course=queue.poll();
- count++;
- res.add(course);
- List<String> list=map.get(course);
- if(list!=null){
- int n=list.size();
- for(int i=0;i<n;i++){
- String subCourse=map.get(course).get(i);. check 1point3acres for more.
- indegree.put(subCourse, indegree.getOrDefault(subCourse,0)-1);
- if(indegree.get(subCourse)==0)queue.offer(subCourse);
- }
- }
- }
- if(count==len)return res;
- 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);. .и
复制代码
}
[/i][/i][/i][/i][/i][/i][/i][/i]
. Waral dи,
[/i][/i][/i][/i][/i][/i][/i][/i][/i] |