b**********f 发帖数: 136 | 1 Java 小白,实在不知道怎么做。求大侠解答!下周就要去面试了,不是Java
developer的职位,但是会考到一点编程。
public class Person {
Person father;
Person mother;
Gender gender;
Integer age;
List children;
int level = 0;
public enum Gender {
Male,
Female;
}
}
For the above class, you basically have to implement 2 methods.
public List getOldestSisters()
(person can have multiple parents since remarrying )
public List getGreatestAncestors()
Find the oldest sisters of the given Person object. Such class recursively
defines a Person father, Person mother, and a List of children |
W***o 发帖数: 6519 | 2 小朋友,自己要先try try,上来就这么直接问让人写code会被骂的。
不过,恍惚记得大学cs系的女僧貌似都是让其男友写code做作业的 |
o*******r 发帖数: 73 | 3 同是java小白,简单写了一下,供参考。希望楼主是萌妹纸。。嘿嘿。
预祝面试成功。
public List getOldestSisters() {
List ret = new LinkedList<>();
PriorityQueue pq = new PriorityQueue(1, (a, b) -> a.
age - b.age);
Set visitedParents = new HashSet<>();
Queue queue = new LinkedList<>();
if (father != null) queue.offer(father);
if (mother != null) queue.offer(mother);
while (!queue.isEmpty()) {
Person p = queue.poll();
visitedParents.add(p);
if (p.children != null) {
for (Person kid : p.children) {
if (kid.father != null && !visitedParents.contains(kid.
father)) queue.offer(kid.father);
if (kid.mother != null && !visitedParents.contains(kid.
mother)) queue.offer(kid.mother);
if (kid != this && kid.gender == Gender.Female) {
if (kid.age != null) {
if (pq.isEmpty()) pq.add(kid);
else {
while (!pq.isEmpty() && pq.peek().age < kid.
age) pq.poll();
pq.offer(kid);
}
}
}
}
}
}
while (!pq.isEmpty()) {
ret.add(pq.poll());
}
return ret;
} |
S********t 发帖数: 3431 | 4 你这是当伦理题做呢
【在 o*******r 的大作中提到】 : 同是java小白,简单写了一下,供参考。希望楼主是萌妹纸。。嘿嘿。 : 预祝面试成功。 : public List getOldestSisters() { : List ret = new LinkedList<>(); : PriorityQueue pq = new PriorityQueue(1, (a, b) -> a. : age - b.age); : Set visitedParents = new HashSet<>(); : Queue queue = new LinkedList<>(); : if (father != null) queue.offer(father); : if (mother != null) queue.offer(mother);
|
b**********f 发帖数: 136 | 5 大侠请赐教,真的很着急。
几乎知道此题必考。
工作中不需要写java code,实在是做不出来。 |
b**********f 发帖数: 136 | 6 大侠请赐教,真的很着急。
几乎知道此题必考。
工作中不需要写java code,实在是做不出来。 |
b**********f 发帖数: 136 | 7 大侠请赐教,真的很着急。
几乎知道此题必考。
工作中不需要写java code,实在是做不出来。 |
w*****w 发帖数: 53 | 8 是女生吗?是女生的话,给我看看Linkedin,我当面手把手给你写。 |
j*r 发帖数: 23 | 9 public class Person {
Person father;
Person mother;
Gender gender;
Integer age;
List children;
int level = 0;
public enum Gender {
Male,
Female;
}
}
public List getOldestSisters() {
Person parent = father;
if (parent == null) {
parent = mother;
}
if (parent == null) {
return null;
}
Person oldestSister = null;
List siblings = parent.children;
if (siblings != null) {
for(Person sibling: siblings) {
if (sibling.gender == Female) {
if (oldSister == null || sibling.age > oldestSister.age)
{
oldSister = sibling;
}
}
}
}
return oldestSister;
}
【在 b**********f 的大作中提到】 : 大侠请赐教,真的很着急。 : 几乎知道此题必考。 : 工作中不需要写java code,实在是做不出来。
|
p*********g 发帖数: 2998 | 10 直接做查询的真是傻,
要我就直接加一个参数MaxHeap<>sister()
然后方法里直接
return heap.DeleteMax();
直接完事
【在 j*r 的大作中提到】 : public class Person { : Person father; : Person mother; : Gender gender; : Integer age; : List children; : int level = 0; : public enum Gender { : Male, : Female;
|