Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created August 9, 2017 08:52
Show Gist options
  • Save luojiyin1987/a25732085692dda588da53a48d0d0836 to your computer and use it in GitHub Desktop.
Save luojiyin1987/a25732085692dda588da53a48d0d0836 to your computer and use it in GitHub Desktop.
Construct the Rectangle
func constructRectangle(area int) []int {
res := 1
for i := 2; i * i <= area; i++ {
if area % i == 0 {
res = i
}
}
return []int{area / res, res}
}
func constructRectangle(area int) []int {
var temp []int
x := int(math.Sqrt(float64(area)))
if x * x == area {
temp = append(temp, x)
temp = append(temp, x)
} else {
for i :=x ; ;i++ {
if area % i == 0 {
if area / i > i {
temp =append(temp , area/i)
temp =append(temp, i)
} else {
temp = append(temp, i)
temp = append(temp,area/i)
}
break
}
}
}
return temp
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment