Skip to content

Commit 911ae90

Browse files
committed
LeetCode 题解 1572, 1573
1 parent 7cb6799 commit 911ae90

File tree

6 files changed

+365
-0
lines changed

6 files changed

+365
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# [1572. 矩阵对角线元素的和](https://leetcode-cn.com/problems/matrix-diagonal-sum)
2+
3+
[English Version](/solution/1500-1599/1572.Matrix Diagonal Sum/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
<p>给你一个正方形矩阵 <code>mat</code>,请你返回矩阵对角线元素的和。</p>
9+
10+
<p>请你返回在矩阵主对角线上的元素和副对角线上且不在主对角线上元素的和。</p>
11+
12+
<p>&nbsp;</p>
13+
14+
<p><strong>示例&nbsp; 1:</strong></p>
15+
16+
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/08/14/sample_1911.png" style="height:174px; width:336px" /></p>
17+
18+
<pre>
19+
<strong>输入:</strong>mat = [[<strong>1</strong>,2,<strong>3</strong>],
20+
&nbsp; [4,<strong>5</strong>,6],
21+
&nbsp; [<strong>7</strong>,8,<strong>9</strong>]]
22+
<strong>输出:</strong>25
23+
<strong>解释:</strong>对角线的和为:1 + 5 + 9 + 3 + 7 = 25
24+
请注意,元素 mat[1][1] = 5 只会被计算一次。
25+
</pre>
26+
27+
<p><strong>示例&nbsp; 2:</strong></p>
28+
29+
<pre>
30+
<strong>输入:</strong>mat = [[<strong>1</strong>,1,1,<strong>1</strong>],
31+
&nbsp; [1,<strong>1</strong>,<strong>1</strong>,1],
32+
&nbsp; [1,<strong>1</strong>,<strong>1</strong>,1],
33+
&nbsp; [<strong>1</strong>,1,1,<strong>1</strong>]]
34+
<strong>输出:</strong>8
35+
</pre>
36+
37+
<p><strong>示例 3:</strong></p>
38+
39+
<pre>
40+
<strong>输入:</strong>mat = [[<strong>5</strong>]]
41+
<strong>输出:</strong>5
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
46+
<p><strong>提示:</strong></p>
47+
48+
<ul>
49+
<li><code>n == mat.length == mat[i].length</code></li>
50+
<li><code>1 &lt;= n &lt;= 100</code></li>
51+
<li><code>1 &lt;= mat[i][j] &lt;= 100</code></li>
52+
</ul>
53+
54+
55+
56+
## 解法
57+
58+
<!-- 这里可写通用的实现逻辑 -->
59+
60+
61+
<!-- tabs:start -->
62+
63+
### **Python3**
64+
65+
<!-- 这里可写当前语言的特殊实现逻辑 -->
66+
67+
```python
68+
69+
```
70+
71+
### **Java**
72+
73+
<!-- 这里可写当前语言的特殊实现逻辑 -->
74+
75+
```java
76+
77+
```
78+
79+
### **...**
80+
```
81+
82+
```
83+
84+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [1572. Matrix Diagonal Sum](https://leetcode.com/problems/matrix-diagonal-sum)
2+
3+
[中文文档](/solution/1500-1599/1572.Matrix Diagonal Sum/README.md)
4+
5+
## Description
6+
7+
<p>Given a&nbsp;square&nbsp;matrix&nbsp;<code>mat</code>, return the sum of the matrix diagonals.</p>
8+
9+
<p>Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong>Example 1:</strong></p>
13+
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/14/sample_1911.png" style="width: 336px; height: 174px;" />
14+
<pre>
15+
<strong>Input:</strong> mat = [[<strong>1</strong>,2,<strong>3</strong>],
16+
&nbsp; [4,<strong>5</strong>,6],
17+
&nbsp; [<strong>7</strong>,8,<strong>9</strong>]]
18+
<strong>Output:</strong> 25
19+
<strong>Explanation: </strong>Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
20+
Notice that element mat[1][1] = 5 is counted only once.
21+
</pre>
22+
23+
<p><strong>Example 2:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> mat = [[<strong>1</strong>,1,1,<strong>1</strong>],
27+
&nbsp; [1,<strong>1</strong>,<strong>1</strong>,1],
28+
&nbsp; [1,<strong>1</strong>,<strong>1</strong>,1],
29+
&nbsp; [<strong>1</strong>,1,1,<strong>1</strong>]]
30+
<strong>Output:</strong> 8
31+
</pre>
32+
33+
<p><strong>Example 3:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> mat = [[<strong>5</strong>]]
37+
<strong>Output:</strong> 5
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>n == mat.length == mat[i].length</code></li>
45+
<li><code>1 &lt;= n &lt;= 100</code></li>
46+
<li><code>1 &lt;= mat[i][j] &lt;= 100</code></li>
47+
</ul>
48+
49+
50+
## Solutions
51+
52+
53+
54+
<!-- tabs:start -->
55+
56+
### **Python3**
57+
58+
59+
```python
60+
61+
```
62+
63+
### **Java**
64+
65+
66+
```java
67+
68+
```
69+
70+
### **...**
71+
```
72+
73+
```
74+
75+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public int diagonalSum(int[][] mat) {
3+
int sum = 0, n = mat.length, mid = n >> 1;
4+
for (int i = 0, j = n - 1; i < n; i++, j--) {
5+
sum += (mat[i][i] + mat[i][j]);
6+
}
7+
return n % 2 == 0 ? sum : sum - mat[mid][mid];
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# [1573. 分割字符串的方案数](https://leetcode-cn.com/problems/number-of-ways-to-split-a-string)
2+
3+
[English Version](/solution/1500-1599/1573.Number of Ways to Split a String/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
<p>给你一个二进制串&nbsp;<code>s</code>&nbsp; (一个只包含 0 和 1 的字符串),我们可以将 <code>s</code>&nbsp;分割成 3 个 <strong>非空</strong>&nbsp;字符串 s1, s2, s3 (s1 + s2 + s3 = s)。</p>
9+
10+
<p>请你返回分割&nbsp;<code>s</code>&nbsp;的方案数,满足 s1,s2 和 s3 中字符 &#39;1&#39; 的数目相同。</p>
11+
12+
<p>由于答案可能很大,请将它对 10^9 + 7 取余后返回。</p>
13+
14+
<p>&nbsp;</p>
15+
16+
<p><strong>示例 1:</strong></p>
17+
18+
<pre><strong>输入:</strong>s = &quot;10101&quot;
19+
<strong>输出:</strong>4
20+
<strong>解释:</strong>总共有 4 种方法将 s 分割成含有 &#39;1&#39; 数目相同的三个子字符串。
21+
&quot;1|010|1&quot;
22+
&quot;1|01|01&quot;
23+
&quot;10|10|1&quot;
24+
&quot;10|1|01&quot;
25+
</pre>
26+
27+
<p><strong>示例 2:</strong></p>
28+
29+
<pre><strong>输入:</strong>s = &quot;1001&quot;
30+
<strong>输出:</strong>0
31+
</pre>
32+
33+
<p><strong>示例 3:</strong></p>
34+
35+
<pre><strong>输入:</strong>s = &quot;0000&quot;
36+
<strong>输出:</strong>3
37+
<strong>解释:</strong>总共有 3 种分割 s 的方法。
38+
&quot;0|0|00&quot;
39+
&quot;0|00|0&quot;
40+
&quot;00|0|0&quot;
41+
</pre>
42+
43+
<p><strong>示例 4:</strong></p>
44+
45+
<pre><strong>输入:</strong>s = &quot;100100010100110&quot;
46+
<strong>输出:</strong>12
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
51+
<p><strong>提示:</strong></p>
52+
53+
<ul>
54+
<li><code>s[i] == &#39;0&#39;</code>&nbsp;或者&nbsp;<code>s[i] == &#39;1&#39;</code></li>
55+
<li><code>3 &lt;= s.length &lt;= 10^5</code></li>
56+
</ul>
57+
58+
59+
60+
## 解法
61+
62+
<!-- 这里可写通用的实现逻辑 -->
63+
64+
65+
<!-- tabs:start -->
66+
67+
### **Python3**
68+
69+
<!-- 这里可写当前语言的特殊实现逻辑 -->
70+
71+
```python
72+
73+
```
74+
75+
### **Java**
76+
77+
<!-- 这里可写当前语言的特殊实现逻辑 -->
78+
79+
```java
80+
81+
```
82+
83+
### **...**
84+
```
85+
86+
```
87+
88+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# [1573. Number of Ways to Split a String](https://leetcode.com/problems/number-of-ways-to-split-a-string)
2+
3+
[中文文档](/solution/1500-1599/1573.Number of Ways to Split a String/README.md)
4+
5+
## Description
6+
7+
<p>Given a binary string <code>s</code> (a string consisting only of &#39;0&#39;s and &#39;1&#39;s),&nbsp;we can split <code>s</code>&nbsp;into 3 <strong>non-empty</strong> strings s1, s2, s3 (s1+ s2+ s3 = s).</p>
8+
9+
<p>Return the number of ways <code>s</code> can be split such that the number of&nbsp;characters &#39;1&#39; is the same in s1, s2, and s3.</p>
10+
11+
<p>Since the answer&nbsp;may be too large,&nbsp;return it modulo&nbsp;10^9 + 7.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> s = &quot;10101&quot;
18+
<strong>Output:</strong> 4
19+
<strong>Explanation:</strong> There are four ways to split s in 3 parts where each part contain the same number of letters &#39;1&#39;.
20+
&quot;1|010|1&quot;
21+
&quot;1|01|01&quot;
22+
&quot;10|10|1&quot;
23+
&quot;10|1|01&quot;
24+
</pre>
25+
26+
<p><strong>Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> s = &quot;1001&quot;
30+
<strong>Output:</strong> 0
31+
</pre>
32+
33+
<p><strong>Example 3:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> s = &quot;0000&quot;
37+
<strong>Output:</strong> 3
38+
<strong>Explanation:</strong> There are three ways to split s in 3 parts.
39+
&quot;0|0|00&quot;
40+
&quot;0|00|0&quot;
41+
&quot;00|0|0&quot;
42+
</pre>
43+
44+
<p><strong>Example 4:</strong></p>
45+
46+
<pre>
47+
<strong>Input:</strong> s = &quot;100100010100110&quot;
48+
<strong>Output:</strong> 12
49+
</pre>
50+
51+
<p>&nbsp;</p>
52+
<p><strong>Constraints:</strong></p>
53+
54+
<ul>
55+
<li><code>3 &lt;= s.length &lt;= 10^5</code></li>
56+
<li><code>s[i]</code> is <code>&#39;0&#39;</code>&nbsp;or&nbsp;<code>&#39;1&#39;</code>.</li>
57+
</ul>
58+
59+
60+
## Solutions
61+
62+
63+
64+
<!-- tabs:start -->
65+
66+
### **Python3**
67+
68+
69+
```python
70+
71+
```
72+
73+
### **Java**
74+
75+
76+
```java
77+
78+
```
79+
80+
### **...**
81+
```
82+
83+
```
84+
85+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int numWays(String s) {
3+
char[] chars = s.toCharArray();
4+
List<Long> p = new ArrayList<>();
5+
for (int i = 0; i < chars.length; i++) {
6+
if (chars[i] == '1') {
7+
p.add((long) i);
8+
}
9+
}
10+
int l = p.size();
11+
if (l % 3 != 0) {
12+
return 0;
13+
}
14+
15+
int MOD = (int) (1e9 + 7);
16+
if (l == 0) {
17+
return (int) (((long) (s.length() - 1) * (s.length() - 2) / 2) % MOD);
18+
}
19+
20+
// 每 n/3 的地方为分界线
21+
return (int) ((p.get(l / 3) - p.get(l / 3 - 1)) * (p.get(2 * l / 3) - p.get(2 * l / 3 - 1))
22+
% MOD);
23+
}
24+
}

0 commit comments

Comments
 (0)