You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.2 KiB

package class34;
import java.util.HashMap;
// 测试链接 : https://leetcode.com/problems/insert-delete-getrandom-o1/
public class Problem_0380_InsertDeleteGetRandom {
public class RandomizedSet {
private HashMap<Integer, Integer> keyIndexMap;
private HashMap<Integer, Integer> indexKeyMap;
private int size;
public RandomizedSet() {
keyIndexMap = new HashMap<Integer, Integer>();
indexKeyMap = new HashMap<Integer, Integer>();
size = 0;
}
public boolean insert(int val) {
if (!keyIndexMap.containsKey(val)) {
keyIndexMap.put(val, size);
indexKeyMap.put(size++, val);
return true;
}
return false;
}
public boolean remove(int val) {
if (keyIndexMap.containsKey(val)) {
int deleteIndex = keyIndexMap.get(val);
int lastIndex = --size;
int lastKey = indexKeyMap.get(lastIndex);
keyIndexMap.put(lastKey, deleteIndex);
indexKeyMap.put(deleteIndex, lastKey);
keyIndexMap.remove(val);
indexKeyMap.remove(lastIndex);
return true;
}
return false;
}
public int getRandom() {
if (size == 0) {
return -1;
}
int randomIndex = (int) (Math.random() * size);
return indexKeyMap.get(randomIndex);
}
}
}