Skip to content

Commit 0a66a94

Browse files
committed
feat: add solutions to lc problem: No.2231
No.2231.Largest Number After Digit Swaps by Parity
1 parent e4aed2b commit 0a66a94

File tree

8 files changed

+321
-4
lines changed

8 files changed

+321
-4
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
## 站点
2222

23-
- Gitee Pages: https://doocs.gitee.io/leetcode
23+
- Vercel: https://leetcode-doocs.vercel.app
2424
- GitHub Pages: https://doocs.github.io/leetcode
2525

2626
## 算法全解

README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
2020

2121
## Sites
2222

23-
- Gitee Pages: https://doocs.gitee.io/leetcode
23+
- Vercel: https://leetcode-doocs.vercel.app
2424
- GitHub Pages: https://doocs.github.io/leetcode
2525

2626
## Solutions

solution/2200-2299/2231.Largest Number After Digit Swaps by Parity/README.md

+112-1
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,133 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:计数**
47+
48+
**方法二:分组 + 排序**
49+
4650
<!-- tabs:start -->
4751

4852
### **Python3**
4953

5054
<!-- 这里可写当前语言的特殊实现逻辑 -->
5155

5256
```python
53-
57+
class Solution:
58+
def largestInteger(self, num: int) -> int:
59+
cnt = Counter()
60+
x = num
61+
while x:
62+
x, v = divmod(x, 10)
63+
cnt[v] += 1
64+
x = num
65+
ans = 0
66+
t = 1
67+
while x:
68+
x, v = divmod(x, 10)
69+
for y in range(10):
70+
if ((v ^ y) & 1) == 0 and cnt[y]:
71+
ans += y * t
72+
t *= 10
73+
cnt[y] -= 1
74+
break
75+
return ans
5476
```
5577

5678
### **Java**
5779

5880
<!-- 这里可写当前语言的特殊实现逻辑 -->
5981

6082
```java
83+
class Solution {
84+
public int largestInteger(int num) {
85+
int[] cnt = new int[10];
86+
int x = num;
87+
while (x != 0) {
88+
cnt[x % 10]++;
89+
x /= 10;
90+
}
91+
x = num;
92+
int ans = 0;
93+
int t = 1;
94+
while (x != 0) {
95+
int v = x % 10;
96+
x /= 10;
97+
for (int y = 0; y < 10; ++y) {
98+
if (((v ^ y) & 1) == 0 && cnt[y] > 0) {
99+
cnt[y]--;
100+
ans += y * t;
101+
t *= 10;
102+
break;
103+
}
104+
}
105+
}
106+
return ans;
107+
}
108+
}
109+
```
61110

111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
int largestInteger(int num) {
117+
vector<int> cnt(10);
118+
int x = num;
119+
while (x)
120+
{
121+
cnt[x % 10]++;
122+
x /= 10;
123+
}
124+
x = num;
125+
int ans = 0;
126+
long t = 1;
127+
while (x)
128+
{
129+
int v = x % 10;
130+
x /= 10;
131+
for (int y = 0; y < 10; ++y)
132+
{
133+
if (((v ^ y) & 1) == 0 && cnt[y] > 0)
134+
{
135+
cnt[y]--;
136+
ans += y * t;
137+
t *= 10;
138+
break;
139+
}
140+
}
141+
}
142+
return ans;
143+
}
144+
};
145+
```
146+
147+
### **Go**
148+
149+
```go
150+
func largestInteger(num int) int {
151+
cnt := make([]int, 10)
152+
x := num
153+
for x != 0 {
154+
cnt[x%10]++
155+
x /= 10
156+
}
157+
x = num
158+
ans, t := 0, 1
159+
for x != 0 {
160+
v := x % 10
161+
x /= 10
162+
for y := 0; y < 10; y++ {
163+
if ((v^y)&1) == 0 && cnt[y] > 0 {
164+
cnt[y]--
165+
ans += y * t
166+
t *= 10
167+
break
168+
}
169+
}
170+
}
171+
return ans
172+
}
62173
```
63174

64175
### **TypeScript**

solution/2200-2299/2231.Largest Number After Digit Swaps by Parity/README_EN.md

+108-1
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,120 @@ Note that there may be other sequences of swaps but it can be shown that 87655 i
4444
### **Python3**
4545

4646
```python
47-
47+
class Solution:
48+
def largestInteger(self, num: int) -> int:
49+
cnt = Counter()
50+
x = num
51+
while x:
52+
x, v = divmod(x, 10)
53+
cnt[v] += 1
54+
x = num
55+
ans = 0
56+
t = 1
57+
while x:
58+
x, v = divmod(x, 10)
59+
for y in range(10):
60+
if ((v ^ y) & 1) == 0 and cnt[y]:
61+
ans += y * t
62+
t *= 10
63+
cnt[y] -= 1
64+
break
65+
return ans
4866
```
4967

5068
### **Java**
5169

5270
```java
71+
class Solution {
72+
public int largestInteger(int num) {
73+
int[] cnt = new int[10];
74+
int x = num;
75+
while (x != 0) {
76+
cnt[x % 10]++;
77+
x /= 10;
78+
}
79+
x = num;
80+
int ans = 0;
81+
int t = 1;
82+
while (x != 0) {
83+
int v = x % 10;
84+
x /= 10;
85+
for (int y = 0; y < 10; ++y) {
86+
if (((v ^ y) & 1) == 0 && cnt[y] > 0) {
87+
cnt[y]--;
88+
ans += y * t;
89+
t *= 10;
90+
break;
91+
}
92+
}
93+
}
94+
return ans;
95+
}
96+
}
97+
```
5398

99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int largestInteger(int num) {
105+
vector<int> cnt(10);
106+
int x = num;
107+
while (x)
108+
{
109+
cnt[x % 10]++;
110+
x /= 10;
111+
}
112+
x = num;
113+
int ans = 0;
114+
long t = 1;
115+
while (x)
116+
{
117+
int v = x % 10;
118+
x /= 10;
119+
for (int y = 0; y < 10; ++y)
120+
{
121+
if (((v ^ y) & 1) == 0 && cnt[y] > 0)
122+
{
123+
cnt[y]--;
124+
ans += y * t;
125+
t *= 10;
126+
break;
127+
}
128+
}
129+
}
130+
return ans;
131+
}
132+
};
133+
```
134+
135+
### **Go**
136+
137+
```go
138+
func largestInteger(num int) int {
139+
cnt := make([]int, 10)
140+
x := num
141+
for x != 0 {
142+
cnt[x%10]++
143+
x /= 10
144+
}
145+
x = num
146+
ans, t := 0, 1
147+
for x != 0 {
148+
v := x % 10
149+
x /= 10
150+
for y := 0; y < 10; y++ {
151+
if ((v^y)&1) == 0 && cnt[y] > 0 {
152+
cnt[y]--
153+
ans += y * t
154+
t *= 10
155+
break
156+
}
157+
}
158+
}
159+
return ans
160+
}
54161
```
55162

56163
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int largestInteger(int num) {
4+
vector<int> cnt(10);
5+
int x = num;
6+
while (x)
7+
{
8+
cnt[x % 10]++;
9+
x /= 10;
10+
}
11+
x = num;
12+
int ans = 0;
13+
long t = 1;
14+
while (x)
15+
{
16+
int v = x % 10;
17+
x /= 10;
18+
for (int y = 0; y < 10; ++y)
19+
{
20+
if (((v ^ y) & 1) == 0 && cnt[y] > 0)
21+
{
22+
cnt[y]--;
23+
ans += y * t;
24+
t *= 10;
25+
break;
26+
}
27+
}
28+
}
29+
return ans;
30+
}
31+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func largestInteger(num int) int {
2+
cnt := make([]int, 10)
3+
x := num
4+
for x != 0 {
5+
cnt[x%10]++
6+
x /= 10
7+
}
8+
x = num
9+
ans, t := 0, 1
10+
for x != 0 {
11+
v := x % 10
12+
x /= 10
13+
for y := 0; y < 10; y++ {
14+
if ((v^y)&1) == 0 && cnt[y] > 0 {
15+
cnt[y]--
16+
ans += y * t
17+
t *= 10
18+
break
19+
}
20+
}
21+
}
22+
return ans
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int largestInteger(int num) {
3+
int[] cnt = new int[10];
4+
int x = num;
5+
while (x != 0) {
6+
cnt[x % 10]++;
7+
x /= 10;
8+
}
9+
x = num;
10+
int ans = 0;
11+
int t = 1;
12+
while (x != 0) {
13+
int v = x % 10;
14+
x /= 10;
15+
for (int y = 0; y < 10; ++y) {
16+
if (((v ^ y) & 1) == 0 && cnt[y] > 0) {
17+
cnt[y]--;
18+
ans += y * t;
19+
t *= 10;
20+
break;
21+
}
22+
}
23+
}
24+
return ans;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def largestInteger(self, num: int) -> int:
3+
cnt = Counter()
4+
x = num
5+
while x:
6+
x, v = divmod(x, 10)
7+
cnt[v] += 1
8+
x = num
9+
ans = 0
10+
t = 1
11+
while x:
12+
x, v = divmod(x, 10)
13+
for y in range(10):
14+
if ((v ^ y) & 1) == 0 and cnt[y]:
15+
ans += y * t
16+
t *= 10
17+
cnt[y] -= 1
18+
break
19+
return ans

0 commit comments

Comments
 (0)