Skip to content

Commit c6b450c

Browse files
committed
feat: add solutions to lc problem: No.1460
No.1460.Make Two Arrays Equal by Reversing Sub-arrays
1 parent cda64b1 commit c6b450c

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed

solution/1400-1499/1460.Make Two Arrays Equal by Reversing Sub-arrays/README.md

+72
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,78 @@ func canBeEqual(target []int, arr []int) bool {
183183
}
184184
```
185185

186+
### **C**
187+
188+
```c
189+
bool canBeEqual(int* target, int targetSize, int* arr, int arrSize){
190+
int count[1001] = {0};
191+
for (int i = 0 ; i < targetSize; i++) {
192+
count[target[i]]++;
193+
count[arr[i]]--;
194+
}
195+
for (int i = 0; i < 1001; i++) {
196+
if (count[i] != 0) {
197+
return false;
198+
}
199+
}
200+
return true;
201+
}
202+
```
203+
204+
### **TypeScript**
205+
206+
```ts
207+
function canBeEqual(target: number[], arr: number[]): boolean {
208+
target.sort((a, b) => a - b);
209+
arr.sort((a, b) => a - b);
210+
const n = arr.length;
211+
for (let i = 0; i < n; i++) {
212+
if (target[i] !== arr[i]) {
213+
return false;
214+
}
215+
}
216+
return true;
217+
}
218+
```
219+
220+
```ts
221+
function canBeEqual(target: number[], arr: number[]): boolean {
222+
const n = target.length;
223+
const count = new Array(10001).fill(0);
224+
for (let i = 0; i < n; i++) {
225+
count[target[i]]++;
226+
count[arr[i]]--;
227+
}
228+
return count.every(v => v === 0);
229+
}
230+
```
231+
232+
### **Rust**
233+
234+
```rust
235+
impl Solution {
236+
pub fn can_be_equal(mut target: Vec<i32>, mut arr: Vec<i32>) -> bool {
237+
target.sort();
238+
arr.sort();
239+
target == arr
240+
}
241+
}
242+
```
243+
244+
```rust
245+
impl Solution {
246+
pub fn can_be_equal(mut target: Vec<i32>, mut arr: Vec<i32>) -> bool {
247+
let n = target.len();
248+
let mut count = [0; 1001];
249+
for i in 0..n {
250+
count[target[i] as usize] += 1;
251+
count[arr[i] as usize] -= 1;
252+
}
253+
count.iter().all(|v| *v == 0)
254+
}
255+
}
256+
```
257+
186258
### **...**
187259

188260
```

solution/1400-1499/1460.Make Two Arrays Equal by Reversing Sub-arrays/README_EN.md

+72
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,78 @@ func canBeEqual(target []int, arr []int) bool {
155155
}
156156
```
157157

158+
### **C**
159+
160+
```c
161+
bool canBeEqual(int* target, int targetSize, int* arr, int arrSize){
162+
int count[1001] = {0};
163+
for (int i = 0 ; i < targetSize; i++) {
164+
count[target[i]]++;
165+
count[arr[i]]--;
166+
}
167+
for (int i = 0; i < 1001; i++) {
168+
if (count[i] != 0) {
169+
return false;
170+
}
171+
}
172+
return true;
173+
}
174+
```
175+
176+
### **TypeScript**
177+
178+
```ts
179+
function canBeEqual(target: number[], arr: number[]): boolean {
180+
target.sort((a, b) => a - b);
181+
arr.sort((a, b) => a - b);
182+
const n = arr.length;
183+
for (let i = 0; i < n; i++) {
184+
if (target[i] !== arr[i]) {
185+
return false;
186+
}
187+
}
188+
return true;
189+
}
190+
```
191+
192+
```ts
193+
function canBeEqual(target: number[], arr: number[]): boolean {
194+
const n = target.length;
195+
const count = new Array(10001).fill(0);
196+
for (let i = 0; i < n; i++) {
197+
count[target[i]]++;
198+
count[arr[i]]--;
199+
}
200+
return count.every(v => v === 0);
201+
}
202+
```
203+
204+
### **Rust**
205+
206+
```rust
207+
impl Solution {
208+
pub fn can_be_equal(mut target: Vec<i32>, mut arr: Vec<i32>) -> bool {
209+
target.sort();
210+
arr.sort();
211+
target == arr
212+
}
213+
}
214+
```
215+
216+
```rust
217+
impl Solution {
218+
pub fn can_be_equal(mut target: Vec<i32>, mut arr: Vec<i32>) -> bool {
219+
let n = target.len();
220+
let mut count = [0; 1001];
221+
for i in 0..n {
222+
count[target[i] as usize] += 1;
223+
count[arr[i] as usize] -= 1;
224+
}
225+
count.iter().all(|v| *v == 0)
226+
}
227+
}
228+
```
229+
158230
### **...**
159231

160232
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
bool canBeEqual(int* target, int targetSize, int* arr, int arrSize){
2+
int count[1001] = {0};
3+
for (int i = 0 ; i < targetSize; i++) {
4+
count[target[i]]++;
5+
count[arr[i]]--;
6+
}
7+
for (int i = 0; i < 1001; i++) {
8+
if (count[i] != 0) {
9+
return false;
10+
}
11+
}
12+
return true;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
impl Solution {
2+
pub fn can_be_equal(mut target: Vec<i32>, mut arr: Vec<i32>) -> bool {
3+
let n = target.len();
4+
let mut count = [0; 1001];
5+
for i in 0..n {
6+
count[target[i] as usize] += 1;
7+
count[arr[i] as usize] -= 1;
8+
}
9+
count.iter().all(|v| *v == 0)
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function canBeEqual(target: number[], arr: number[]): boolean {
2+
const n = target.length;
3+
const count = new Array(10001).fill(0);
4+
for (let i = 0; i < n; i++) {
5+
count[target[i]]++;
6+
count[arr[i]]--;
7+
}
8+
return count.every(v => v === 0);
9+
}

0 commit comments

Comments
 (0)