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
8383#### Python3
8484
8585``` 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 )
8793```
8894
8995#### Java
9096
9197``` 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+ }
93109```
94110
95111#### C++
96112
97113``` 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+ };
99126```
100127
101128#### Go
102129
103130```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+ ```
104141
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+ }
105154```
106155
107156<!-- 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
8181#### Python3
8282
8383``` 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 )
8591```
8692
8793#### Java
8894
8995``` 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+ }
91107```
92108
93109#### C++
94110
95111``` 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+ };
97124```
98125
99126#### Go
100127
101128```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+ ```
102139
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+ }
103152```
104153
105154<!-- 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