Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ tags:

### 方法一:一次遍历

要满足题目要求,那么数组 `nums` 中最多只能存在一个元素,其值大于下一个元素,即 $nums[i] \gt nums[i + 1]$。如果存在多个这样的元素,那么数组 `nums` 无法通过轮转得到。
要满足题目要求,那么数组 $\textit{nums}$ 中最多只能存在一个元素,其值大于下一个元素,即 $nums[i] \gt nums[i + 1]$。如果存在多个这样的元素,那么数组 $\textit{nums}$ 无法通过轮转得到。

注意,数组 `nums` 最后一个元素的下一个元素是数组 `nums` 的第一个元素。
注意,数组 $\textit{nums}$ 最后一个元素的下一个元素是数组 $\textit{nums}$ 的第一个元素。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$

<!-- tabs:start -->

Expand All @@ -84,7 +84,7 @@ tags:
```python
class Solution:
def check(self, nums: List[int]) -> bool:
return sum(nums[i - 1] > v for i, v in enumerate(nums)) <= 1
return sum(nums[i - 1] > x for i, x in enumerate(nums)) <= 1
```

#### Java
Expand Down Expand Up @@ -123,8 +123,8 @@ public:
```go
func check(nums []int) bool {
cnt := 0
for i, v := range nums {
if v > nums[(i+1)%len(nums)] {
for i, x := range nums {
if x > nums[(i+1)%len(nums)] {
cnt++
}
}
Expand All @@ -137,7 +137,7 @@ func check(nums []int) bool {
```ts
function check(nums: number[]): boolean {
const n = nums.length;
return nums.reduce((r, v, i) => r + (v > nums[(i + 1) % n] ? 1 : 0), 0) <= 1;
return nums.reduce((cnt, x, i) => cnt + (x > nums[(i + 1) % n] ? 1 : 0), 0) <= 1;
}
```

Expand All @@ -147,13 +147,10 @@ function check(nums: number[]): boolean {
impl Solution {
pub fn check(nums: Vec<i32>) -> bool {
let n = nums.len();
let mut count = 0;
for i in 0..n {
if nums[i] > nums[(i + 1) % n] {
count += 1;
}
}
count <= 1
let cnt = nums.iter().enumerate().fold(0, |cnt, (i, &x)| {
cnt + if x > nums[(i + 1) % n] { 1 } else { 0 }
});
cnt <= 1
}
}
```
Expand All @@ -162,13 +159,13 @@ impl Solution {

```c
bool check(int* nums, int numsSize) {
int count = 0;
int cnt = 0;
for (int i = 0; i < numsSize; i++) {
if (nums[i] > nums[(i + 1) % numsSize]) {
count++;
cnt++;
}
}
return count <= 1;
return cnt <= 1;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ You can rotate the array by x = 0 positions (i.e. no rotation) to make nums.

<!-- solution:start -->

### Solution 1
### Solution 1: Single Pass

To satisfy the problem's requirement, there can be at most one element in array $\textit{nums}$ whose value is greater than the next element, i.e., $nums[i] \gt nums[i + 1]$. If there are more than one such elements, then array $\textit{nums}$ cannot be obtained by rotation.

Note that the next element after the last element of array $\textit{nums}$ is the first element of array $\textit{nums}$.

The time complexity is $O(n)$, where $n$ is the length of array $\textit{nums}$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand All @@ -74,7 +80,7 @@ You can rotate the array by x = 0 positions (i.e. no rotation) to make nums.
```python
class Solution:
def check(self, nums: List[int]) -> bool:
return sum(nums[i - 1] > v for i, v in enumerate(nums)) <= 1
return sum(nums[i - 1] > x for i, x in enumerate(nums)) <= 1
```

#### Java
Expand Down Expand Up @@ -113,8 +119,8 @@ public:
```go
func check(nums []int) bool {
cnt := 0
for i, v := range nums {
if v > nums[(i+1)%len(nums)] {
for i, x := range nums {
if x > nums[(i+1)%len(nums)] {
cnt++
}
}
Expand All @@ -127,7 +133,7 @@ func check(nums []int) bool {
```ts
function check(nums: number[]): boolean {
const n = nums.length;
return nums.reduce((r, v, i) => r + (v > nums[(i + 1) % n] ? 1 : 0), 0) <= 1;
return nums.reduce((cnt, x, i) => cnt + (x > nums[(i + 1) % n] ? 1 : 0), 0) <= 1;
}
```

Expand All @@ -137,13 +143,10 @@ function check(nums: number[]): boolean {
impl Solution {
pub fn check(nums: Vec<i32>) -> bool {
let n = nums.len();
let mut count = 0;
for i in 0..n {
if nums[i] > nums[(i + 1) % n] {
count += 1;
}
}
count <= 1
let cnt = nums.iter().enumerate().fold(0, |cnt, (i, &x)| {
cnt + if x > nums[(i + 1) % n] { 1 } else { 0 }
});
cnt <= 1
}
}
```
Expand All @@ -152,13 +155,13 @@ impl Solution {

```c
bool check(int* nums, int numsSize) {
int count = 0;
int cnt = 0;
for (int i = 0; i < numsSize; i++) {
if (nums[i] > nums[(i + 1) % numsSize]) {
count++;
cnt++;
}
}
return count <= 1;
return cnt <= 1;
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
bool check(int* nums, int numsSize) {
int count = 0;
int cnt = 0;
for (int i = 0; i < numsSize; i++) {
if (nums[i] > nums[(i + 1) % numsSize]) {
count++;
cnt++;
}
}
return count <= 1;
return cnt <= 1;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
func check(nums []int) bool {
cnt := 0
for i, v := range nums {
if v > nums[(i+1)%len(nums)] {
for i, x := range nums {
if x > nums[(i+1)%len(nums)] {
cnt++
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Solution:
def check(self, nums: List[int]) -> bool:
return sum(nums[i - 1] > v for i, v in enumerate(nums)) <= 1
return sum(nums[i - 1] > x for i, x in enumerate(nums)) <= 1
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
impl Solution {
pub fn check(nums: Vec<i32>) -> bool {
let n = nums.len();
let mut count = 0;
for i in 0..n {
if nums[i] > nums[(i + 1) % n] {
count += 1;
}
}
count <= 1
let cnt = nums.iter().enumerate().fold(0, |cnt, (i, &x)| {
cnt + if x > nums[(i + 1) % n] { 1 } else { 0 }
});
cnt <= 1
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function check(nums: number[]): boolean {
const n = nums.length;
return nums.reduce((r, v, i) => r + (v > nums[(i + 1) % n] ? 1 : 0), 0) <= 1;
return nums.reduce((cnt, x, i) => cnt + (x > nums[(i + 1) % n] ? 1 : 0), 0) <= 1;
}
Loading