File tree 9 files changed +61
-22
lines changed
0367.Valid Perfect Square
0395.Longest Substring with At Least K Repeating Characters
0600-0699/0635.Design Log Storage System
1700-1799/1759.Count Number of Homogenous Substrings
2027.Minimum Moves to Convert String
2028.Find Missing Observations
2500-2599/2518.Number of Great Partitions
9 files changed +61
-22
lines changed Original file line number Diff line number Diff line change 4
4
5
5
## Description
6
6
7
- <p >Given a < strong >positive</ strong > integer < i >num</ i >, write a function which returns True if < i >num</i > is a perfect square else False .</p >
7
+ <p >Given a positive integer num, return < code >true</ code > < em >if</ em > < code >num</code > < em > is a perfect square or</ em > < code >false</ code > < em >otherwise</ em > .</p >
8
8
9
- <p ><b >Follow up:</b > <b >Do not</b > use any built-in library function such as <code >sqrt</code >.</p >
9
+ <p >A <strong >perfect square</strong > is an integer that is the square of an integer. In other words, it is the product of some integer with itself.</p >
10
+
11
+ <p >You must not use any built-in library function, such as <code >sqrt</code >.</p >
10
12
11
13
<p >  ; </p >
12
14
<p ><strong class =" example " >Example 1:</strong ></p >
13
- <pre ><strong >Input:</strong > num = 16
15
+
16
+ <pre >
17
+ <strong >Input:</strong > num = 16
14
18
<strong >Output:</strong > true
15
- </pre ><p ><strong class =" example " >Example 2:</strong ></p >
16
- <pre ><strong >Input:</strong > num = 14
19
+ <strong >Explanation:</strong > We return true because 4 * 4 = 16 and 4 is an integer.
20
+ </pre >
21
+
22
+ <p ><strong class =" example " >Example 2:</strong ></p >
23
+
24
+ <pre >
25
+ <strong >Input:</strong > num = 14
17
26
<strong >Output:</strong > false
27
+ <strong >Explanation:</strong > We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer.
18
28
</pre >
29
+
19
30
<p >  ; </p >
20
31
<p ><strong >Constraints:</strong ></p >
21
32
22
33
<ul >
23
- <li><code>1 <= num <= 2^31 - 1</code></li>
34
+ <li><code>1 <= num <= 2<sup>31</sup> - 1</code></li>
24
35
</ul >
25
36
26
37
## Solutions
Original file line number Diff line number Diff line change @@ -60,7 +60,7 @@ class Solution:
60
60
ans = max (ans, t)
61
61
i = j
62
62
return ans
63
-
63
+
64
64
return dfs(0 , len (s) - 1 )
65
65
```
66
66
Original file line number Diff line number Diff line change @@ -100,11 +100,11 @@ class LogSystem {
100
100
d. put(" Minute" , 16 );
101
101
d. put(" Second" , 19 );
102
102
}
103
-
103
+
104
104
public void put (int id , String timestamp ) {
105
105
logs. add(new Log (id, timestamp));
106
106
}
107
-
107
+
108
108
public List<Integer > retrieve (String start , String end , String granularity ) {
109
109
List<Integer > ans = new ArrayList<> ();
110
110
int i = d. get(granularity);
@@ -151,11 +151,11 @@ public:
151
151
d[ "Minute"] = 16;
152
152
d[ "Second"] = 19;
153
153
}
154
-
154
+
155
155
void put(int id, string timestamp) {
156
156
logs.push_back({id, timestamp});
157
157
}
158
-
158
+
159
159
vector<int > retrieve (string start, string end, string granularity) {
160
160
vector<int > ans;
161
161
int i = d[ granularity] ;
Original file line number Diff line number Diff line change @@ -258,7 +258,7 @@ int countHomogenous(char *s) {
258
258
for (int i = 0, j = 0; s[ j] ; j++) {
259
259
if (s[ i] != s[ j] ) {
260
260
i = j;
261
- }
261
+ }
262
262
ans = (ans + j - i + 1) % MOD;
263
263
}
264
264
return ans;
Original file line number Diff line number Diff line change 55
55
56
56
** 方法一:贪心**
57
57
58
- 遍历字符串 $s$,只要遇到 ` X ` ,指针就直接往后移动三格 ,并且答案加 $1$;否则指针往后移动一格 。
58
+ 遍历字符串 $s$,只要遇到 ` 'X' ` ,指针 $i$ 就直接往后移动三格 ,并且答案加 $1$;否则指针 $i$ 往后移动一格 。
59
59
60
60
时间复杂度 $O(n)$。其中 $n$ 表示字符串 $s$ 的长度。
61
61
@@ -118,15 +118,14 @@ public:
118
118
### **Go**
119
119
120
120
```go
121
- func minimumMoves(s string) int {
122
- ans := 0
121
+ func minimumMoves(s string) (ans int) {
123
122
for i := 0; i < len(s); i++ {
124
123
if s[i] == 'X' {
125
124
ans++
126
125
i += 2
127
126
}
128
127
}
129
- return ans
128
+ return
130
129
}
131
130
```
132
131
Original file line number Diff line number Diff line change @@ -102,15 +102,14 @@ public:
102
102
### **Go**
103
103
104
104
```go
105
- func minimumMoves(s string) int {
106
- ans := 0
105
+ func minimumMoves(s string) (ans int) {
107
106
for i := 0; i < len(s); i++ {
108
107
if s[i] == 'X' {
109
108
ans++
110
109
i += 2
111
110
}
112
111
}
113
- return ans
112
+ return
114
113
}
115
114
```
116
115
Original file line number Diff line number Diff line change 1
- func minimumMoves (s string ) int {
2
- ans := 0
1
+ func minimumMoves (s string ) (ans int ) {
3
2
for i := 0 ; i < len (s ); i ++ {
4
3
if s [i ] == 'X' {
5
4
ans ++
6
5
i += 2
7
6
}
8
7
}
9
- return ans
8
+ return
10
9
}
Original file line number Diff line number Diff line change 64
64
65
65
<!-- 这里可写通用的实现逻辑 -->
66
66
67
+ ** 方法一:构造**
68
+
69
+ 根据题目描述,所有数字之和为 $(n + m) \times mean$,已知的数字之和为 ` sum(rolls) ` ,那么缺失的数字之和为 $s = (n + m) \times mean - sum(rolls)$。
70
+
71
+ 如果 $s \gt n \times 6$ 或者 $s \lt n$,说明不存在满足条件的答案,返回空数组。
72
+
73
+ 否则,我们可以将 $s$ 平均分配到 $n$ 个数字上,即每个数字的值为 $s / n$,其中 $s \bmod n$ 个数字的值再加上 $1$。
74
+
75
+ 时间复杂度 $O(n + m)$,空间复杂度 $O(1)$。其中 $n$ 和 $m$ 分别为缺失的数字个数和已知的数字个数。
76
+
67
77
<!-- tabs:start -->
68
78
69
79
### ** Python3**
Original file line number Diff line number Diff line change 54
54
55
55
<!-- 这里可写通用的实现逻辑 -->
56
56
57
+ ** 方法一:逆向思维 + 动态规划**
58
+
59
+ 对于一个长度为 $n$ 的数组 ` nums ` ,每个元素都可以选择放入第一个分区或第二个分区,因此一共有 $2^n$ 种分区方式。每一种分区方式,得到的结果可以是“好分区”或者“坏分区”,题目要我们求“好分区”的个数,我们可以转换为求“坏分区”的个数。那么“好分区”的个数就是 $2^n$ 减去“坏分区”的个数。
60
+
61
+ “坏分区”实际上就是从数组 ` nums ` 中选出若干个元素,使得这若干个元素之和不超过 $k$。这可以通过动态规划(0-1 背包问题)来求解。
62
+
63
+ 我们用 $f[ i] [ j ] $ 表示从数组 ` nums ` 的前 $i$ 个元素中选出若干个元素,使得这若干个元素之和为 $j$ 的方案数。那么 $f[ i] [ j ] $ 的状态转移方程为:
64
+
65
+ $$
66
+ f[i][j] = \left\{
67
+ \begin{aligned}
68
+ &f[i - 1][j] & \text{如果不选第 } i \text{ 个元素} \\
69
+ &f[i - 1][j - nums[i - 1]] & \text{如果选第 } i \text{ 个元素}
70
+ \end{aligned}
71
+ \right.
72
+ $$
73
+
74
+ 那么“坏分区”的个数就是 $\sum_ {j=0}^{k-1} f[ n] [ j ] \times 2$,其中 $n$ 为数组 ` nums ` 的长度。最后,我们用 $2^n$ 减去“坏分区”的个数,即可得到“好分区”的个数。
75
+
76
+ 时间复杂度 $O(n \times k)$,空间复杂度 $O(n \times k)$。其中 $n$ 为数组 ` nums ` 的长度,而 $k$ 为整数 $k$。
77
+
57
78
<!-- tabs:start -->
58
79
59
80
### ** Python3**
You can’t perform that action at this time.
0 commit comments