Skip to content

Commit 2fce58e

Browse files
committed
feat: add typescript solution to lc problem: No.2295
No.2295.Replace Elements in an Array
1 parent 771f8f3 commit 2fce58e

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

solution/2200-2299/2295.Replace Elements in an Array/README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,20 @@ func arrayChange(nums []int, operations [][]int) []int {
158158
### **TypeScript**
159159

160160
```ts
161-
161+
function arrayChange(nums: number[], operations: number[][]): number[] {
162+
const n = nums.length;
163+
let hashMap = new Map(nums.map((v, i) => [v, i]));
164+
for (let [oldVal, newVal] of operations) {
165+
let idx = hashMap.get(oldVal);
166+
hashMap.delete(oldVal);
167+
hashMap.set(newVal, idx);
168+
}
169+
let ans = new Array(n);
170+
for (let [val, key] of hashMap.entries()) {
171+
ans[key] = val;
172+
}
173+
return ans;
174+
};
162175
```
163176

164177
### **...**

solution/2200-2299/2295.Replace Elements in an Array/README_EN.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,20 @@ func arrayChange(nums []int, operations [][]int) []int {
148148
### **TypeScript**
149149

150150
```ts
151-
151+
function arrayChange(nums: number[], operations: number[][]): number[] {
152+
const n = nums.length;
153+
let hashMap = new Map(nums.map((v, i) => [v, i]));
154+
for (let [oldVal, newVal] of operations) {
155+
let idx = hashMap.get(oldVal);
156+
hashMap.delete(oldVal);
157+
hashMap.set(newVal, idx);
158+
}
159+
let ans = new Array(n);
160+
for (let [val, key] of hashMap.entries()) {
161+
ans[key] = val;
162+
}
163+
return ans;
164+
};
152165
```
153166

154167
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function arrayChange(nums: number[], operations: number[][]): number[] {
2+
const n = nums.length;
3+
let hashMap = new Map(nums.map((v, i) => [v, i]));
4+
for (let [oldVal, newVal] of operations) {
5+
let idx = hashMap.get(oldVal);
6+
hashMap.delete(oldVal);
7+
hashMap.set(newVal, idx);
8+
}
9+
let ans = new Array(n);
10+
for (let [val, key] of hashMap.entries()) {
11+
ans[key] = val;
12+
}
13+
return ans;
14+
};

0 commit comments

Comments
 (0)