Skip to content

Commit

Permalink
Merge pull request #522 from 0xff-dev/1318
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1318
  • Loading branch information
6boris committed Jun 27, 2023
2 parents 1061fe0 + de8344a commit c1bd923
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
# [1318.Minimum Flips to Make a OR b Equal to c][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
Given 3 positives numbers `a`, `b` and `c`. Return the minimum flips required in some bits of `a` and `b` to make ( `a` OR `b` == `c` ). (bitwise OR operation).
Flip operation consists of change **any** single bit 1 to 0 or change the bit 0 to 1 in their binary representation.

**Example 1:**

**Example 1:**
![example1](./sample_3_1676.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: a = 2, b = 6, c = 5
Output: 3
Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c)
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Minimum Flips to Make a OR b Equal to c
```go
```
Input: a = 4, b = 2, c = 7
Output: 1
```

**Example 3:**

```
Input: a = 1, b = 2, c = 3
Output: 0
```

## 结语

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

func Solution(x bool) bool {
return x
import (
"fmt"
)

func Solution(a int, b int, c int) int {
aStr := fmt.Sprintf("%b", a)
bStr := fmt.Sprintf("%b", b)
cStr := fmt.Sprintf("%b", c)

la := len(aStr)
lb := len(bStr)
need := 0
ia, ib := la-1, lb-1
for i := len(cStr) - 1; i >= 0 || ia >= 0 || ib >= 0; i, ia, ib = i-1, ia-1, ib-1 {
aa, bb, cc := byte('0'), byte('0'), byte('0')
if ia >= 0 {
aa = aStr[ia]
}
if ib >= 0 {
bb = bStr[ib]
}
if i >= 0 {
cc = cStr[i]
}
if cc == '0' {
if aa != '0' {
need++
}
if bb != '0' {
need++
}
}
if cc == '1' {
if aa == '0' && bb == '0' {
need += 1
}
}
}
return need
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
a, b, c int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 2, 6, 5, 3},
{"TestCase2", 4, 2, 7, 1},
{"TestCase3", 1, 2, 3, 0},
{"TestCase4", 8, 3, 5, 3},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.a, c.b, c.c)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.a, c.b, c.c)
}
})
}
}

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

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c1bd923

Please sign in to comment.