Skip to content
Open
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
@@ -1,28 +1,39 @@
# [3228.Maximum Number of Operations to Move Ones to the End][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given a binary string `s`.

You can perform the following operation on the string **any** number of times:

- Choose **any** index `i` from the string where `i + 1 < s.length` such that `s[i] == '1'` and `s[i + 1] == '0'`.
- Move the character `s[i]` to the **right** until it reaches the end of the string or another `'1'`. For example, for `s = "010010"`, if we choose `i = 1`, the resulting string will be `s = "000110"`.

Return the **maximum** number of operations that you can perform.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```
Input: s = "1001101"

## 题意
> ...
Output: 4

## 题解
Explanation:

### 思路1
> ...
Maximum Number of Operations to Move Ones to the End
```go
We can perform the following operations:

Choose index i = 0. The resulting string is s = "0011101".
Choose index i = 4. The resulting string is s = "0011011".
Choose index i = 3. The resulting string is s = "0010111".
Choose index i = 2. The resulting string is s = "0001111".
```

**Example 2:**

```
Input: s = "00111"

Output: 0
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(s string) int {
countOne := 0
ans := 0
i := 0
for i < len(s) {
if s[i] == '0' {
for i+1 < len(s) && s[i+1] == '0' {
i++
}
ans += countOne
} else {
countOne++
}
i++
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "1001101", 4},
{"TestCase2", "00111", 0},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading