add new solutions
This commit is contained in:
parent
475d438db4
commit
1433bf4850
17 changed files with 394 additions and 0 deletions
26
solutions/0/q69/solution.go
Normal file
26
solutions/0/q69/solution.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package q69
|
||||
|
||||
func mySqrt(x int) int {
|
||||
if x < 2 {
|
||||
return x
|
||||
}
|
||||
l, r := 0, x/2+1
|
||||
for l < r {
|
||||
m := (l + r) / 2
|
||||
sq := m * m
|
||||
switch {
|
||||
case sq == x:
|
||||
return m
|
||||
case sq < x:
|
||||
if (m+1)*(m+1) > x {
|
||||
return m
|
||||
}
|
||||
l = m + 1
|
||||
case sq > x:
|
||||
r = m
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
var _ = mySqrt
|
||||
Loading…
Add table
Add a link
Reference in a new issue