題目
給定一個非負整數 c ,判斷是否有兩個整數 a 和 b 使得 a2 + b2 = c 。
解法一
使用 for 迴圈
- a, b 一定比 c 開平方還要小(或等於),所以可以從 c 開平方慢慢往回遍歷尋找最大的a
- b2= c – a2
- 得出 b2 後開根號如果是整數就代表 c 是兩數平方數之和
/**
 * @param {number} c
 * @return {boolean}
 */
var judgeSquareSum = function(c) {
    let result = false
    for (let a = Math.floor(Math.sqrt(c)); a >= 0; a--) {
        const b_square = c - Math.pow(a, 2)
        if (Number.isInteger(Math.sqrt(b_square)))
            result = true
    }
    return result
};要注意的是 0 是由 02 + 02 組成,所以 0 回傳的是 true
解法二(推薦)
使用雙指針來解。
- 初始化雙指針
- left從 0 開始,代表較小的平方數。
- right從 根號c 開始,代表較大的平方數。
 
- 迴圈遍歷
- 當 left小於或等於right時,計算left和right的平方和。
- 如果平方和等於 c,立即返回true。
- 如果平方和小於 c,增大left以增加平方和。
- 如果平方和大於 c,減小right以減小平方和。
 
- 當 
/**
 * @param {number} c
 * @return {boolean}
 */
var judgeSquareSum = function(c) {
    let left = 0
    let right = Math.floor(Math.sqrt(c))
    while(left <= right) {
        let sum = Math.pow(left, 2) + Math.pow(right, 2)
        if (sum === c) return true
        else if (sum < c) left++
        else right--
    }
    
    return false
};![[LeetCode] 633. Sum of Square Numbers (JS) 2 image 3](https://www.may-notes.com/wp-content/uploads/2024/06/image-3-1024x213.png)
兩種解法的Runtime和Memory Usage差不了太多。left 和 right 兩個指針最多各自移動 [math]\sqrt{c}[/math] 次,因為指針移動的範圍是從 0 到 [math]\sqrt{c}[/math]。所以此解法的時間複雜度為 O([math]\sqrt{c}[/math])
 
				 
					![[LeetCode] 633. Sum of Square Numbers (JS) 1 [LeetCode] 633. Sum of Square Numbers (JS)](https://www.may-notes.com/wp-content/uploads/2024/06/LeetCode_Sharing.png)
![[React] Redux Toolkit Query(RTKQ) 基礎使用指南 3 RTKQ logo](https://www.may-notes.com/wp-content/uploads/2023/12/nl9bkr5l1h5ke31vkula-150x150.webp)
![[LeetCode] 826. Most Profit Assigning Work (JS) 4 LeetCode Sharing](https://www.may-notes.com/wp-content/uploads/2024/06/LeetCode_Sharing-150x150.png)

![[GreatFrontEnd題目] Binary Search 二分搜尋法(JS) 8 code-banner](https://www.may-notes.com/wp-content/uploads/2023/10/emile-perron-xrVDYZRGdw4-unsplash-150x150.jpg)
![[LeetCode] 26. Remove Duplicates from Sorted Array (JS) 9 [LeetCode] 26. Remove Duplicates from Sorted Array (JS)](https://www.may-notes.com/wp-content/uploads/2024/06/LeetCode_Sharing-120x120.png)