C#刷遍Leetcode面试题系列连载(4): No.633 - 平方数之和
點(diǎn)擊藍(lán)字“dotNET匠人”關(guān)注我喲
加個(gè)“星標(biāo)★”,每日 7:15,好文必達(dá)!
前文傳送門:
上篇文章中一道數(shù)學(xué)問(wèn)題 - 自除數(shù),今天我們接著分析 LeetCode 中的另一道數(shù)學(xué)題吧~
今天要給大家分析的面試題是 LeetCode 上第 633 號(hào)問(wèn)題,
Leetcode 633 - 平方數(shù)之和
https://leetcode.com/problems/sum-of-square-numbers/
題目描述
給定一個(gè)非負(fù)整數(shù)c ,你要判斷是否存在兩個(gè)整數(shù)a和 b,使得。
示例1:
輸入: 5 輸出: True 解釋: 1* 1+ 2* 2= 5示例2:
輸入: 3 輸出: FalseInput:
5 2 100Expected answer:
true true true題目難度: 簡(jiǎn)單
貢獻(xiàn)者:Stomach_ache
相關(guān)話題
數(shù)學(xué)
https://leetcode-cn.com/tag/math
相似題目
有效的完全平方數(shù)
https://leetcode-cn.com/problems/valid-perfect-square
解題思路:
方法1: 遍歷
做一次循環(huán),用目標(biāo)和減去循環(huán)變量的平方,如果剩下的部分依然是完全平方的情形存在,就返回true;否則返回false。
假定,根據(jù)數(shù)據(jù)的對(duì)稱性,循環(huán)變量 i 只需取到??即可覆蓋所有情形.
時(shí)間復(fù)雜度: O(n)
方法2:?雙指針?lè)?/strong>
左指針 l=0,右指針 r = √C,夾逼條件是 ll + rr = C
感謝?博客園園友?msp的昌偉哥哥?的補(bǔ)充和指正~
時(shí)間復(fù)雜度: 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; // 若向上取整=向下取整,則該數(shù)開(kāi)方后是整數(shù) if ((int)(Math.Ceiling(Math.Sqrt(diff))) == (int)(Math.Floor(Math.Sqrt(diff)))) return true; } return false; } }Rank:
執(zhí)行用時(shí): 56ms, 在所有 csharp 提交中擊敗了 68.18%的用戶.
優(yōu)化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:
執(zhí)行用時(shí): 52ms, 在所有 csharp 提交中擊敗了 90.91%?的用戶.
優(yōu)化2(根據(jù)文末參考資料[1]中MPUCoder 的回答改寫(xiě),16進(jìn)制下mod16減少比較次數(shù)):
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:
執(zhí)行用時(shí): 44ms, 在所有 csharp 提交中擊敗了 100.00%?的用戶.
優(yōu)化3(根據(jù)文末參考資料[1]中 Simon 的回答改寫(xiě)):
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:
執(zhí)行用時(shí): 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; } // 以下為測(cè)試 public static void Main(string[] args) { var sol = new Solution(); var res = sol.JudgeSquareSum(25); Console.WriteLine(res); } }Rank:?
執(zhí)行用時(shí): 40ms, 在所有 csharp 提交中擊敗了 100.00% 的用戶.
相比較而已,雙指針?lè)ù_實(shí)更快一些~
相應(yīng)代碼已經(jīng)上傳到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
作者簡(jiǎn)介:Bravo Yeung,計(jì)算機(jī)碩士,知乎干貨答主(獲81K?贊同,?37K?感謝,?234K?收藏)。曾在國(guó)內(nèi) Top3互聯(lián)網(wǎng)視頻直播公司工作過(guò),后加入一家外企做軟件開(kāi)發(fā)至今。
歡迎各位讀者加入?.NET技術(shù)交流群,在公眾號(hào)后臺(tái)回復(fù)“加群”或者“學(xué)習(xí)”即可。
朕已閱?
總結(jié)
以上是生活随笔為你收集整理的C#刷遍Leetcode面试题系列连载(4): No.633 - 平方数之和的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 程序员家的精品大闸蟹:青壳、白底、肉多、
- 下一篇: C# 8.0 的默认接口方法