这里记一次阿里巴巴编程题
题目一
用java实现一个LRU(Least recently used最近最少使用)缓存,支持get和put操作,并且两者的时间复杂度为O(1)
题目二
给定一个整数数组nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 5 ?找出所有满足条件且不重复的三元组。
例如, 给定数组 nums = [10,2,4,-3,9,-9,-1],
满足要求的三元组集合为:
[
[10,4,-9],
[2,4,-1]
]
package com.bilibili.finance.base;
import com.bilibili.utils.JsonUtils;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.junit.Assert;
import org.junit.Test;import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;/**
* Created by user on 2018/8/1.
*
* @author:BXY
* @Description:
* @Date:Created in 20:24 on 2018/8/1.
*/
public class BXYTest {/**
* 题目1:用java实现一个LRU(Least recently used最近最少使用)缓存,支持get和put操作,并且两者的时间复杂度为O(1)
* 实现:参考LinkedHashMap
*/
public static class LRUCache{
private LinkedHashMap<Integer, Integer> map;
private final int capacity;
public LRUCache(int capacity) {
this.capacity = capacity;
//accessOrder = true,按访问顺序排序,访问后会移到链尾(tail)
map = new LinkedHashMap<Integer, Integer>(capacity, 0.75f, true){
//LinkedHashMap封装的淘汰方法,当put新值方法返回true时,就移除该map中最老的键和值
protected boolean removeEldestEntry(Map.Entry eldest) {
//如果Map的size大于设定的最大长度,返回true,再新加入对象时删除最少使用对象(head)
return size() > capacity;
}
};
}public int get(int key) {
return map.getOrDefault(key, -1);
}public void put(int key, int value) {
map.put(key, value);
}
}/**
* 题目1 单元测试
*/
@Test
public void testLRUCache() {
LRUCache t = new LRUCache(4);
t.put(1, 1);
t.put(2, 2);
t.put(3, 3);
t.put(4, 4);
Assert.assertTrue(t.get(1) != -1);
t.put(5, 5);
Assert.assertTrue(t.get(2) == -1);
}/**
* 题目2:给定一个整数数组nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 5 ?找出所有满足条件且不重复的三元组。
* 例如, 给定数组 nums = [10,2,4,-3,9,-9,-1],
* 满足要求的三元组集合为:
* [
* [10,4,-9],
* [2,4,-1]
* ]
*/
public static List<List<Integer>> getSumFiveLists(int[] collection){
if (collection == null || collection.length < 3) {
return Collections.emptyList();
}
List<List<Integer>> result = Lists.newArrayList();
//按从小到大排序
Arrays.sort(collection);
int n = collection.length;
int i = -1;
int a, b, c;
do {
i++;
//固定第一个数a
a = collection[i];
//a和后一个数重复时继续遍历
if (a == collection[i + 1]) {
continue;
}int j = i;
do {
j++;
//固定第二个数b
b = collection[j];
//确认第三个数值为c
c = 5 - a - b;
if (c < b) {
break;
}
//向后遍历确认c是否存在(这里可以使用二分遍历)
for (int k = j + 1; k < n; k++) {
if (c == collection[k]) {
result.add(ImmutableList.of(a, b, c));
break;
}
}
} while (j < n - 1);
} while (i < n - 2);return result;
}/**
* 题目2 单元测试
*/
@Test
public void testGetSumFiveLists() {
int[] nums = {10, 2, 4, -3, 9, -9, -1};
List<List<Integer>> list = getSumFiveLists(nums);
//这里输出:[[-9,4,10],[-3,-1,9],[-1,2,4]]
System.out.println(JsonUtils.toJsonTree(list));
Assert.assertTrue(list.contains(ImmutableList.of(-9, 4, 10)));
Assert.assertTrue(list.contains(ImmutableList.of(-3,-1,9)));
Assert.assertTrue(list.contains(ImmutableList.of(-1, 2, 4)));
}
}