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 分析:
二分。注意溢出!
public class Solution {
public boolean isPerfectSquare(int num) {
long begin = ;
long end = num; while (begin <= end) {
long mid = (end + begin) / ;
if (mid * mid == num) {
return true;
} else if (mid * mid < num) {
begin = mid + ;
} else {
end = mid - ;
}
}
return false;
}
}
Valid Perfect Square的更多相关文章
- 367. Valid Perfect Square
原题: 367. Valid Perfect Square 读题: 求一个整数是否为完全平方数,如1,4,9,16,……就是完全平方数,这题主要是运算效率问题 求解方法1:812ms class So ...
- Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square)
Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square) 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 ...
- LeetCode_367. Valid Perfect Square
367. Valid Perfect Square Easy Given a positive integer num, write a function which returns True if ...
- [LeetCode] Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- Leetcode 367. Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- 【leetcode】367. Valid Perfect Square
题目描述: Given a positive integer num, write a function which returns True if num is a perfect square e ...
- [Swift]LeetCode367. 有效的完全平方数 | Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [leetcode]367. Valid Perfect Square验证完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- 367. Valid Perfect Square判断是不是完全平方数
[抄题]: Given a positive integer num, write a function which returns True if num is a perfect square e ...
随机推荐
- logic标签用法
logic <logic:iterate> <% Dog dog1=new Dog(); dog1.setAge(2); dog1.setName("xiaoming& ...
- OC基础--OC中类的声明与定义
OC中设计一个类的步骤: 一.声明类: 1.用到的关键字--@interface 和 @end 2.类名 3.继承NSObject 4.属性 5.方法(行为,只需要声明) 二.实现(定义)类 1.用到 ...
- yii2图片上传
yii2利用自带UploadedFile上传图片 public static function uploadFile($name) { $uploadedFile = UploadedFile::ge ...
- BZOJ-1015 StarWar星球大战 并查集+离线处理
1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec Memory Limit: 162 MB Submit: 4105 Solved: 1826 [Submit ...
- Spring监听器配置
使用spring框架时如果同时使用org.springframework.web.util.Log4jConfigListener监听器,那么在web.xml中的监听器的注册顺序为org.spring ...
- Web开发中错误页面的配置
一.创建错误处理页. 1)web.xml里面添加 <error-page> <error-code>404</error-code> <location> ...
- android 常见死机问题--log分析
http://blog.csdn.net/fangchongbory/article/details/7645815 android 常见死机问题--log分析============ ...
- Bootstrap Modals(模态框)
http://www.runoob.com/bootstrap/bootstrap-v2-modal-plugin.html 描述 Bootstrap Modals(模态框)是使用定制的 Jquery ...
- SpringMVC 2.5.6 noMapping
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- if,switch,do,while,for实例
1.#include <stdio.h>void f1(int i){ if( i < 6 ) { printf("Failed!\n" ...