返回介绍

solution / 0300-0399 / 0367.Valid Perfect Square / README_EN

发布于 2024-06-17 01:04:01 字数 5143 浏览 0 评论 0 收藏 0

367. Valid Perfect Square

中文文档

Description

Given a positive integer num, return true _if_ num _is a perfect square or_ false _otherwise_.

A perfect square is an integer that is the square of an integer. In other words, it is the product of some integer with itself.

You must not use any built-in library function, such as sqrt.

 

Example 1:

Input: num = 16
Output: true
Explanation: We return true because 4 * 4 = 16 and 4 is an integer.

Example 2:

Input: num = 14
Output: false
Explanation: We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer.

 

Constraints:

  • 1 <= num <= 231 - 1

Solutions

Solution 1: Binary search

class Solution:
  def isPerfectSquare(self, num: int) -> bool:
    left, right = 1, num
    while left < right:
      mid = (left + right) >> 1
      if mid * mid >= num:
        right = mid
      else:
        left = mid + 1
    return left * left == num
class Solution {
  public boolean isPerfectSquare(int num) {
    long left = 1, right = num;
    while (left < right) {
      long mid = (left + right) >>> 1;
      if (mid * mid >= num) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
    return left * left == num;
  }
}
class Solution {
public:
  bool isPerfectSquare(int num) {
    long left = 1, right = num;
    while (left < right) {
      long mid = left + right >> 1;
      if (mid * mid >= num)
        right = mid;
      else
        left = mid + 1;
    }
    return left * left == num;
  }
};
func isPerfectSquare(num int) bool {
  left, right := 1, num
  for left < right {
    mid := (left + right) >> 1
    if mid*mid >= num {
      right = mid
    } else {
      left = mid + 1
    }
  }
  return left*left == num
}
function isPerfectSquare(num: number): boolean {
  let left = 1;
  let right = num >> 1;
  while (left < right) {
    const mid = (left + right) >>> 1;
    if (mid * mid < num) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }
  return left * left === num;
}
use std::cmp::Ordering;
impl Solution {
  pub fn is_perfect_square(num: i32) -> bool {
    let num: i64 = num as i64;
    let mut left = 1;
    let mut right = num >> 1;
    while left < right {
      let mid = left + (right - left) / 2;
      match (mid * mid).cmp(&num) {
        Ordering::Less => {
          left = mid + 1;
        }
        Ordering::Greater => {
          right = mid - 1;
        }
        Ordering::Equal => {
          return true;
        }
      }
    }
    left * left == num
  }
}

Solution 2: Math trick

This is a math problem:

1 = 1
4 = 1 + 3
9 = 1 + 3 + 5
16 = 1 + 3 + 5 + 7
25 = 1 + 3 + 5 + 7 + 9
36 = 1 + 3 + 5 + 7 + 9 + 11
....
so 1+3+...+(2n-1) = (2n-1 + 1)n/2 = n²
class Solution:
  def isPerfectSquare(self, num: int) -> bool:
    i = 1
    while num > 0:
      num -= i
      i += 2
    return num == 0
class Solution {
  public boolean isPerfectSquare(int num) {
    for (int i = 1; num > 0; i += 2) {
      num -= i;
    }
    return num == 0;
  }
}
class Solution {
public:
  bool isPerfectSquare(int num) {
    for (int i = 1; num > 0; i += 2) num -= i;
    return num == 0;
  }
};
func isPerfectSquare(num int) bool {
  for i := 1; num > 0; i += 2 {
    num -= i
  }
  return num == 0
}
function isPerfectSquare(num: number): boolean {
  let i = 1;
  while (num > 0) {
    num -= i;
    i += 2;
  }
  return num === 0;
}
impl Solution {
  pub fn is_perfect_square(mut num: i32) -> bool {
    let mut i = 1;
    while num > 0 {
      num -= i;
      i += 2;
    }
    num == 0
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文