C#刷遍Leetcode面试题系列连载(4): No.633 - 平方数之和
點擊藍字“dotNET匠人”關注我喲
加個“星標★”,每日 7:15,好文必達!
前文傳送門:
上篇文章中一道數學問題 - 自除數,今天我們接著分析 LeetCode 中的另一道數學題吧~
今天要給大家分析的面試題是 LeetCode 上第 633 號問題,
Leetcode 633 - 平方數之和
https://leetcode.com/problems/sum-of-square-numbers/
題目描述
給定一個非負整數c ,你要判斷是否存在兩個整數a和 b,使得。
示例1:
輸入: 5 輸出: True 解釋: 1* 1+ 2* 2= 5示例2:
輸入: 3 輸出: FalseInput:
5 2 100Expected answer:
true true true題目難度: 簡單
貢獻者:Stomach_ache
相關話題
數學
https://leetcode-cn.com/tag/math
相似題目
有效的完全平方數
https://leetcode-cn.com/problems/valid-perfect-square
解題思路:
方法1: 遍歷
做一次循環,用目標和減去循環變量的平方,如果剩下的部分依然是完全平方的情形存在,就返回true;否則返回false。
假定,根據數據的對稱性,循環變量 i 只需取到??即可覆蓋所有情形.
時間復雜度: O(n)
方法2:?雙指針法
左指針 l=0,右指針 r = √C,夾逼條件是 ll + rr = C
感謝?博客園園友?msp的昌偉哥哥?的補充和指正~
時間復雜度: log(n)
方法1 已AC代碼:
最初版本:
public class Solution { public bool JudgeSquareSum(int c) { for (int i = 0; c - 2 * i * i >= 0; i++) { double diff = c - i*i; // 若向上取整=向下取整,則該數開方后是整數 if ((int)(Math.Ceiling(Math.Sqrt(diff))) == (int)(Math.Floor(Math.Sqrt(diff)))) return true; } return false; } }Rank:
執行用時: 56ms, 在所有 csharp 提交中擊敗了 68.18%的用戶.
優化1:
public class Solution { public bool JudgeSquareSum(int c) { for (int i = 0; c - 2 * i * i >= 0; i++) { int diff = c - i*i; if (IsPerfectSquare(diff)) return true; } return false; } private bool IsPerfectSquare(int num) { double sq1 = Math.Sqrt(num); int sq2 = (int)Math.Sqrt(num); if (Math.Abs(sq1 - (double)sq2) < 10e-10) return true; return false; } }Rank:
執行用時: 52ms, 在所有 csharp 提交中擊敗了 90.91%?的用戶.
優化2(根據文末參考資料[1]中MPUCoder 的回答改寫,16進制下mod16減少比較次數):
public class Solution { public bool JudgeSquareSum(int c) { for (int i = 0; i <= c && c - i * i >= 0; i++) { int diff = c - i*i; if (IsPerfectSquare(diff)) return true; } return false; } public bool IsPerfectSquare(int num) { //TRUE only if n mod 16 is 0,1,4,or 9 if ((0x0213 & (1 << (num & 15))) != 0) { int t = (int)Math.Floor(Math.Sqrt((double)num) + 0.5); return t * t == num; } return false; } }Rank:
執行用時: 44ms, 在所有 csharp 提交中擊敗了 100.00%?的用戶.
優化3(根據文末參考資料[1]中 Simon 的回答改寫):
public class Solution { public bool JudgeSquareSum(int c) { for (int i = 0; c - i * i >= 0; i++) { long diff = c - i*i; if (IsSquareFast(diff)) return true; } return false; } bool IsSquareFast(long n) { if ((0x2030213 & (1 << (int)(n & 31))) > 0) { long t = (long)Math.Round(Math.Sqrt((double)n)); bool result = t * t == n; return result; } return false; } }Rank:
執行用時: 48ms, 在所有 csharp 提交中擊敗了 100.00%的用戶.
方法2 已AC代碼:
public class Solution { public bool JudgeSquareSum(int c) { var r = (int)Math.Sqrt(c); var l = 0; while (l <= r) { var sum = l * l + r * r; if (sum == c) return true; if (sum < c) l++; else r--; } return false; } // 以下為測試 public static void Main(string[] args) { var sol = new Solution(); var res = sol.JudgeSquareSum(25); Console.WriteLine(res); } }Rank:?
執行用時: 40ms, 在所有 csharp 提交中擊敗了 100.00% 的用戶.
相比較而已,雙指針法確實更快一些~
相應代碼已經上傳到github:
https://github.com/yanglr/Leetcode-CSharp/tree/master/leetcode633
參考資料:
[1] Fast way to test whether a number is a square
https://www.johndcook.com/blog/2008/11/17/fast-way-to-test-whether-a-number-is-a-square/
[2] Shortest way to check perfect Square? - C#
https://stackoverflow.com/questions/4885925/shortest-way-to-check-perfect-square/4886006#4886006
End
作者簡介:Bravo Yeung,計算機碩士,知乎干貨答主(獲81K?贊同,?37K?感謝,?234K?收藏)。曾在國內 Top3互聯網視頻直播公司工作過,后加入一家外企做軟件開發至今。
歡迎各位讀者加入?.NET技術交流群,在公眾號后臺回復“加群”或者“學習”即可。
朕已閱?
總結
以上是生活随笔為你收集整理的C#刷遍Leetcode面试题系列连载(4): No.633 - 平方数之和的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序员家的精品大闸蟹:青壳、白底、肉多、
- 下一篇: C# 8.0 的默认接口方法