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 85923b4

Browse files
authored
feat: add solutions to lc problem: No.0670 (doocs#3647)
1 parent 2b1da74 commit 85923b4

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

‎solution/0600-0699/0670.Maximum Swap/README.md‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,58 @@ impl Solution {
235235

236236
<!-- solution:end -->
237237

238+
<!-- solution:start -->
239+
240+
### 方法二:贪心 + 空间优化
241+
242+
<!-- tabs:start -->
243+
244+
#### TypeScript
245+
246+
```ts
247+
function maximumSwap(num: number): number {
248+
const ans = [...String(num)];
249+
let [min, max, maybeMax, n] = [-1, -1, -1, ans.length];
250+
251+
for (let i = n - 1; i >= 0; i--) {
252+
if (ans[i] > (ans[maybeMax] ?? -1)) maybeMax = i;
253+
if (i < maybeMax && ans[i] < ans[maybeMax]) {
254+
[min, max] = [i, maybeMax];
255+
}
256+
}
257+
258+
if (~min && ~max && min < max) {
259+
[ans[min], ans[max]] = [ans[max], ans[min]];
260+
}
261+
262+
return +ans.join('');
263+
}
264+
```
265+
266+
#### JavaScript
267+
268+
```js
269+
function maximumSwap(num) {
270+
const ans = [...String(num)];
271+
let [min, max, maybeMax, n] = [-1, -1, -1, ans.length];
272+
273+
for (let i = n - 1; i >= 0; i--) {
274+
if (ans[i] > (ans[maybeMax] ?? -1)) maybeMax = i;
275+
if (i < maybeMax && ans[i] < ans[maybeMax]) {
276+
[min, max] = [i, maybeMax];
277+
}
278+
}
279+
280+
if (~min && ~max && min < max) {
281+
[ans[min], ans[max]] = [ans[max], ans[min]];
282+
}
283+
284+
return +ans.join('');
285+
}
286+
```
287+
288+
<!-- tabs:end -->
289+
290+
<!-- solution:end -->
291+
238292
<!-- problem:end -->

‎solution/0600-0699/0670.Maximum Swap/README_EN.md‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,58 @@ impl Solution {
239239

240240
<!-- solution:end -->
241241

242+
<!-- solution:start -->
243+
244+
### Solution 2: Space Optimized Greedy
245+
246+
<!-- tabs:start -->
247+
248+
#### TypeScript
249+
250+
```ts
251+
function maximumSwap(num: number): number {
252+
const ans = [...String(num)];
253+
let [min, max, maybeMax, n] = [-1, -1, -1, ans.length];
254+
255+
for (let i = n - 1; i >= 0; i--) {
256+
if (ans[i] > (ans[maybeMax] ?? -1)) maybeMax = i;
257+
if (i < maybeMax && ans[i] < ans[maybeMax]) {
258+
[min, max] = [i, maybeMax];
259+
}
260+
}
261+
262+
if (~min && ~max && min < max) {
263+
[ans[min], ans[max]] = [ans[max], ans[min]];
264+
}
265+
266+
return +ans.join('');
267+
}
268+
```
269+
270+
#### JavaScript
271+
272+
```js
273+
function maximumSwap(num) {
274+
const ans = [...String(num)];
275+
let [min, max, maybeMax, n] = [-1, -1, -1, ans.length];
276+
277+
for (let i = n - 1; i >= 0; i--) {
278+
if (ans[i] > (ans[maybeMax] ?? -1)) maybeMax = i;
279+
if (i < maybeMax && ans[i] < ans[maybeMax]) {
280+
[min, max] = [i, maybeMax];
281+
}
282+
}
283+
284+
if (~min && ~max && min < max) {
285+
[ans[min], ans[max]] = [ans[max], ans[min]];
286+
}
287+
288+
return +ans.join('');
289+
}
290+
```
291+
292+
<!-- tabs:end -->
293+
294+
<!-- solution:end -->
295+
242296
<!-- problem:end -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function maximumSwap(num) {
2+
const ans = [...String(num)];
3+
let [min, max, maybeMax, n] = [-1, -1, -1, ans.length];
4+
5+
for (let i = n - 1; i >= 0; i--) {
6+
if (ans[i] > (ans[maybeMax] ?? -1)) maybeMax = i;
7+
if (i < maybeMax && ans[i] < ans[maybeMax]) {
8+
[min, max] = [i, maybeMax];
9+
}
10+
}
11+
12+
if (~min && ~max && min < max) {
13+
[ans[min], ans[max]] = [ans[max], ans[min]];
14+
}
15+
16+
return +ans.join('');
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function maximumSwap(num: number): number {
2+
const ans = [...String(num)];
3+
let [min, max, maybeMax, n] = [-1, -1, -1, ans.length];
4+
5+
for (let i = n - 1; i >= 0; i--) {
6+
if (ans[i] > (ans[maybeMax] ?? -1)) maybeMax = i;
7+
if (i < maybeMax && ans[i] < ans[maybeMax]) {
8+
[min, max] = [i, maybeMax];
9+
}
10+
}
11+
12+
if (~min && ~max && min < max) {
13+
[ans[min], ans[max]] = [ans[max], ans[min]];
14+
}
15+
16+
return +ans.join('');
17+
}

0 commit comments

Comments
(0)

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