美文网首页
leetcode集锦

leetcode集锦

作者: _empty_cup | 来源:发表于2021-02-25 17:10 被阅读0次

1.leetcode--双指针法:判断一个非负整数是否是两个数平方之和
function judgeSquareSum(target) {
var left = 0;
var right = parseInt(Math.sqrt(target));
//Math.sqrt(sum)返回sum的平方根(不一定是整数),所以需要对其进行取整
while (left <= right) { //这里的等于表示两个数是相等的也行
if (Math.pow(left, 2) + Math.pow(right, 2) === target){
return true;
} else if (Math.pow(left, 2) + Math.pow(right, 2) > target){
right--;
} else {
left++;
}
}
return false;
}

相关文章

网友评论

      本文标题:leetcode集锦

      本文链接:https://www.haomeiwen.com/subject/hsyufltx.html