[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:
Input: 14 Returns: False
使用二分查找寻找input的开方值;
使用long long防止溢出。
我的代码如下:
class Solution { public:bool isPerfectSquare(int num) {if (num == 1) return true;long long left = 1, mid = (num + 1) / 2, right = num;while (left <= right) {if (mid * mid == num) return true;if (mid * mid > num) right = mid - 1;else left = mid + 1;mid = (left + right) / 2;}return false;} };
转载于:https://www.cnblogs.com/zmj97/p/7554416.html
[leetcode] 367. Valid Perfect Square相关推荐
- leetcode 367. Valid Perfect Square
二分. bool isPerfectSquare(int num) {if (num == 0 || num == 1)return true;int start = 0;int end = num; ...
- 367. Valid Perfect Square
题目: Given a positive integer num, write a function which returns True if num is a perfect square els ...
- C#LeetCode刷题之#367-有效的完全平方数(Valid Perfect Square)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3869 访问. 给定一个正整数 num,编写一个函数,如果 num ...
- LeetCode Valid Perfect Square(是否是平方数)
题意:给出一正整数,不用 sqrt函数,判断该数是否是平方数 思路: 第一种,连续奇数和为平方数 代码如下: public class Solution {public boolean isPerfe ...
- Leetcode 279. Perfect Square
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- 【To Do! 重点 正则表达式】LeetCode 65. Valid Number
LeetCode 65. Valid Number 本博客转载自:[1]http://www.cnblogs.com/yuzhangcmu/p/4060348.html [2]https://blog ...
- 【回文串14】LeetCode 680. Valid Palindrome II
LeetCode 680. Valid Palindrome II Solution1:我的答案 复杂度是O(n)O(n)O(n),不算好啊.. 注意:对于c++中string对象常用的insert( ...
- 【回文串3】LeetCode 125. Valid Palindrome
LeetCode 125. Valid Palindrome Solution1:我的答案 复杂度为O(n)O(n)O(n),写法不是很简练啊. class Solution { public:boo ...
- LeetCode 36. Valid Sudoku
LeetCode 36. Valid Sudoku Solution1:我的答案,比较笨.但是清晰易懂好上手啊~~~ class Solution { public:bool isValidSudok ...
最新文章
- 如何使用MatPlotLib绘制出具有两个 Y 轴的曲线图?
- 【刷题记录】杂题记录
- 从尾到头打印单向链表
- 【汇总】涉及iOSiPhone开发相关文章汇总
- Redis源码剖析(十三)整数集合
- swfheader 0.10 Released(已更正下载地址)
- 6、使用infowindow
- 【AI视野·今日NLP 自然语言处理论文速览 第八期】Wed, 16 Jun 2021
- DeprecationWarning:current URL string parser is deprecated, and will be removed in a future version.
- transmute[推荐小软件]
- 基于事件的异步模式概述
- GEE开发之Landsat8_NDVI的数据分析
- RS485芯片与单片机接口中的一个容易忽略的隐患
- 黑客教父郭盛华:提升家庭WiFi的10个方法
- SpaceSniffer(磁盘大小扫描分析) 彻底解决C盘爆满问题 清理C盘必备软件
- unit怎么发音_unitl是什么意思
- ubuntu 16.04 下安装搜狗输入法教程
- 进阶:主流的cpu插槽类型详解
- 云原生究竟怎么落地?
- react(let命令)