Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 01a0b03

Browse files
authored
feat: add js solution to lc problem: No. 2191 (doocs#3319)
1 parent 177c878 commit 01a0b03

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

‎solution/2100-2199/2191.Sort the Jumbled Numbers/README.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,34 @@ function sortJumbled(mapping: number[], nums: number[]): number[] {
224224
}
225225
```
226226

227+
#### JavaScript
228+
229+
```js
230+
/**
231+
* @param {number[]} mapping
232+
* @param {number[]} nums
233+
* @return {number[]}
234+
*/
235+
var sortJumbled = function (mapping, nums) {
236+
const n = nums.length;
237+
const f = x => {
238+
if (x === 0) {
239+
return mapping[0];
240+
}
241+
let y = 0;
242+
for (let k = 1; x; x = (x / 10) | 0) {
243+
const v = mapping[x % 10];
244+
y += v * k;
245+
k *= 10;
246+
}
247+
return y;
248+
};
249+
const arr = nums.map((x, i) => [f(x), i]);
250+
arr.sort((a, b) => (a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]));
251+
return arr.map(x => nums[x[1]]);
252+
};
253+
```
254+
227255
#### Rust
228256

229257
```rust

‎solution/2100-2199/2191.Sort the Jumbled Numbers/README_EN.md‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ tags:
3838
<pre>
3939
<strong>Input:</strong> mapping = [8,9,4,0,2,1,3,5,7,6], nums = [991,338,38]
4040
<strong>Output:</strong> [338,38,991]
41-
<strong>Explanation:</strong>
41+
<strong>Explanation:</strong>
4242
Map the number 991 as follows:
4343
1. mapping[9] = 6, so all occurrences of the digit 9 will become 6.
4444
2. mapping[1] = 9, so all occurrences of the digit 1 will become 9.
@@ -222,6 +222,34 @@ function sortJumbled(mapping: number[], nums: number[]): number[] {
222222
}
223223
```
224224

225+
#### JavaScript
226+
227+
```js
228+
/**
229+
* @param {number[]} mapping
230+
* @param {number[]} nums
231+
* @return {number[]}
232+
*/
233+
var sortJumbled = function (mapping, nums) {
234+
const n = nums.length;
235+
const f = x => {
236+
if (x === 0) {
237+
return mapping[0];
238+
}
239+
let y = 0;
240+
for (let k = 1; x; x = (x / 10) | 0) {
241+
const v = mapping[x % 10];
242+
y += v * k;
243+
k *= 10;
244+
}
245+
return y;
246+
};
247+
const arr = nums.map((x, i) => [f(x), i]);
248+
arr.sort((a, b) => (a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]));
249+
return arr.map(x => nums[x[1]]);
250+
};
251+
```
252+
225253
#### Rust
226254

227255
```rust
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} mapping
3+
* @param {number[]} nums
4+
* @return {number[]}
5+
*/
6+
var sortJumbled = function (mapping, nums) {
7+
const n = nums.length;
8+
const f = x => {
9+
if (x === 0) {
10+
return mapping[0];
11+
}
12+
let y = 0;
13+
for (let k = 1; x; x = (x / 10) | 0) {
14+
const v = mapping[x % 10];
15+
y += v * k;
16+
k *= 10;
17+
}
18+
return y;
19+
};
20+
const arr = nums.map((x, i) => [f(x), i]);
21+
arr.sort((a, b) => (a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]));
22+
return arr.map(x => nums[x[1]]);
23+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /