You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
According to the problem description, the length of the array $\textit{nums}$ is $3$. We can enumerate all permutations of $\textit{nums}$, which has $3! = 6$ permutations. Then, we convert the elements of the permuted array into binary strings, concatenate these binary strings, and finally convert the concatenated binary string into a decimal number to get the maximum value.
65
+
66
+
The time complexity is $O(\log M)$, where $M$ represents the maximum value of the elements in $\textit{nums}$. The space complexity is $O(1)$.
63
67
64
68
<!-- tabs:start -->
65
69
66
70
#### Python3
67
71
68
72
```python
69
-
73
+
classSolution:
74
+
defmaxGoodNumber(self, nums: List[int]) -> int:
75
+
ans =0
76
+
for arr in permutations(nums):
77
+
num =int("".join(bin(i)[2:] for i in arr), 2)
78
+
ans =max(ans, num)
79
+
return ans
70
80
```
71
81
72
82
#### Java
73
83
74
84
```java
75
-
85
+
classSolution {
86
+
privateint[] nums;
87
+
88
+
publicintmaxGoodNumber(int[] nums) {
89
+
this.nums = nums;
90
+
int ans = f(0, 1, 2);
91
+
ans =Math.max(ans, f(0, 2, 1));
92
+
ans =Math.max(ans, f(1, 0, 2));
93
+
ans =Math.max(ans, f(1, 2, 0));
94
+
ans =Math.max(ans, f(2, 0, 1));
95
+
ans =Math.max(ans, f(2, 1, 0));
96
+
return ans;
97
+
}
98
+
99
+
privateintf(inti, intj, intk) {
100
+
String a =Integer.toBinaryString(nums[i]);
101
+
String b =Integer.toBinaryString(nums[j]);
102
+
String c =Integer.toBinaryString(nums[k]);
103
+
returnInteger.parseInt(a + b + c, 2);
104
+
}
105
+
}
76
106
```
77
107
78
108
#### C++
79
109
80
110
```cpp
81
-
111
+
classSolution {
112
+
public:
113
+
int maxGoodNumber(vector<int>& nums) {
114
+
int ans = 0;
115
+
auto f = [&](vector<int>& nums) {
116
+
int res = 0;
117
+
vector<int> t;
118
+
for (int x : nums) {
119
+
for (; x; x >>= 1) {
120
+
t.push_back(x & 1);
121
+
}
122
+
}
123
+
while (t.size()) {
124
+
res = res * 2 + t.back();
125
+
t.pop_back();
126
+
}
127
+
return res;
128
+
};
129
+
for (int i = 0; i < 6; ++i) {
130
+
ans = max(ans, f(nums));
131
+
next_permutation(nums.begin(), nums.end());
132
+
}
133
+
return ans;
134
+
}
135
+
};
82
136
```
83
137
84
138
#### Go
85
139
86
140
```go
141
+
func maxGoodNumber(nums []int) int {
142
+
f := func(i, j, k int) int {
143
+
a := strconv.FormatInt(int64(nums[i]), 2)
144
+
b := strconv.FormatInt(int64(nums[j]), 2)
145
+
c := strconv.FormatInt(int64(nums[k]), 2)
146
+
res, _ := strconv.ParseInt(a+b+c, 2, 64)
147
+
return int(res)
148
+
}
149
+
ans := f(0, 1, 2)
150
+
ans = max(ans, f(0, 2, 1))
151
+
ans = max(ans, f(1, 0, 2))
152
+
ans = max(ans, f(1, 2, 0))
153
+
ans = max(ans, f(2, 0, 1))
154
+
ans = max(ans, f(2, 1, 0))
155
+
return ans
156
+
}
157
+
```
87
158
159
+
#### TypeScript
160
+
161
+
```ts
162
+
function maxGoodNumber(nums:number[]):number {
163
+
const f = (i:number, j:number, k:number):number=> {
0 commit comments