|
47 | 47 |
|
48 | 48 | <!-- 这里可写通用的实现逻辑 -->
|
49 | 49 |
|
| 50 | +**方法一:乘法原理** |
| 51 | + |
| 52 | +根据题目描述,我们可以在两个 $1$ 之间画一条分割线,假设两个 $1$ 之间的下标分别为 $j$ 和 $i$,那么可以画的不同分割线的数量为 $i - j$。我们找出所有满足条件的 $j$ 和 $i$,然后将所有的 $i - j$ 相乘即可。如果找不到两个 $1$ 之间的分割线,那么说明数组中不存在 $1$,此时答案为 $0$。 |
| 53 | + |
| 54 | +时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。 |
| 55 | + |
50 | 56 | <!-- tabs:start -->
|
51 | 57 |
|
52 | 58 | ### **Python3**
|
53 | 59 |
|
54 | 60 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
55 | 61 |
|
56 | 62 | ```python
|
57 |
| - |
| 63 | +class Solution: |
| 64 | + def numberOfGoodSubarraySplits(self, nums: List[int]) -> int: |
| 65 | + mod = 10**9 + 7 |
| 66 | + ans, j = 1, -1 |
| 67 | + for i, x in enumerate(nums): |
| 68 | + if x == 0: |
| 69 | + continue |
| 70 | + if j > -1: |
| 71 | + ans = ans * (i - j) % mod |
| 72 | + j = i |
| 73 | + return 0 if j == -1 else ans |
58 | 74 | ```
|
59 | 75 |
|
60 | 76 | ### **Java**
|
61 | 77 |
|
62 | 78 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
63 | 79 |
|
64 | 80 | ```java
|
65 |
| - |
| 81 | +class Solution { |
| 82 | + public int numberOfGoodSubarraySplits(int[] nums) { |
| 83 | + final int mod = (int) 1e9 + 7; |
| 84 | + int ans = 1, j = -1; |
| 85 | + for (int i = 0; i < nums.length; ++i) { |
| 86 | + if (nums[i] == 0) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + if (j > -1) { |
| 90 | + ans = (int) ((long) ans * (i - j) % mod); |
| 91 | + } |
| 92 | + j = i; |
| 93 | + } |
| 94 | + return j == -1 ? 0 : ans; |
| 95 | + } |
| 96 | +} |
66 | 97 | ```
|
67 | 98 |
|
68 | 99 | ### **C++**
|
69 | 100 |
|
70 | 101 | ```cpp
|
71 |
| - |
| 102 | +class Solution { |
| 103 | +public: |
| 104 | + int numberOfGoodSubarraySplits(vector<int>& nums) { |
| 105 | + const int mod = 1e9 + 7; |
| 106 | + int ans = 1, j = -1; |
| 107 | + for (int i = 0; i < nums.size(); ++i) { |
| 108 | + if (nums[i] == 0) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + if (j > -1) { |
| 112 | + ans = 1LL * ans * (i - j) % mod; |
| 113 | + } |
| 114 | + j = i; |
| 115 | + } |
| 116 | + return j == -1 ? 0 : ans; |
| 117 | + } |
| 118 | +}; |
72 | 119 | ```
|
73 | 120 |
|
74 | 121 | ### **Go**
|
75 | 122 |
|
76 | 123 | ```go
|
| 124 | +func numberOfGoodSubarraySplits(nums []int) int { |
| 125 | + const mod int = 1e9 + 7 |
| 126 | + ans, j := 1, -1 |
| 127 | + for i, x := range nums { |
| 128 | + if x == 0 { |
| 129 | + continue |
| 130 | + } |
| 131 | + if j > -1 { |
| 132 | + ans = ans * (i - j) % mod |
| 133 | + } |
| 134 | + j = i |
| 135 | + } |
| 136 | + if j == -1 { |
| 137 | + return 0 |
| 138 | + } |
| 139 | + return ans |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +### **TypeScript** |
| 144 | + |
| 145 | +```ts |
| 146 | +function numberOfGoodSubarraySplits(nums: number[]): number { |
| 147 | + let ans = 1; |
| 148 | + let j = -1; |
| 149 | + const mod = 10 ** 9 + 7; |
| 150 | + const n = nums.length; |
| 151 | + for (let i = 0; i < n; ++i) { |
| 152 | + if (nums[i] === 0) { |
| 153 | + continue; |
| 154 | + } |
| 155 | + if (j > -1) { |
| 156 | + ans = (ans * (i - j)) % mod; |
| 157 | + } |
| 158 | + j = i; |
| 159 | + } |
| 160 | + return j === -1 ? 0 : ans; |
| 161 | +} |
| 162 | +``` |
77 | 163 |
|
| 164 | +### **C#** |
| 165 | + |
| 166 | +```cs |
| 167 | +public class Solution { |
| 168 | + public int NumberOfGoodSubarraySplits(int[] nums) { |
| 169 | + long ans = 1, j = -1; |
| 170 | + int mod = 1000000007; |
| 171 | + int n = nums.Length; |
| 172 | + for (int i = 0; i < n; ++i) { |
| 173 | + if (nums[i] == 0) { |
| 174 | + continue; |
| 175 | + } |
| 176 | + if (j > -1) { |
| 177 | + ans = ans * (i - j) % mod; |
| 178 | + } |
| 179 | + j = i; |
| 180 | + } |
| 181 | + return j == -1 ? 0 : (int) ans; |
| 182 | + } |
| 183 | +} |
78 | 184 | ```
|
79 | 185 |
|
80 | 186 | ### **...**
|
|
0 commit comments