leetcode 633 平方数之和
给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c 。
示例1
输入:c = 5
输出:true
解释:1 * 1 + 2 * 2 = 5
示例2
输入:c = 3
输出:false
思路
利用二分搜索,找到是否存在平方和
代码
/**
* @param {number} c
* @return {boolean}
*/
var judgeSquareSum = function(c) {
let low = 0;
let high = Math.floor(Math.sqrt(c));
while (low <= high) {
if (low * low + high * high < c) {
low++;
} else if (low * low + high * high > c) {
high--;
} else {
return true;
}
}
return false;
};
复杂度分析:
- 时间复杂度: O(logN)
- 空间复杂度:O(1)
FEATURED TAGS
前端开发
H5
JavaScript
设计模式
browser
jQuery
源码分析
生活
leetcode
Array
Stack
Queue
Linked List
剑指offer
Binary Search Tree
Binary Tree
Breadth-First Search
Depth-First Search
String
Set
Binary Search
Sliding Window
Backtracking
Dynamic Programming
Two Pointers
Union Find
Sort
Bit Operation
Recursion
Map
Graph
Search
Hash
LinkedList
复盘
QuickSort
Trie
Design
MinHeap
Traverse
Min Heap
Node.js
BackEnd
SQL
MySQL
Design Patterns
Network
计算机网络
Python
SVG