avatar
Java面试题求解# JobHunting - 待字闺中
b*f
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
avatar
W*o
2
小朋友,自己要先try try,上来就这么直接问让人写code会被骂的。
不过,恍惚记得大学cs系的女僧貌似都是让其男友写code做作业的
avatar
o*r
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;
}
avatar
S*t
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);

avatar
b*f
5
大侠请赐教,真的很着急。
几乎知道此题必考。
工作中不需要写java code,实在是做不出来。
avatar
b*f
6
大侠请赐教,真的很着急。
几乎知道此题必考。
工作中不需要写java code,实在是做不出来。
avatar
b*f
7
大侠请赐教,真的很着急。
几乎知道此题必考。
工作中不需要写java code,实在是做不出来。
avatar
w*w
8
是女生吗?是女生的话,给我看看Linkedin,我当面手把手给你写。
avatar
j*r
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,实在是做不出来。

avatar
p*g
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;

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。