Leetcode: Valid Perfect Squares

Given a positive integer num, write a function which returns True if num is a perfect square else False.

Note: Do not use any built-in library function such as sqrt.

Example 1:

Input: 16
Returns: True
Example 2:

Input: 14
Returns: False

我的binary Search 解法:无需变成long

 1 public class Solution {
 2     public boolean isPerfectSquare(int num) {
 3         if (num <= 0) return false;
 4         int l=1, r=num/2+1;
 5         while (l <= r) {
 6             int mid = l + (r-l)/2;
 7             if (mid == num/mid && num%mid == 0) return true;
 8             else if (mid > num/mid) {
 9                 r = mid - 1;
10             }
11             else l = mid + 1;
12         }
13         return false;
14     }
15 }

别人三种方法总结:

  1. a square number is 1+3+5+7+... Time Complexity O(sqrt(N)) (Credit to lizhibupt, thanks for correcting this).
  2. binary search. Time Complexity O(logN)
  3. Newton Method. See [this wiki page][1]. Time Complexity is close to constant, given a positive integer.
 1 public boolean isPerfectSquare(int num) {
 2       if (num < 1) return false;
 3       for (int i = 1; num > 0; i += 2)
 4         num -= i;
 5       return num == 0;
 6     }
 7
 8     public boolean isPerfectSquare(int num) {
 9       if (num < 1) return false;
10       long left = 1, right = num;// long type to avoid 2147483647 case
11
12       while (left <= right) {
13         long mid = left + (right - left) / 2;
14         long t = mid * mid;
15         if (t > num) {
16           right = mid - 1;
17         } else if (t < num) {
18           left = mid + 1;
19         } else {
20           return true;
21         }
22       }
23
24       return false;
25     }
26
27     boolean isPerfectSquare(int num) {
28       if (num < 1) return false;
29       long t = num / 2 + 1; //or t = num as the begining
30       while (t * t > num) {
31         t = (t + num / t) / 2;
32       }
33       return t * t == num;
34     }
时间: 2024-07-29 21:36:19

Leetcode: Valid Perfect Squares的相关文章

[LeetCode] Valid Perfect Square 检验完全平方数

Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2: Input: 14 Returns: False Credits:Speci

【Leetcode】Perfect Squares

题目链接:https://leetcode.com/problems/perfect-squares/ 题目: Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 

LeetCode 279. Perfect Squares

Question: Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9. Code: public

[LeetCode] Valid Perfect Square

Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2: Input: 14 Returns: False Credits:Speci

【leetcode】Perfect Squares (#279)

Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16 ...) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9. 解析: 利用动态规划解决此问题:对于要求的当前节

LeetCode 第 367 题 (Valid Perfect Square)

LeetCode 第 367 题 (Valid Perfect Square) Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

LeetCode: Valid Parentheses [020]

[题目] Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]

69. Sqrt(x) &amp;&amp; 367. Valid Perfect Square

69. Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. Hide Tags Binary Search Math Hide Similar Problems (M) Pow(x, n) (M) Valid Perfect Square Time Limit Exceed Solution if start from 0. public class Solution { public int m