add new solutions
This commit is contained in:
parent
475d438db4
commit
1433bf4850
17 changed files with 394 additions and 0 deletions
28
solutions/0/q67/solution.go
Normal file
28
solutions/0/q67/solution.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package q67
|
||||
|
||||
func toDigit(byt byte) uint8 { return byt - '0' }
|
||||
func toByte(digit uint8) byte { return '0' + digit }
|
||||
|
||||
func addBinary(a string, b string) string {
|
||||
lenA, lenB := len(a), len(b)
|
||||
ret := make([]byte, max(lenA, lenB)+1)
|
||||
var carry uint8
|
||||
for i := range ret {
|
||||
d := carry
|
||||
if i < lenA {
|
||||
d += toDigit(a[lenA-i-1])
|
||||
}
|
||||
if i < lenB {
|
||||
d += toDigit(b[lenB-i-1])
|
||||
}
|
||||
carry = d / 2
|
||||
ret[len(ret)-i-1] = toByte(d % 2)
|
||||
}
|
||||
|
||||
if ret[0] != '1' {
|
||||
return string(ret[1:])
|
||||
}
|
||||
return string(ret)
|
||||
}
|
||||
|
||||
var _ = addBinary
|
||||
Loading…
Add table
Add a link
Reference in a new issue