367. Valid Perfect Square

202409302204
tags: #binary-search

var isPerfectSquare = function(num) {
	let start = 1;
	let end = Math.floor(num / 2) + 1;
	while (start <= end) {
		const mid = Math.floor((end - start) / 2) + start;
		const product = mid * mid;
		if (product === num) {
			return true;
		}
		if (product < num) {
			start = mid + 1;
		} else {
			end = mid - 1;
		}
	}
	return num === end * end;
};

Reference

https://leetcode.com/problems/valid-perfect-square/