Skip to content
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

Dev lc2750 #1077

Merged
merged 2 commits into from
Jun 26, 2023
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 @@ -47,34 +47,140 @@

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

**方法一:乘法原理**

根据题目描述,我们可以在两个 $1$ 之间画一条分割线,假设两个 $1$ 之间的下标分别为 $j$ 和 $i$,那么可以画的不同分割线的数量为 $i - j$。我们找出所有满足条件的 $j$ 和 $i$,然后将所有的 $i - j$ 相乘即可。如果找不到两个 $1$ 之间的分割线,那么说明数组中不存在 $1$,此时答案为 $0$。

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

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:
mod = 10**9 + 7
ans, j = 1, -1
for i, x in enumerate(nums):
if x == 0:
continue
if j > -1:
ans = ans * (i - j) % mod
j = i
return 0 if j == -1 else ans
```

### **Java**

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

```java

class Solution {
public int numberOfGoodSubarraySplits(int[] nums) {
final int mod = (int) 1e9 + 7;
int ans = 1, j = -1;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] == 0) {
continue;
}
if (j > -1) {
ans = (int) ((long) ans * (i - j) % mod);
}
j = i;
}
return j == -1 ? 0 : ans;
}
}
```

### **C++**

```cpp

class Solution {
public:
int numberOfGoodSubarraySplits(vector<int>& nums) {
const int mod = 1e9 + 7;
int ans = 1, j = -1;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] == 0) {
continue;
}
if (j > -1) {
ans = 1LL * ans * (i - j) % mod;
}
j = i;
}
return j == -1 ? 0 : ans;
}
};
```

### **Go**

```go
func numberOfGoodSubarraySplits(nums []int) int {
const mod int = 1e9 + 7
ans, j := 1, -1
for i, x := range nums {
if x == 0 {
continue
}
if j > -1 {
ans = ans * (i - j) % mod
}
j = i
}
if j == -1 {
return 0
}
return ans
}
```

### **TypeScript**

```ts
function numberOfGoodSubarraySplits(nums: number[]): number {
let ans = 1;
let j = -1;
const mod = 10 ** 9 + 7;
const n = nums.length;
for (let i = 0; i < n; ++i) {
if (nums[i] === 0) {
continue;
}
if (j > -1) {
ans = (ans * (i - j)) % mod;
}
j = i;
}
return j === -1 ? 0 : ans;
}
```

### **C#**

```cs
public class Solution {
public int NumberOfGoodSubarraySplits(int[] nums) {
long ans = 1, j = -1;
int mod = 1000000007;
int n = nums.Length;
for (int i = 0; i < n; ++i) {
if (nums[i] == 0) {
continue;
}
if (j > -1) {
ans = ans * (i - j) % mod;
}
j = i;
}
return j == -1 ? 0 : (int) ans;
}
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,125 @@
### **Python3**

```python

class Solution:
def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:
mod = 10**9 + 7
ans, j = 1, -1
for i, x in enumerate(nums):
if x == 0:
continue
if j > -1:
ans = ans * (i - j) % mod
j = i
return 0 if j == -1 else ans
```

### **Java**

```java

class Solution {
public int numberOfGoodSubarraySplits(int[] nums) {
final int mod = (int) 1e9 + 7;
int ans = 1, j = -1;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] == 0) {
continue;
}
if (j > -1) {
ans = (int) ((long) ans * (i - j) % mod);
}
j = i;
}
return j == -1 ? 0 : ans;
}
}
```

### **C++**

```cpp

class Solution {
public:
int numberOfGoodSubarraySplits(vector<int>& nums) {
const int mod = 1e9 + 7;
int ans = 1, j = -1;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] == 0) {
continue;
}
if (j > -1) {
ans = 1LL * ans * (i - j) % mod;
}
j = i;
}
return j == -1 ? 0 : ans;
}
};
```

### **Go**

```go
func numberOfGoodSubarraySplits(nums []int) int {
const mod int = 1e9 + 7
ans, j := 1, -1
for i, x := range nums {
if x == 0 {
continue
}
if j > -1 {
ans = ans * (i - j) % mod
}
j = i
}
if j == -1 {
return 0
}
return ans
}
```

### **TypeScript**

```ts
function numberOfGoodSubarraySplits(nums: number[]): number {
let ans = 1;
let j = -1;
const mod = 10 ** 9 + 7;
const n = nums.length;
for (let i = 0; i < n; ++i) {
if (nums[i] === 0) {
continue;
}
if (j > -1) {
ans = (ans * (i - j)) % mod;
}
j = i;
}
return j === -1 ? 0 : ans;
}
```

### **C#**

```cs
public class Solution {
public int NumberOfGoodSubarraySplits(int[] nums) {
long ans = 1, j = -1;
int mod = 1000000007;
int n = nums.Length;
for (int i = 0; i < n; ++i) {
if (nums[i] == 0) {
continue;
}
if (j > -1) {
ans = ans * (i - j) % mod;
}
j = i;
}
return j == -1 ? 0 : (int) ans;
}
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int numberOfGoodSubarraySplits(vector<int>& nums) {
const int mod = 1e9 + 7;
int ans = 1, j = -1;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] == 0) {
continue;
}
if (j > -1) {
ans = 1LL * ans * (i - j) % mod;
}
j = i;
}
return j == -1 ? 0 : ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class Solution {
public int NumberOfGoodSubarraySplits(int[] nums) {
long ans = 1, j = -1;
int mod = 1000000007;
int n = nums.Length;
for (int i = 0; i < n; ++i) {
if (nums[i] == 0) {
continue;
}
if (j > -1) {
ans = ans * (i - j) % mod;
}
j = i;
}
return j == -1 ? 0 : (int) ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
func numberOfGoodSubarraySplits(nums []int) int {
const mod int = 1e9 + 7
ans, j := 1, -1
for i, x := range nums {
if x == 0 {
continue
}
if j > -1 {
ans = ans * (i - j) % mod
}
j = i
}
if j == -1 {
return 0
}
return ans
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int numberOfGoodSubarraySplits(int[] nums) {
final int mod = (int) 1e9 + 7;
int ans = 1, j = -1;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] == 0) {
continue;
}
if (j > -1) {
ans = (int) ((long) ans * (i - j) % mod);
}
j = i;
}
return j == -1 ? 0 : ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:
mod = 10**9 + 7
ans, j = 1, -1
for i, x in enumerate(nums):
if x == 0:
continue
if j > -1:
ans = ans * (i - j) % mod
j = i
return 0 if j == -1 else ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function numberOfGoodSubarraySplits(nums: number[]): number {
let ans = 1;
let j = -1;
const mod = 10 ** 9 + 7;
const n = nums.length;
for (let i = 0; i < n; ++i) {
if (nums[i] === 0) {
continue;
}
if (j > -1) {
ans = (ans * (i - j)) % mod;
}
j = i;
}
return j === -1 ? 0 : ans;
}