🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c. Example 1: ``` Input: 5 Output: True Explanation: 1 * 1 + 2 * 2 = 5 ``` Example 2: ``` Input: 3 Output: False ``` 思路:通过判断一个整数对1求模是否为0来判断一个数是否为整数。将双层循环变为一层 ``` /** * @param {number} c * @return {boolean} */ var judgeSquareSum = function(c) { for(var i = 0; i*i <= c; i++){ var val = Math.sqrt(c-i*i); if(val%1 === 0){ return true; } } return false; }; ```