博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
367. Valid Perfect Square
阅读量:4509 次
发布时间:2019-06-08

本文共 786 字,大约阅读时间需要 2 分钟。

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: 16Output: true

Example 2:

Input: 14Output: false

 

Approach #1:

class Solution {public:    bool isPerfectSquare(int num) {        long long l = 0, r = num;        while (l <= r) {            long long m = l + (r - l) / 2;            long long flag = m * m;            if (flag == num) return true;            if (flag < num) l = m + 1;            if (flag > num) r = m - 1;        }        return false;    }};

  

Runtime: 
0 ms, faster than 100.00% of C++ online submissions for Valid Perfect Square.

 

 

转载于:https://www.cnblogs.com/ruruozhenhao/p/9907378.html

你可能感兴趣的文章
HTTP报文(面试会问开发时常用的报文头格式)
查看>>
机器学习从业人员到底做什么?
查看>>
word发表博客的方法
查看>>
Programming Erlang_CHAPTER2_Basic Erlang 学习笔记(2)。
查看>>
Linux基础
查看>>
2019北航软工暑期班作业-预培训个人项目(地铁线路规划)
查看>>
【模板】高精度
查看>>
弱弱的玩下Javascript
查看>>
二叉树相关操作
查看>>
在webstorm开发微信小程序之使用阿里自定义字体图标
查看>>
序列化模块/模块/包
查看>>
eclipse maven plugin 插件 安装 和 配置
查看>>
收集一些复杂有用的正则表达式
查看>>
子数组求和之大数溢出
查看>>
浏览器预览office文件(word,Excel,等)
查看>>
MySQL工具汇总
查看>>
cookie
查看>>
如何使用Eclipse编译C,C++,JAVA程序
查看>>
手把手教如何搭建node+egg项目 引入Sequelize
查看>>
Xcode 4 with External Static Library for iPhone Development
查看>>