mirror of
https://github.com/lWolvesl/leetcode.git
synced 2026-01-12 18:08:38 +08:00
251116
This commit is contained in:
20
25/11/1513.cpp
Normal file
20
25/11/1513.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <string>
|
||||
|
||||
|
||||
int numSub(std::string s) {
|
||||
int count = 0;
|
||||
long ans = 0;
|
||||
int mod = 1e9+7;
|
||||
int n = s.length();
|
||||
for(int i = 0;i<n;i++){
|
||||
if(s[i] == '1'){
|
||||
count++;
|
||||
}
|
||||
if(s[i] == '0' || i == n-1){
|
||||
while (count != 0) {
|
||||
ans += count--;
|
||||
}
|
||||
}
|
||||
}
|
||||
return int(ans % mod);
|
||||
}
|
||||
17
25/11/3228-unfinish.cpp
Normal file
17
25/11/3228-unfinish.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <string>
|
||||
|
||||
int maxOperations(std::string s) {
|
||||
int countOne = 0;
|
||||
int ans = 0;
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
if (s[i] == '0') {
|
||||
while ((i + 1) < s.length() && s[i + 1] == '0') {
|
||||
i++;
|
||||
}
|
||||
ans += countOne;
|
||||
} else {
|
||||
countOne++;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
38
25/11/go/2654.go
Normal file
38
25/11/go/2654.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package B
|
||||
|
||||
func minOperations(nums []int) int {
|
||||
n, gcdAll, cnt1 := len(nums), 0, 0
|
||||
for _, x := range nums {
|
||||
gcdAll = gcd(gcdAll, x)
|
||||
if x == 1 {
|
||||
cnt1++
|
||||
}
|
||||
}
|
||||
if gcdAll > 1 {
|
||||
return -1
|
||||
}
|
||||
if cnt1 > 0 {
|
||||
return n - cnt1
|
||||
}
|
||||
|
||||
minSize := n
|
||||
for i := range nums {
|
||||
g := 0
|
||||
for j, x := range nums[i:] {
|
||||
g = gcd(g, x)
|
||||
if g == 1 {
|
||||
// 这里本来是 j+1,把 +1 提出来合并到 return 中
|
||||
minSize = min(minSize, j)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return minSize + n - 1
|
||||
}
|
||||
|
||||
func gcd(a, b int) int {
|
||||
for a != 0 {
|
||||
a, b = b%a, a
|
||||
}
|
||||
return b
|
||||
}
|
||||
Reference in New Issue
Block a user