Skip to content

[pull] main from doocs:main #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ee7ddb9
feat: add solutions to lc problems: No.3151~3153 (#2838)
yanglbme May 19, 2024
3f6a34f
feat: add solutions to lc problem: No.3154 (#2842)
yanglbme May 19, 2024
51b984f
feat: add cpp solution to lc problem: No.3068 (#2839)
AlleyNawaz May 19, 2024
f55b0d7
feat: add cpp solution to lc problem: No.3068 (#2840)
AlleyNawaz May 19, 2024
ac3102d
feat: add cpp solution to lc problem: No.3068 (#2841)
AlleyNawaz May 19, 2024
818daa9
feat: add solutions to lc problem: No.3155 (#2843)
yanglbme May 19, 2024
06533ad
feat: add solutions to lc problem: No.1317 (#2844)
yanglbme May 19, 2024
41ebef4
refactor: update solution with sorting via object to lc problem: No.2…
rain84 May 20, 2024
aa8ed86
refactor: update solution to lc problem: No.2625 (#2848)
rain84 May 20, 2024
dfb0297
feat: add ts solution to lc problem: No.1542 (#2850)
yanglbme May 20, 2024
5e67f9f
feat: update solutions to lc problem: No.2705 (#2851)
yanglbme May 20, 2024
5301c76
feat: update lc problems (#2852)
yanglbme May 20, 2024
4db3cd4
feat: add swift implementation to lcof problem: No.06 (#2853)
klever34 May 20, 2024
d2ee6aa
feat: add swift implementation to lcof problem: No.07 (#2854)
klever34 May 20, 2024
5cc6315
feat: add swift implementation to lcof problem: No.09 (#2855)
klever34 May 20, 2024
d451882
feat: add swift implementation to lcof problem: No.10.1 (#2856)
klever34 May 20, 2024
98beaf4
feat: add swift implementation to lcof problem: No.10.2 (#2857)
klever34 May 20, 2024
5f6a3af
feat: add swift implementation to lcof problem: No.11 (#2858)
klever34 May 20, 2024
df9bd5d
feat: add sql solution to lc problem: No.3156 (#2859)
yanglbme May 20, 2024
14c8085
feat: add solutions to lcp problem: No.80 (#2860)
yanglbme May 20, 2024
feae0f6
feat: update solutions to lc/lcof problems (#2861)
yanglbme May 21, 2024
27036e8
feat: update solutions to lcof problem: No.15 (#2862)
yanglbme May 21, 2024
6bb333d
--- (#2863)
dependabot[bot] May 21, 2024
a9caf56
feat: add swift implementation to lcof problem: No.12 (#2864)
klever34 May 21, 2024
d449a8a
feat: add swift implementation to lcof problem: No.13 (#2865)
klever34 May 21, 2024
7796a59
feat: add swift implementation to lcof problem: No.14.1 (#2866)
klever34 May 21, 2024
fc09c8c
feat: add swift implementation to lcof problem: No.14.2 (#2867)
klever34 May 21, 2024
d6c9b4f
feat: add swift implementation to lcof problem: No.15 (#2868)
klever34 May 21, 2024
c954041
feat: add swift implementation to lcof problem: No.16 (#2869)
klever34 May 21, 2024
3c5c2d6
feat: add solutions to lc problem: No.2831 (#2870)
yanglbme May 21, 2024
3768553
feat: update solutions to lcof problems: No.26,27,30 (#2871)
yanglbme May 21, 2024
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
Next Next commit
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
  • Loading branch information
yanglbme authored May 19, 2024
commit ee7ddb904280bb224dee2531d857e060caefe34a
53 changes: 49 additions & 4 deletions solution/3100-3199/3151.Special Array I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,77 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3151.Sp

<!-- solution:start -->

### 方法一
### 方法一:一次遍历

我们从左到右遍历数组,对于每一对相邻元素,如果它们的奇偶性相同,那么数组就不是特殊数组,返回 `false`;否则,数组是特殊数组,返回 `true`。

时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def isArraySpecial(self, nums: List[int]) -> bool:
return all(a % 2 != b % 2 for a, b in pairwise(nums))
```

#### Java

```java

class Solution {
public boolean isArraySpecial(int[] nums) {
for (int i = 1; i < nums.length; ++i) {
if (nums[i] % 2 == nums[i - 1] % 2) {
return false;
}
}
return true;
}
}
```

#### C++

```cpp

class Solution {
public:
bool isArraySpecial(vector<int>& nums) {
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] % 2 == nums[i - 1] % 2) {
return false;
}
}
return true;
}
};
```

#### Go

```go
func isArraySpecial(nums []int) bool {
for i, x := range nums[1:] {
if x%2 == nums[i]%2 {
return false
}
}
return true
}
```

#### TypeScript

```ts
function isArraySpecial(nums: number[]): boolean {
for (let i = 1; i < nums.length; ++i) {
if (nums[i] % 2 === nums[i - 1] % 2) {
return false;
}
}
return true;
}
```

<!-- tabs:end -->
Expand Down
53 changes: 49 additions & 4 deletions solution/3100-3199/3151.Special Array I/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,77 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3151.Sp

<!-- solution:start -->

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

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`.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)`.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def isArraySpecial(self, nums: List[int]) -> bool:
return all(a % 2 != b % 2 for a, b in pairwise(nums))
```

#### Java

```java

class Solution {
public boolean isArraySpecial(int[] nums) {
for (int i = 1; i < nums.length; ++i) {
if (nums[i] % 2 == nums[i - 1] % 2) {
return false;
}
}
return true;
}
}
```

#### C++

```cpp

class Solution {
public:
bool isArraySpecial(vector<int>& nums) {
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] % 2 == nums[i - 1] % 2) {
return false;
}
}
return true;
}
};
```

#### Go

```go
func isArraySpecial(nums []int) bool {
for i, x := range nums[1:] {
if x%2 == nums[i]%2 {
return false
}
}
return true
}
```

#### TypeScript

```ts
function isArraySpecial(nums: number[]): boolean {
for (let i = 1; i < nums.length; ++i) {
if (nums[i] % 2 === nums[i - 1] % 2) {
return false;
}
}
return true;
}
```

<!-- tabs:end -->
Expand Down
11 changes: 11 additions & 0 deletions solution/3100-3199/3151.Special Array I/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public:
bool isArraySpecial(vector<int>& nums) {
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] % 2 == nums[i - 1] % 2) {
return false;
}
}
return true;
}
};
8 changes: 8 additions & 0 deletions solution/3100-3199/3151.Special Array I/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
func isArraySpecial(nums []int) bool {
for i, x := range nums[1:] {
if x%2 == nums[i]%2 {
return false
}
}
return true
}
10 changes: 10 additions & 0 deletions solution/3100-3199/3151.Special Array I/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution {
public boolean isArraySpecial(int[] nums) {
for (int i = 1; i < nums.length; ++i) {
if (nums[i] % 2 == nums[i - 1] % 2) {
return false;
}
}
return true;
}
}
3 changes: 3 additions & 0 deletions solution/3100-3199/3151.Special Array I/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def isArraySpecial(self, nums: List[int]) -> bool:
return all(a % 2 != b % 2 for a, b in pairwise(nums))
8 changes: 8 additions & 0 deletions solution/3100-3199/3151.Special Array I/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function isArraySpecial(nums: number[]): boolean {
for (let i = 1; i < nums.length; ++i) {
if (nums[i] % 2 === nums[i - 1] % 2) {
return false;
}
}
return true;
}
86 changes: 82 additions & 4 deletions solution/3100-3199/3152.Special Array II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,110 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3152.Sp

<!-- solution:start -->

### 方法一
### 方法一:记录每个位置的最左特殊数组位置

我们可以定义一个数组 $d$ 来记录每个位置的最左特殊数组位置,初始时 $d[i] = i$。然后我们从左到右遍历数组 $nums$,如果 $nums[i]$ 和 $nums[i - 1]$ 的奇偶性不同,那么 $d[i] = d[i - 1]$。

最后我们遍历每个查询,判断 $d[to] <= from$ 是否成立即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def isArraySpecial(self, nums: List[int], queries: List[List[int]]) -> List[bool]:
n = len(nums)
d = list(range(n))
for i in range(1, n):
if nums[i] % 2 != nums[i - 1] % 2:
d[i] = d[i - 1]
return [d[t] <= f for f, t in queries]
```

#### Java

```java

class Solution {
public boolean[] isArraySpecial(int[] nums, int[][] queries) {
int n = nums.length;
int[] d = new int[n];
for (int i = 1; i < n; ++i) {
if (nums[i] % 2 != nums[i - 1] % 2) {
d[i] = d[i - 1];
} else {
d[i] = i;
}
}
int m = queries.length;
boolean[] ans = new boolean[m];
for (int i = 0; i < m; ++i) {
ans[i] = d[queries[i][1]] <= queries[i][0];
}
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
vector<bool> isArraySpecial(vector<int>& nums, vector<vector<int>>& queries) {
int n = nums.size();
vector<int> d(n);
iota(d.begin(), d.end(), 0);
for (int i = 1; i < n; ++i) {
if (nums[i] % 2 != nums[i - 1] % 2) {
d[i] = d[i - 1];
}
}
vector<bool> ans;
for (auto& q : queries) {
ans.push_back(d[q[1]] <= q[0]);
}
return ans;
}
};
```

#### Go

```go
func isArraySpecial(nums []int, queries [][]int) (ans []bool) {
n := len(nums)
d := make([]int, n)
for i := range d {
d[i] = i
}
for i := 1; i < len(nums); i++ {
if nums[i]%2 != nums[i-1]%2 {
d[i] = d[i-1]
}
}
for _, q := range queries {
ans = append(ans, d[q[1]] <= q[0])
}
return
}
```

#### TypeScript

```ts
function isArraySpecial(nums: number[], queries: number[][]): boolean[] {
const n = nums.length;
const d: number[] = Array.from({ length: n }, (_, i) => i);
for (let i = 1; i < n; ++i) {
if (nums[i] % 2 !== nums[i - 1] % 2) {
d[i] = d[i - 1];
}
}
return queries.map(([from, to]) => d[to] <= from);
}
```

<!-- tabs:end -->
Expand Down
Loading