From 47710b223d9c1eff4b52d1d03ddd706b474be51e Mon Sep 17 00:00:00 2001 From: Dominik Date: Sat, 28 Oct 2023 12:19:40 +0200 Subject: [PATCH 1/3] Create 0303-range-sum-query-immutable.ts --- typescript/0303-range-sum-query-immutable.ts | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 typescript/0303-range-sum-query-immutable.ts diff --git a/typescript/0303-range-sum-query-immutable.ts b/typescript/0303-range-sum-query-immutable.ts new file mode 100644 index 000000000..10b3113c7 --- /dev/null +++ b/typescript/0303-range-sum-query-immutable.ts @@ -0,0 +1,24 @@ +class NumArray { + prefix: number[] = []; + + constructor(nums: number[]) { + let cur = 0; + for (const n of nums) { + cur += n; + this.prefix.push(cur); + } + } + + sumRange(left: number, right: number): number { + const rightSum = this.prefix[right], + leftSum = (left > 0) ? this.prefix[left - 1] : 0; + + return rightSum - leftSum; + } +} + +/** + * Your NumArray object will be instantiated and called as such: + * var obj = new NumArray(nums) + * var param_1 = obj.sumRange(left,right) + */ From 09de9a2711c73bce4fd0eef515c4563c409f30a8 Mon Sep 17 00:00:00 2001 From: Dominik Date: Sat, 28 Oct 2023 13:26:32 +0200 Subject: [PATCH 2/3] Create 0705-design-hashset.ts --- typescript/0705-design-hashset.ts | 65 +++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 typescript/0705-design-hashset.ts diff --git a/typescript/0705-design-hashset.ts b/typescript/0705-design-hashset.ts new file mode 100644 index 000000000..b0bdf8744 --- /dev/null +++ b/typescript/0705-design-hashset.ts @@ -0,0 +1,65 @@ +class _ListNode { // ListNode has a confict + key: number; + next: _ListNode | undefined; + + constructor(key: number) { + this.key = key; + } +} + +class MyHashSet { + readonly ARRAY_LENGTH = Math.pow(10,4); + set = new Array<_ListNode>(this.ARRAY_LENGTH); + + constructor() { + for (let i = 0; i < this.ARRAY_LENGTH; i++) + this.set[i] = new _ListNode(0); + } + + add(key: number): void { + let cur = this.set[key % this.set.length]; + + while (cur.next) { + if (cur.next.key === key) + return; + + cur = cur.next; + } + + cur.next = new _ListNode(key); + } + + remove(key: number): void { + let cur = this.set[key % this.set.length]; + + while (cur.next) { + if (cur.next.key === key) { + cur.next = cur.next.next; + return; + } + + cur = cur.next; + } + } + + contains(key: number): boolean { + let cur = this.set[key % this.set.length]; + + while (cur.next) { + if (cur.next.key === key) + return true; + + cur = cur.next; + } + + return false; + } +} + +/** + * Your MyHashSet object will be instantiated and called as such: + * var obj = new MyHashSet() + * obj.add(key) + * obj.remove(key) + * var param_3 = obj.contains(key) + */ From 006677f9c541c6256ad1ba692271057d90e62224 Mon Sep 17 00:00:00 2001 From: Yaseen Khan Date: Sat, 9 Nov 2024 13:17:00 -0700 Subject: [PATCH 3/3] Add Space --- typescript/0705-design-hashset.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typescript/0705-design-hashset.ts b/typescript/0705-design-hashset.ts index b0bdf8744..b1f3816cc 100644 --- a/typescript/0705-design-hashset.ts +++ b/typescript/0705-design-hashset.ts @@ -8,7 +8,7 @@ class _ListNode { // ListNode has a confict } class MyHashSet { - readonly ARRAY_LENGTH = Math.pow(10,4); + readonly ARRAY_LENGTH = Math.pow(10, 4); set = new Array<_ListNode>(this.ARRAY_LENGTH); constructor() {