File tree Expand file tree Collapse file tree 7 files changed +152
-6
lines changed
solution/3600-3699/3644.Maximum K to Sort a Permutation Expand file tree Collapse file tree 7 files changed +152
-6
lines changed Original file line number Diff line number Diff line change @@ -83,25 +83,74 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3644.Ma
83
83
#### Python3
84
84
85
85
``` python
86
-
86
+ class Solution :
87
+ def sortPermutation (self , nums : List[int ]) -> int :
88
+ ans = - 1
89
+ for i, x in enumerate (nums):
90
+ if i != x:
91
+ ans &= x
92
+ return max (ans, 0 )
87
93
```
88
94
89
95
#### Java
90
96
91
97
``` java
92
-
98
+ class Solution {
99
+ public int sortPermutation (int [] nums ) {
100
+ int ans = - 1 ;
101
+ for (int i = 0 ; i < nums. length; ++ i) {
102
+ if (i != nums[i]) {
103
+ ans &= nums[i];
104
+ }
105
+ }
106
+ return Math . max(ans, 0 );
107
+ }
108
+ }
93
109
```
94
110
95
111
#### C++
96
112
97
113
``` cpp
98
-
114
+ class Solution {
115
+ public:
116
+ int sortPermutation(vector<int >& nums) {
117
+ int ans = -1;
118
+ for (int i = 0; i < nums.size(); ++i) {
119
+ if (i != nums[ i] ) {
120
+ ans &= nums[ i] ;
121
+ }
122
+ }
123
+ return max(ans, 0);
124
+ }
125
+ };
99
126
```
100
127
101
128
#### Go
102
129
103
130
```go
131
+ func sortPermutation(nums []int) int {
132
+ ans := -1
133
+ for i, x := range nums {
134
+ if i != x {
135
+ ans &= x
136
+ }
137
+ }
138
+ return max(ans, 0)
139
+ }
140
+ ```
104
141
142
+ #### TypeScript
143
+
144
+ ``` ts
145
+ function sortPermutation(nums : number []): number {
146
+ let ans = - 1 ;
147
+ for (let i = 0 ; i < nums .length ; ++ i ) {
148
+ if (i != nums [i ]) {
149
+ ans &= nums [i ];
150
+ }
151
+ }
152
+ return Math .max (ans , 0 );
153
+ }
105
154
```
106
155
107
156
<!-- tabs: end -->
Original file line number Diff line number Diff line change @@ -81,25 +81,74 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3644.Ma
81
81
#### Python3
82
82
83
83
``` python
84
-
84
+ class Solution :
85
+ def sortPermutation (self , nums : List[int ]) -> int :
86
+ ans = - 1
87
+ for i, x in enumerate (nums):
88
+ if i != x:
89
+ ans &= x
90
+ return max (ans, 0 )
85
91
```
86
92
87
93
#### Java
88
94
89
95
``` java
90
-
96
+ class Solution {
97
+ public int sortPermutation (int [] nums ) {
98
+ int ans = - 1 ;
99
+ for (int i = 0 ; i < nums. length; ++ i) {
100
+ if (i != nums[i]) {
101
+ ans &= nums[i];
102
+ }
103
+ }
104
+ return Math . max(ans, 0 );
105
+ }
106
+ }
91
107
```
92
108
93
109
#### C++
94
110
95
111
``` cpp
96
-
112
+ class Solution {
113
+ public:
114
+ int sortPermutation(vector<int >& nums) {
115
+ int ans = -1;
116
+ for (int i = 0; i < nums.size(); ++i) {
117
+ if (i != nums[ i] ) {
118
+ ans &= nums[ i] ;
119
+ }
120
+ }
121
+ return max(ans, 0);
122
+ }
123
+ };
97
124
```
98
125
99
126
#### Go
100
127
101
128
```go
129
+ func sortPermutation(nums []int) int {
130
+ ans := -1
131
+ for i, x := range nums {
132
+ if i != x {
133
+ ans &= x
134
+ }
135
+ }
136
+ return max(ans, 0)
137
+ }
138
+ ```
102
139
140
+ #### TypeScript
141
+
142
+ ``` ts
143
+ function sortPermutation(nums : number []): number {
144
+ let ans = - 1 ;
145
+ for (let i = 0 ; i < nums .length ; ++ i ) {
146
+ if (i != nums [i ]) {
147
+ ans &= nums [i ];
148
+ }
149
+ }
150
+ return Math .max (ans , 0 );
151
+ }
103
152
```
104
153
105
154
<!-- tabs: end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int sortPermutation (vector<int >& nums) {
4
+ int ans = -1 ;
5
+ for (int i = 0 ; i < nums.size (); ++i) {
6
+ if (i != nums[i]) {
7
+ ans &= nums[i];
8
+ }
9
+ }
10
+ return max (ans, 0 );
11
+ }
12
+ };
Original file line number Diff line number Diff line change
1
+ func sortPermutation (nums []int ) int {
2
+ ans := - 1
3
+ for i , x := range nums {
4
+ if i != x {
5
+ ans &= x
6
+ }
7
+ }
8
+ return max (ans , 0 )
9
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int sortPermutation (int [] nums ) {
3
+ int ans = -1 ;
4
+ for (int i = 0 ; i < nums .length ; ++i ) {
5
+ if (i != nums [i ]) {
6
+ ans &= nums [i ];
7
+ }
8
+ }
9
+ return Math .max (ans , 0 );
10
+ }
11
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def sortPermutation (self , nums : List [int ]) -> int :
3
+ ans = - 1
4
+ for i , x in enumerate (nums ):
5
+ if i != x :
6
+ ans &= x
7
+ return max (ans , 0 )
Original file line number Diff line number Diff line change
1
+ function sortPermutation ( nums : number [ ] ) : number {
2
+ let ans = - 1 ;
3
+ for ( let i = 0 ; i < nums . length ; ++ i ) {
4
+ if ( i != nums [ i ] ) {
5
+ ans &= nums [ i ] ;
6
+ }
7
+ }
8
+ return Math . max ( ans , 0 ) ;
9
+ }
You can’t perform that action at this time.
0 commit comments