小白路过,学习了。
第一题是leetcode 271道.
public class Solution {
public String serialize(List strs) {
if (strs == null) return null;
StringBuffer ret = new StringBuffer();
for (String s : strs) ret.append(s.replace("#", "##")).append(" # ");
return ret.toString();
}
public List deserialize(String s) {
if (s == null) return null;
List ret = new ArrayList();
String[] array = s.split(" # ", -1);
for (int i = 0; i < array.length - 1; i++) ret.add(array[i].replace(
"##", "#"));
return ret;
}
}
不知道第二道题目的generator是怎么样的。感觉和generator的范围有很大的关系。我
感觉这么写可能达不到狗家的要求,抛砖引玉一下。
class RandomGenerator {
RandomGenerator(int min, int max) {
if (min > max) throw new IllegalArgumentException();
this.max = max;
this.bound = max - min + 1;
}
int nextInt() {
return random.nextInt(bound) - max;
}
private Random random = new Random();
private int max;
private int bound;
}
public class Solution {
public int timeToGenerateAll(RandomGenerator random, List list)
{
if (random == null || list == null) throw new
IllegalArgumentException();
else if (list.isEmpty()) return 0;
Set set = new HashSet<>();
set.addAll(list);
int ret = 0;
while (!set.isEmpty()) {
ret++;
set.remove(random.nextInt());
}
return ret;
}
}
第三道:rsync到底实现哪一部分?一个daemon程序?把需要sync的文件切成4K这么大的
然后编号做hash? 发现不一致只发不一致的?需要压缩。。更重要的是要有一个时间同
步?如果用时间服务器的话精度是不是够?而且因为网络的原因两个时间多少有差别的
。如果用向量时钟的话就靠谱了,这题太难。。
第四道: 我想可以考虑求两个available time slots看看有没有重叠的部分。
class Interval implements Comparable {
Interval(int s, int e) {
if (s >= e) throw new IllegalArgumentException();
this.start = s;
this.end = e;
}
int start;
int end;
public int compareTo(Object arg0) {
if (arg0 == null) throw new IllegalArgumentException();
else {
if (arg0 instanceof Interval) {
Interval inter = (Interval)arg0;
if (this.start != inter.start) return this.start - inter.
start;
else return this.end - inter.end;
} else {
throw new ClassCastException();
}
}
}
}
public class Solution {
public Interval findFirstOverlap(Interval[] intervals1, Interval[]
intervals2) {
if (intervals1 == null || intervals2 == null) throw new
IllegalArgumentException();
if (intervals1.length == 0 || intervals2.length == 0) return null;
Arrays.sort(intervals1);
Arrays.sort(intervals2);
int index1 = 0, index2 = 0;
while (true) {
while (index1 < intervals1.length && index2 < intervals2.length
&& intervals1[index1].end <= intervals2[index2].start) index1++;
if (index1 == intervals1.length) return null;
while (index1 < intervals1.length && index2 < intervals2.length
&& intervals2[index2].end <= intervals1[index1].start) index2++;
if (index2 == intervals2.length) return null;
if (intervals1[index1].end > intervals2[index2].start ||
intervals2[index2].end > intervals1[index1].start)
return new Interval(Math.max(intervals1[index1].start,
intervals2[index2].start), Math.min(intervals1[index1].end, intervals2[
index2].end));
}
}
}
第五道: 大文件还是小文件?gossip协议?文件分片多个线程下载再merge?如果是
audio或video的话分片是有优先级的,先播的优先级高。如何load balance? 有没有
CDN?