Sum of Square Numbers 平方數之和
給定一個非負整數 c
,你要判斷是否存在兩個整數 a
和 b
,使得 a2 + b2 = c。
示例1:
輸入: 5 輸出: True 解釋: 1 * 1 + 2 * 2 = 5
示例2:
輸入: 3 輸出: False
思路:因為要滿足a^2+b^2=c,所以a的取值範圍為[0,sqrt(c)],所以迴圈設定為[0,sqrt(c)],每次迴圈中我們反計算出b,b=sqrt(c-a^2),如果b是整數,那麼返回true,如果遍歷完都找不到滿足條件的b,那麼返回false。
參考程式碼:
class Solution { public: bool judgeSquareSum(int c) { for (long long a = 0; a*a <= c; a++) { double b = sqrt(c - a * a); if (b == ((int)b)) { return true; } } return false; } };
相關推薦
Sum of Square Numbers 平方數之和
給定一個非負整數 c ,你要判斷是否存在兩個整數 a 和 b,使得 a2 + b2 = c。 示例1: 輸入: 5 輸出: True 解釋: 1 * 1 + 2 * 2 = 5 示例2: 輸入: 3 輸出: False 思路:因為要滿足a^2+b^2=c,所以a的
[LeetCode] Sum of Square Numbers 平方數之和
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
【LeetCode】633. 平方數之和(Sum of Square Numbers)
【 英文練習 | 中文練習 】 題目描述: 給定一個非負整數 c ,你要判斷是否存在兩個整數 a 和 b,c 等於 a 和 b 的平方和。 示例: 輸入: 5 輸出: True 解釋: 1 * 1 + 2 * 2 = 5 解法一: 利用雙指標的思想,需要注意兩個坑點,一
C#LeetCode刷題之#633-平方數之和( Sum of Square Numbers)
問題 給定一個非負整數 c ,你要判斷是否存在兩個整數 a 和 b,使得 a2 + b2 = c。 輸入: 5 輸出: True 解釋: 1 * 1 + 2 * 2 = 5 輸入: 3
leetcode 633. 平方數之和(Sum of Square Numbers)
-- etc 題目 while code ++ title int div 目錄 題目描述: 示例1: 示例2: 解法: 題目描述: 給定一個
lintcode: Check Sum of Square Numbers
floor data- logs footer -s div strong you cksum Check Sum of Square Numbers Given a integer c, your task is to decide whether there‘
633. Sum of Square Numbers
pla numbers task -- boolean color else integer while Given a non-negative integer c, your task is to decide whether there‘re two integers
[leetcode]633. Sum of Square Numbers
body ges sum pre -- mat sqrt else 註意 雙指針比較簡單的應用,搜索範圍要註意 public boolean judgeSquareSum(int c) { /* 雙指針,搜索範圍是0到sqrt(c)
leetcode:(633) Sum Of Square Numbers(java)
題目: Given a non-negative integer c, your task is to decide whether there're two integers a and b 
[LeetCode] 633. Sum of Square Numbers
題目 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:
Sum of Square Numbers
Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 =
[LeetCode] Sum of Two Integers 兩數之和
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example: Given a = 1 and b = 2, return 3. Credits:Sp
Leetcode——633. Sum of Square Numbers
題目原址 題目描述 Given a non-negative integer c, your task is to decide whether there’re two integers
633. Sum of Square Numbers(平方和)(leetcode)
時間復雜度 ron html lis src hide 平方根 you HERE 題目: Given a non-negative integer c, your task is to decide whether there‘re two integers a and b
Sum of AP series——AP系列之和
clu 找到 ima 約束 區別 系列 .cn common 小數 A series with same common difference is known as arithmetic series. The first term of series is ‘a‘ and
[leetcode]404. Sum of Left Leaves左葉子之和
== clas lean 節點 urn nbsp ftl log tle 弄個flag記錄是不是左節點就行 int res = 0; public int sumOfLeftLeaves(TreeNode root) { if (root==nul
LintCode 697. 判斷是否為平方數之和
超時 判斷 param -a 是否 兩個 ber cksum ret ---恢復內容開始--- 給一個整數 c, 你需要判斷是否存在兩個整數 a 和 b 使得 a^2 + b^2 = c. 樣例 給出 n = 5返回 true // 1 * 1 + 2 * 2 = 5給出
404 Sum of Left Leaves 左葉子之和
AR tree etc 二叉 ble roo bsp true desc 計算給定二叉樹的所有左葉子之和。示例: 3 / \ 9 20 / \ 15 7在這個二叉樹中,有兩個左葉子,分別是 9 和 15,所以返回 24。 詳見:https://
【JS】平方數之和 #數學
給定一個非負整數 c ,你要判斷是否存在兩個整數 a 和 b,使得 a2 + b2 = c。 輸入: 5 ,輸出: True ,解釋: 1 * 1 + 2 * 2 = 5 輸入: 3 ,輸出: False 解法一: 範圍內求值 ,0 - C的平方根 var ju
633. 平方數之和
633.平方數之和 給定一個非負整數 c ,你要判斷是否存在兩個整數 a 和 b,使得 a2 + b2 = c。 示例1: 輸入: 5 輸出: True 解釋: 1 * 1 + 2 * 2 = 5 示例2: 輸入: 3 輸出: False 分析: 用左右