Skip to content

Commit ee7ddb9

Browse files
authored
feat: add solutions to lc problems: No.3151~3153 (doocs#2838)
* No.3151.Special Array I * No.3152.Special Array II * No.3153.Sum of Digit Differences of All Pairs
1 parent edb7ba7 commit ee7ddb9

21 files changed

+655
-24
lines changed

solution/3100-3199/3151.Special Array I/README.md

+49-4
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,77 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3151.Sp
7171

7272
<!-- solution:start -->
7373

74-
### 方法一
74+
### 方法一:一次遍历
75+
76+
我们从左到右遍历数组,对于每一对相邻元素,如果它们的奇偶性相同,那么数组就不是特殊数组,返回 `false`;否则,数组是特殊数组,返回 `true`
77+
78+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
7579

7680
<!-- tabs:start -->
7781

7882
#### Python3
7983

8084
```python
81-
85+
class Solution:
86+
def isArraySpecial(self, nums: List[int]) -> bool:
87+
return all(a % 2 != b % 2 for a, b in pairwise(nums))
8288
```
8389

8490
#### Java
8591

8692
```java
87-
93+
class Solution {
94+
public boolean isArraySpecial(int[] nums) {
95+
for (int i = 1; i < nums.length; ++i) {
96+
if (nums[i] % 2 == nums[i - 1] % 2) {
97+
return false;
98+
}
99+
}
100+
return true;
101+
}
102+
}
88103
```
89104

90105
#### C++
91106

92107
```cpp
93-
108+
class Solution {
109+
public:
110+
bool isArraySpecial(vector<int>& nums) {
111+
for (int i = 1; i < nums.size(); ++i) {
112+
if (nums[i] % 2 == nums[i - 1] % 2) {
113+
return false;
114+
}
115+
}
116+
return true;
117+
}
118+
};
94119
```
95120
96121
#### Go
97122
98123
```go
124+
func isArraySpecial(nums []int) bool {
125+
for i, x := range nums[1:] {
126+
if x%2 == nums[i]%2 {
127+
return false
128+
}
129+
}
130+
return true
131+
}
132+
```
99133

134+
#### TypeScript
135+
136+
```ts
137+
function isArraySpecial(nums: number[]): boolean {
138+
for (let i = 1; i < nums.length; ++i) {
139+
if (nums[i] % 2 === nums[i - 1] % 2) {
140+
return false;
141+
}
142+
}
143+
return true;
144+
}
100145
```
101146

102147
<!-- tabs:end -->

solution/3100-3199/3151.Special Array I/README_EN.md

+49-4
Original file line numberDiff line numberDiff line change
@@ -69,32 +69,77 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3151.Sp
6969

7070
<!-- solution:start -->
7171

72-
### Solution 1
72+
### Solution 1: Single Pass
73+
74+
We traverse the array from left to right. For each pair of adjacent elements, if their parity is the same, then the array is not a special array, return `false`; otherwise, the array is a special array, return `true`.
75+
76+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)`.
7377

7478
<!-- tabs:start -->
7579

7680
#### Python3
7781

7882
```python
79-
83+
class Solution:
84+
def isArraySpecial(self, nums: List[int]) -> bool:
85+
return all(a % 2 != b % 2 for a, b in pairwise(nums))
8086
```
8187

8288
#### Java
8389

8490
```java
85-
91+
class Solution {
92+
public boolean isArraySpecial(int[] nums) {
93+
for (int i = 1; i < nums.length; ++i) {
94+
if (nums[i] % 2 == nums[i - 1] % 2) {
95+
return false;
96+
}
97+
}
98+
return true;
99+
}
100+
}
86101
```
87102

88103
#### C++
89104

90105
```cpp
91-
106+
class Solution {
107+
public:
108+
bool isArraySpecial(vector<int>& nums) {
109+
for (int i = 1; i < nums.size(); ++i) {
110+
if (nums[i] % 2 == nums[i - 1] % 2) {
111+
return false;
112+
}
113+
}
114+
return true;
115+
}
116+
};
92117
```
93118
94119
#### Go
95120
96121
```go
122+
func isArraySpecial(nums []int) bool {
123+
for i, x := range nums[1:] {
124+
if x%2 == nums[i]%2 {
125+
return false
126+
}
127+
}
128+
return true
129+
}
130+
```
97131

132+
#### TypeScript
133+
134+
```ts
135+
function isArraySpecial(nums: number[]): boolean {
136+
for (let i = 1; i < nums.length; ++i) {
137+
if (nums[i] % 2 === nums[i - 1] % 2) {
138+
return false;
139+
}
140+
}
141+
return true;
142+
}
98143
```
99144

100145
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
bool isArraySpecial(vector<int>& nums) {
4+
for (int i = 1; i < nums.size(); ++i) {
5+
if (nums[i] % 2 == nums[i - 1] % 2) {
6+
return false;
7+
}
8+
}
9+
return true;
10+
}
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func isArraySpecial(nums []int) bool {
2+
for i, x := range nums[1:] {
3+
if x%2 == nums[i]%2 {
4+
return false
5+
}
6+
}
7+
return true
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public boolean isArraySpecial(int[] nums) {
3+
for (int i = 1; i < nums.length; ++i) {
4+
if (nums[i] % 2 == nums[i - 1] % 2) {
5+
return false;
6+
}
7+
}
8+
return true;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def isArraySpecial(self, nums: List[int]) -> bool:
3+
return all(a % 2 != b % 2 for a, b in pairwise(nums))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function isArraySpecial(nums: number[]): boolean {
2+
for (let i = 1; i < nums.length; ++i) {
3+
if (nums[i] % 2 === nums[i - 1] % 2) {
4+
return false;
5+
}
6+
}
7+
return true;
8+
}

solution/3100-3199/3152.Special Array II/README.md

+82-4
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,110 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3152.Sp
6767

6868
<!-- solution:start -->
6969

70-
### 方法一
70+
### 方法一:记录每个位置的最左特殊数组位置
71+
72+
我们可以定义一个数组 $d$ 来记录每个位置的最左特殊数组位置,初始时 $d[i] = i$。然后我们从左到右遍历数组 $nums$,如果 $nums[i]$ 和 $nums[i - 1]$ 的奇偶性不同,那么 $d[i] = d[i - 1]$。
73+
74+
最后我们遍历每个查询,判断 $d[to] <= from$ 是否成立即可。
75+
76+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。
7177

7278
<!-- tabs:start -->
7379

7480
#### Python3
7581

7682
```python
77-
83+
class Solution:
84+
def isArraySpecial(self, nums: List[int], queries: List[List[int]]) -> List[bool]:
85+
n = len(nums)
86+
d = list(range(n))
87+
for i in range(1, n):
88+
if nums[i] % 2 != nums[i - 1] % 2:
89+
d[i] = d[i - 1]
90+
return [d[t] <= f for f, t in queries]
7891
```
7992

8093
#### Java
8194

8295
```java
83-
96+
class Solution {
97+
public boolean[] isArraySpecial(int[] nums, int[][] queries) {
98+
int n = nums.length;
99+
int[] d = new int[n];
100+
for (int i = 1; i < n; ++i) {
101+
if (nums[i] % 2 != nums[i - 1] % 2) {
102+
d[i] = d[i - 1];
103+
} else {
104+
d[i] = i;
105+
}
106+
}
107+
int m = queries.length;
108+
boolean[] ans = new boolean[m];
109+
for (int i = 0; i < m; ++i) {
110+
ans[i] = d[queries[i][1]] <= queries[i][0];
111+
}
112+
return ans;
113+
}
114+
}
84115
```
85116

86117
#### C++
87118

88119
```cpp
89-
120+
class Solution {
121+
public:
122+
vector<bool> isArraySpecial(vector<int>& nums, vector<vector<int>>& queries) {
123+
int n = nums.size();
124+
vector<int> d(n);
125+
iota(d.begin(), d.end(), 0);
126+
for (int i = 1; i < n; ++i) {
127+
if (nums[i] % 2 != nums[i - 1] % 2) {
128+
d[i] = d[i - 1];
129+
}
130+
}
131+
vector<bool> ans;
132+
for (auto& q : queries) {
133+
ans.push_back(d[q[1]] <= q[0]);
134+
}
135+
return ans;
136+
}
137+
};
90138
```
91139
92140
#### Go
93141
94142
```go
143+
func isArraySpecial(nums []int, queries [][]int) (ans []bool) {
144+
n := len(nums)
145+
d := make([]int, n)
146+
for i := range d {
147+
d[i] = i
148+
}
149+
for i := 1; i < len(nums); i++ {
150+
if nums[i]%2 != nums[i-1]%2 {
151+
d[i] = d[i-1]
152+
}
153+
}
154+
for _, q := range queries {
155+
ans = append(ans, d[q[1]] <= q[0])
156+
}
157+
return
158+
}
159+
```
95160

161+
#### TypeScript
162+
163+
```ts
164+
function isArraySpecial(nums: number[], queries: number[][]): boolean[] {
165+
const n = nums.length;
166+
const d: number[] = Array.from({ length: n }, (_, i) => i);
167+
for (let i = 1; i < n; ++i) {
168+
if (nums[i] % 2 !== nums[i - 1] % 2) {
169+
d[i] = d[i - 1];
170+
}
171+
}
172+
return queries.map(([from, to]) => d[to] <= from);
173+
}
96174
```
97175

98176
<!-- tabs:end -->

0 commit comments

Comments
 (0)