freedom.9
Senior Member
clonemaasterUwU - LeetCode Profile
View clonemaasterUwU's profile on LeetCode, the world's largest programming community.
leetcode.com
step 1: has 150 iqĐâu thấy ông nào ở Voz là ClonemaasterUwU đâu nhỉ? À bác này hả @clonemasteruwu vô chia sẻ kinh nghiệm học leetcode cho ae đi bácclonemaasterUwU - LeetCode Profile
View clonemaasterUwU's profile on LeetCode, the world's largest programming community.leetcode.com
chie thất nghiệp mới học leetcode thôi bác đến cơ quan chửi sếp, lên mạng nói xấu cty là có động lực cày ngay ấy màĐâu thấy ông nào ở Voz là ClonemaasterUwU đâu nhỉ? À bác này hả @clonemasteruwu vô chia sẻ kinh nghiệm học leetcode cho ae đi bácclonemaasterUwU - LeetCode Profile
View clonemaasterUwU's profile on LeetCode, the world's largest programming community.leetcode.com
Giờ bác làm big tech hay quant vậychie thất nghiệp mới học leetcode thôi bác đến cơ quan chửi sếp, lên mạng nói xấu cty là có động lực cày ngay ấy mà
Vào giao lưu truyền nghề anh em chứ bác, hay giấu nghề đúng kochie thất nghiệp mới học leetcode thôi bác đến cơ quan chửi sếp, lên mạng nói xấu cty là có động lực cày ngay ấy mà
var MyHashMap = function () {
this.obj = {}
};
/**
* @param {number} key
* @param {number} value
* @return {void}
*/
MyHashMap.prototype.put = function (key, value) {
this.obj[key] = value;
};
/**
* @param {number} key
* @return {number}
*/
MyHashMap.prototype.get = function (key) {
if (key in this.obj) {
return this.obj[key]
}
return -1
};
/**
* @param {number} key
* @return {void}
*/
MyHashMap.prototype.remove = function (key) {
delete this.obj[key]
};
/**
* Your MyHashMap object will be instantiated and called as such:
* var obj = new MyHashMap()
* obj.put(key,value)
* var param_2 = obj.get(key)
* obj.remove(key)
*/
bài này phải tự viết hash hoặc dùng array chứ chơi object thì khác gì map đâuchào ngày mới
JavaScript:var MyHashMap = function () { this.obj = {} }; /** * @param {number} key * @param {number} value * @return {void} */ MyHashMap.prototype.put = function (key, value) { this.obj[key] = value; }; /** * @param {number} key * @return {number} */ MyHashMap.prototype.get = function (key) { if (key in this.obj) { return this.obj[key] } return -1 }; /** * @param {number} key * @return {void} */ MyHashMap.prototype.remove = function (key) { delete this.obj[key] }; /** * Your MyHashMap object will be instantiated and called as such: * var obj = new MyHashMap() * obj.put(key,value) * var param_2 = obj.get(key) * obj.remove(key) */
không ngờ top runtime lại implement bằng Map, thế thì cần gì phải làm nữa đâu
class MyHashMap {
public:
vector<int> data;
MyHashMap(): data((long)1e6 + 1, -1){
}
void put(int key, int value) {
data[key] = value;
}
int get(int key) {
return data[key];
}
void remove(int key) {
data[key] = -1;
}
};
ok vậy để viết lại theo 2d arraybài này phải tự viết hash hoặc dùng array chứ chơi object thì khác gì map đâu
public class MyHashMap {
SupperTreeNode root;
public MyHashMap() {
}
public void Put(int key, int value) {
this.root = InsertNode(this.root, key, value);
}
public int Get(int key) {
SupperTreeNode cur = this.root;
while (cur != null) {
if(key == cur.key)
{
return cur.val;
}
if (key < cur.key) {
cur = cur.left;
} else {
cur = cur.right;
}
}
return -1;
}
public void Remove(int key) {
this.root = DeleteNode(this.root, key);
}
private SupperTreeNode InsertNode(SupperTreeNode root, int key, int val)
{
if(root == null)
return new SupperTreeNode(key, val, null, null);
if(key < root.key)
{
root.left = InsertNode(root.left, key, val);
}else if(key == root.key)
{
root.val = val;
}
else{
root.right = InsertNode(root.right, key, val);
}
return root;
}
private SupperTreeNode DeleteNode(SupperTreeNode root, int key) {
if(root == null)
return null;
if(key < root.key )
root.left = DeleteNode(root.left, key);
else if(key > root.key)
root.right = DeleteNode(root.right, key);
else {
if(root.left == null)
return root.right;
else if(root.right == null)
{
return root.left;
}
else{
var item = successor(root);
root.key = item.key;
root.val = item.val;
[B]root.right = DeleteNode(root.right, root.val); => Sai ở đây[/B]
}
}
return root;
}
private (int key, int val) successor(SupperTreeNode root) {
root = root.right;
while (root.left != null) root = root.left;
return (root.key, root.val);
}
}
public class SupperTreeNode{
public int key;
public int val;
public SupperTreeNode left;
public SupperTreeNode right;
public SupperTreeNode(int key, int val, SupperTreeNode left, SupperTreeNode right)
{
this.key = key;
this.val = val;
this.left = left;
this.right = right;
}
}
/**
* Your MyHashMap object will be instantiated and called as such:
* MyHashMap obj = new MyHashMap();
* obj.Put(key,value);
* int param_2 = obj.Get(key);
* obj.Remove(key);
*/
Cách này ko tối ưu cho những TH key cực to. Chẳng hạn như key = 1 tỷ.Khởi tạo mảng a 10^6, value -1.
remove thì gán lại giá trí -1, get thì trả giá trị a, put thì a = v.
- 0 <= key, value <= 106
- At most 104 calls will be made to put, get, and remove.
java: jdk/src/java.base/share/classes/java/util/HashMap.java at master · openjdk/jdk (https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/HashMap.java)Bình thường thì mấy ngôn ngữ tụi nó implement cái data structure HashMap với O(1) như thế nào nhỉ các fence?
Như JS chỉ có language specification cho Map(). Còn việc implemetation thế nào là do engine như v8, SpiderMonkey làm.Bình thường thì mấy ngôn ngữ tụi nó implement cái data structure HashMap với O(1) như thế nào nhỉ các fence?
java: jdk/src/java.base/share/classes/java/util/HashMap.java at master · openjdk/jdk (https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/HashMap.java)
python thì chắc cái này, xài nhiều mà đó giờ cũng chưa đọc thử :v
python: cpython/Objects/dictobject.c at main · python/cpython (https://github.com/python/cpython/blob/main/Objects/dictobject.c)
class MyHashMap {
constructor() {
this.arr = Array(1337);
}
put(k, v) {
const kk = k % 1337;
this.arr[kk] ??= [];
for (const it of this.arr[kk]) {
if (it[0] === k) {
it[1] = v;
return;
}
}
this.arr[kk].push([k, v]);
}
get(k) {
const kk = k % 1337;
for (const it of (this.arr[kk] ?? [])) {
if (it[0] === k) {
return it[1];
}
}
return -1;
}
remove(k) {
const kk = k % 1337;
if (!this.arr[kk]) {
return;
}
this.arr[kk] = this.arr[kk].filter(it => it[0] !== k);
}
}