Find the next perfect square
题目: 找出下一个完全平方数
描述: You might know some pretty large perfect squares. But what about the NEXT one?
Complete the findNextSquare method that finds the next integral perfect square after the one passed as a parameter. Recall that an integral perfect square is an integer n such that sqrt(n) is also an integer.
If the argument is itself not a perfect square then return either -1 or an empty value like None or null, depending on your language. You may assume the argument is non-negative.
Examples ( Input --> Output )
121 --> 144
625 --> 676
114 --> -1 # because 114 is not a perfect square
1
2
3
2
3
在线地址:Find the next perfect square!
function findNextSquare(num){
const sqrtNum = Math.sqrt(num)
if (Number.isInteger(sqrtNum)) {
return Math.pow(sqrtNum + 1, 2)
}
return -1
}
1
2
3
4
5
6
7
2
3
4
5
6
7
上次更新: 2025/09/05, 8:09:00