367. Valid Perfect Square
原题:
读题:
求一个整数是否为完全平方数,如1,4,9,16,……就是完全平方数,这题主要是运算效率问题
求解方法1:812ms
class Solution {
public:
bool isPerfectSquare(int num)
{
if(num < 0) return false;
int i = 0;
//这里要加1,不然num = 1时会出错
for(;i< num/2 + 1;i++)
{
if(i*i == num)
{
return true;
}
}
return false;
}
};
论坛高效率求解法:
class Solution {
public:
bool isPerfectSquare(int num) {
long left = 1, right = num;
while(left <= right){
long mid = left + (right - left) / 2;
long t = mid * mid;
if(t > num){
right = mid - 1;
}else if(t < num){
left = mid + 1;
}else
return true;
}
return false;
}
};
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 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 ...
- [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 ...
- [LC] 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 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:完全平方式性质 方法二:暴力求解 方法三:二 ...
- Python 解LeetCode:367. Valid Perfect Square
题目描述:给出一个正整数,不使用内置函数,如sqrt(),判断这个数是不是一个数的平方. 思路:直接使用二分法,貌似没啥好说的.代码如下: class Solution(object): def is ...
- 【easy】367. Valid Perfect Square 判断是不是平方数
class Solution { public: bool isPerfectSquare(int num) { /* //方法一:蜜汁超时…… if (num < 0) return fals ...
随机推荐
- RHEL6安装配置DNS服务
RHEL6安装配置DNS服务 作者:Eric 微信:loveoracle11g 安装软件包 [root@rac1 ~]# yum -y install bind bind-chroot caching ...
- win7右键菜单调整顺序
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ Discardable\PostSetup\ShellNew ...
- CPU Rings, Privilege, and Protection.CPU的运行环, 特权级与保护
原文标题:CPU Rings, Privilege, and Protection 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限,只好挑一些国外高手的 ...
- day24类的继承
类的继承1 什么是继承 继承一种新建类的方式,新建的类称之为子类/派生类,被继承的类称之为父类\基类\超类 python中继承的特点: 1. 子类可以遗传/重用父类的属性 ...
- JVM总结-Java语法糖与Java编译器
自动装箱与自动拆箱 首先要提到的便是 Java 的自动装箱(auto-boxing)和自动拆箱(auto-unboxing). 我们知道,Java 语言拥有 8 个基本类型,每个基本类型都有对应的包装 ...
- day2----python的基本类型
本文档的大致内容:(python使用版本3.6.4) 1 数字--int 2 布尔--bool 3 字符串--str 4 元祖--() 5 列表---['a','b'] 6 字典--{} 运算符: ...
- asp 月末 月初
上个月第一天:<%=dateadd("m",-1,year(date)&"-"&month(date)&"-1" ...
- Linux网络管理-相关笔记【自用】
ISO/OSI七层模型应用层 APDU 应用层协议数据单元 越靠近用户表示层 PPDU 表示层协议数据单元会话层 SPDU 会话协 ...
- Xcode清除缓存,清除多余证书
http://blog.csdn.net/IDOshi201109/article/details/46634541
- C#利用反射动态调用DLL并返回结果,和获取程序集的信息
反射的基本概念: .Net Framework 中提供了反射机制,可以再加载程序运行时,动态获取和加载程序集,并且可以获取到程序集的信息 创建Assembly和Entity两个程序集,在Assembl ...