Skip to content

Commit fb86085

Browse files
committed
feat: add solutions to lc problem: No.1752
No.1752.Check if Array Is Sorted and Rotated
1 parent 419c159 commit fb86085

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

solution/1700-1799/1752.Check if Array Is Sorted and Rotated/README.md

+42
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,48 @@ func check(nums []int) bool {
126126
}
127127
```
128128

129+
### **TypeScript**
130+
131+
```ts
132+
function check(nums: number[]): boolean {
133+
const n = nums.length;
134+
return (
135+
nums.reduce((r, v, i) => r + (v > nums[(i + 1) % n] ? 1 : 0), 0) <= 1
136+
);
137+
}
138+
```
139+
140+
### **Rust**
141+
142+
```rust
143+
impl Solution {
144+
pub fn check(nums: Vec<i32>) -> bool {
145+
let n = nums.len();
146+
let mut count = 0;
147+
for i in 0..n {
148+
if nums[i] > nums[(i + 1) % n] {
149+
count += 1;
150+
}
151+
}
152+
count <= 1
153+
}
154+
}
155+
```
156+
157+
### **C**
158+
159+
```c
160+
bool check(int *nums, int numsSize) {
161+
int count = 0;
162+
for (int i = 0; i < numsSize; i++) {
163+
if (nums[i] > nums[(i + 1) % numsSize]) {
164+
count++;
165+
}
166+
}
167+
return count <= 1;
168+
}
169+
```
170+
129171
### **...**
130172
131173
```

solution/1700-1799/1752.Check if Array Is Sorted and Rotated/README_EN.md

+42
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,48 @@ func check(nums []int) bool {
106106
}
107107
```
108108

109+
### **TypeScript**
110+
111+
```ts
112+
function check(nums: number[]): boolean {
113+
const n = nums.length;
114+
return (
115+
nums.reduce((r, v, i) => r + (v > nums[(i + 1) % n] ? 1 : 0), 0) <= 1
116+
);
117+
}
118+
```
119+
120+
### **Rust**
121+
122+
```rust
123+
impl Solution {
124+
pub fn check(nums: Vec<i32>) -> bool {
125+
let n = nums.len();
126+
let mut count = 0;
127+
for i in 0..n {
128+
if nums[i] > nums[(i + 1) % n] {
129+
count += 1;
130+
}
131+
}
132+
count <= 1
133+
}
134+
}
135+
```
136+
137+
### **C**
138+
139+
```c
140+
bool check(int *nums, int numsSize) {
141+
int count = 0;
142+
for (int i = 0; i < numsSize; i++) {
143+
if (nums[i] > nums[(i + 1) % numsSize]) {
144+
count++;
145+
}
146+
}
147+
return count <= 1;
148+
}
149+
```
150+
109151
### **...**
110152
111153
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
bool check(int *nums, int numsSize) {
2+
int count = 0;
3+
for (int i = 0; i < numsSize; i++) {
4+
if (nums[i] > nums[(i + 1) % numsSize]) {
5+
count++;
6+
}
7+
}
8+
return count <= 1;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn check(nums: Vec<i32>) -> bool {
3+
let n = nums.len();
4+
let mut count = 0;
5+
for i in 0..n {
6+
if nums[i] > nums[(i + 1) % n] {
7+
count += 1;
8+
}
9+
}
10+
count <= 1
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function check(nums: number[]): boolean {
2+
const n = nums.length;
3+
return (
4+
nums.reduce((r, v, i) => r + (v > nums[(i + 1) % n] ? 1 : 0), 0) <= 1
5+
);
6+
}

0 commit comments

Comments
 (0)