Skip to content

Commit 3307f4b

Browse files
committed
feat: add solutions to lc problem: No.1752
No.1752.Check if Array Is Sorted and Rotated
1 parent 54e6497 commit 3307f4b

File tree

10 files changed

+156
-11
lines changed

10 files changed

+156
-11
lines changed

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,75 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:一次遍历**
59+
60+
要满足题目要求,那么数组 `nums` 中最多只能存在一个元素,其值大于下一个元素,即 `nums[i] > nums[i + 1]`。如果存在多个这样的元素,那么数组 `nums` 无法通过轮转得到。
61+
62+
注意,数组 `nums` 最后一个元素的下一个元素是数组 `nums` 的第一个元素。
63+
64+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
65+
5866
<!-- tabs:start -->
5967

6068
### **Python3**
6169

6270
<!-- 这里可写当前语言的特殊实现逻辑 -->
6371

6472
```python
65-
73+
class Solution:
74+
def check(self, nums: List[int]) -> bool:
75+
n = len(nums)
76+
return sum(v > nums[(i + 1) % n] for i, v in enumerate(nums)) <= 1
6677
```
6778

6879
### **Java**
6980

7081
<!-- 这里可写当前语言的特殊实现逻辑 -->
7182

7283
```java
84+
class Solution {
85+
public boolean check(int[] nums) {
86+
int n = nums.length;
87+
int cnt = 0;
88+
for (int i = 0; i < n; ++i) {
89+
if (nums[i] > nums[(i + 1) % n]) {
90+
++cnt;
91+
}
92+
}
93+
return cnt <= 1;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
bool check(vector<int>& nums) {
104+
int n = nums.size();
105+
int cnt = 0;
106+
for (int i = 0; i < n; ++i) {
107+
cnt += nums[i] > (nums[(i + 1) % n]);
108+
}
109+
return cnt <= 1;
110+
}
111+
};
112+
```
73113
114+
### **Go**
115+
116+
```go
117+
func check(nums []int) bool {
118+
n := len(nums)
119+
cnt := 0
120+
for i, v := range nums {
121+
if v > nums[(i+1)%n] {
122+
cnt++
123+
}
124+
}
125+
return cnt <= 1
126+
}
74127
```
75128

76129
### **...**

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,58 @@ You can rotate the array by x = 0 positions (i.e. no rotation) to make nums.
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def check(self, nums: List[int]) -> bool:
57+
n = len(nums)
58+
return sum(v > nums[(i + 1) % n] for i, v in enumerate(nums)) <= 1
5659
```
5760

5861
### **Java**
5962

6063
```java
64+
class Solution {
65+
public boolean check(int[] nums) {
66+
int n = nums.length;
67+
int cnt = 0;
68+
for (int i = 0; i < n; ++i) {
69+
if (nums[i] > nums[(i + 1) % n]) {
70+
++cnt;
71+
}
72+
}
73+
return cnt <= 1;
74+
}
75+
}
76+
```
77+
78+
### **C++**
79+
80+
```cpp
81+
class Solution {
82+
public:
83+
bool check(vector<int>& nums) {
84+
int n = nums.size();
85+
int cnt = 0;
86+
for (int i = 0; i < n; ++i) {
87+
cnt += nums[i] > (nums[(i + 1) % n]);
88+
}
89+
return cnt <= 1;
90+
}
91+
};
92+
```
6193
94+
### **Go**
95+
96+
```go
97+
func check(nums []int) bool {
98+
n := len(nums)
99+
cnt := 0
100+
for i, v := range nums {
101+
if v > nums[(i+1)%n] {
102+
cnt++
103+
}
104+
}
105+
return cnt <= 1
106+
}
62107
```
63108

64109
### **...**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
bool check(vector<int>& nums) {
4+
int n = nums.size();
5+
int cnt = 0;
6+
for (int i = 0; i < n; ++i) {
7+
cnt += nums[i] > (nums[(i + 1) % n]);
8+
}
9+
return cnt <= 1;
10+
}
11+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func check(nums []int) bool {
2+
n := len(nums)
3+
cnt := 0
4+
for i, v := range nums {
5+
if v > nums[(i+1)%n] {
6+
cnt++
7+
}
8+
}
9+
return cnt <= 1
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public boolean check(int[] nums) {
3+
int n = nums.length;
4+
int cnt = 0;
5+
for (int i = 0; i < n; ++i) {
6+
if (nums[i] > nums[(i + 1) % n]) {
7+
++cnt;
8+
}
9+
}
10+
return cnt <= 1;
11+
}
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def check(self, nums: List[int]) -> bool:
3+
n = len(nums)
4+
return sum(v > nums[(i + 1) % n] for i, v in enumerate(nums)) <= 1

solution/2400-2499/2425.Bitwise XOR of All Pairings/Solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ function xorAllNums(nums1: number[], nums2: number[]): number {
77
ans ^= nums2.reduce((a, c) => a ^ c, 0);
88
}
99
return ans;
10-
};
10+
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
function maxSum(grid: number[][]): number {
2-
const m = grid.length, n = grid[0].length;
2+
const m = grid.length,
3+
n = grid[0].length;
34
let threeSum = Array.from({ length: m }, () => new Array(n - 2).fill(0));
45
for (let i = 0; i < m; i++) {
56
for (let j = 1; j < n - 1; j++) {
@@ -9,8 +10,11 @@ function maxSum(grid: number[][]): number {
910
let ans = 0;
1011
for (let i = 1; i < m - 1; i++) {
1112
for (let j = 1; j < n - 1; j++) {
12-
ans = Math.max(ans, threeSum[i - 1][j - 1] + grid[i][j] + threeSum[i + 1][j - 1]);
13+
ans = Math.max(
14+
ans,
15+
threeSum[i - 1][j - 1] + grid[i][j] + threeSum[i + 1][j - 1],
16+
);
1317
}
1418
}
1519
return ans;
16-
};
20+
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
function minimizeXor(num1: number, num2: number): number {
22
let ans = num1;
3-
let target = num1.toString(2).split('').reduce((a, c) => a + Number(c), 0);
4-
let count = num2.toString(2).split('').reduce((a, c) => a + Number(c), 0);
3+
let target = num1
4+
.toString(2)
5+
.split('')
6+
.reduce((a, c) => a + Number(c), 0);
7+
let count = num2
8+
.toString(2)
9+
.split('')
10+
.reduce((a, c) => a + Number(c), 0);
511
while (count > target) {
612
ans |= ans + 1;
713
count -= 1;
@@ -11,4 +17,4 @@ function minimizeXor(num1: number, num2: number): number {
1117
count += 1;
1218
}
1319
return ans;
14-
};
20+
}

solution/2400-2499/2437.Number of Valid Clock Times/Solution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function countTime(time: string): number {
22
let [hh, mm] = time.split(':');
33
return count(hh, 24) * count(mm, 60);
4-
};
4+
}
55

66
function count(str: string, limit: number): number {
77
let [a, b] = str.split('').map(d => Number(d));
@@ -20,4 +20,4 @@ function count(str: string, limit: number): number {
2020
return ans;
2121
}
2222
return 1;
23-
}
23+
}

0 commit comments

Comments
 (0)