Java实现 LeetCode 367 有效的完全平方数
367. 有效的完全平方数
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。
说明:不要使用任何内置的库函数,如 sqrt。
示例 1:
输入:16
输出:True
示例 2:
输入:14
输出:False
PS:
牛顿迭代法
class Solution {
public boolean isPerfectSquare(int num) {
if (num < 2) return true;
long x = num;
while (x * x > num) {
x = (x + num / x) / 2;
if (x * x == num) {
return true;
}
}
return false;
}
}
Java实现 LeetCode 367 有效的完全平方数的更多相关文章
- LeetCode 367.有效的完全平方数(C++)
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False. 说明:不要使用任何内置的库函数,如 sqrt. 示例 1: 输入:16 输出:True ...
- Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square)
Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square) 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- STM32F767ZI NUCLEO144 基于CubeIDE快速开发入门指南
刚入手的NUCLEO-F767ZI:整合官网资源,理清思路,便于快速进行快发: 文章目录 1 NUCLEO 系列 2 NUCLEO-F767ZI 3 环境搭建 3.1 Keil/IAR安装 3.2 C ...
- springboot整合mybatis,利用mybatis-genetor自动生成文件
springboot整合mybatis,利用mybatis-genetor自动生成文件 项目结构: xx 实现思路: 1.添加依赖 <?xml version="1.0" e ...
- 深入理解CSS定位—浮动模型
前面我们讲到了绝对定位,在这篇文章中,我们将讲到3种定位模型中的浮动模型.主要参考 张鑫旭在慕课网的 深入理解float 那些年我们一起清过的浮动---by 一丝丝凉 精通CSS 注意:第二小节基本参 ...
- 利用一个VI写入或读取另一个VI的控件值
利用一个VI写入或读取另一个VI的控件值(转载自浅浅的小草) 有一个VI,叫未命名1.vi. 还有一个vi,叫未命名2.vi 现在,我在 1.vi里要对2.vi的字符串控件进行写入. 可 ...
- 画结构图的神器 Graphviz
经常看到别人的论文里各种优美的图,没想过它们是怎么来的,前两天也是在看论文的时候被推荐了一款画图软件graphviz,稍微了解了一下这个画图软件,据说是贝尔实验室的几位牛人开发出来的,试了一下觉得很不 ...
- python--正则表达式中(.)(*)(.*?)以及re.S的认识
https://yiyibooks.cn/xx/python_352/library/re.html 看command: #-*-coding:gb2312-*- __author__ = 'fuda ...
- mysql 获取当前指定分钟的时间
SELECT NOW(); MINUTE); 结果:
- Winform GDI+ 绘图一:绘制2D电池
winform桌面软件开发,在工业控制领域的使用还是很广泛的,打算好好学习一下GDI+绘图.以前都是用别人的轮子,自己也打算封装一些工业控制领域常用的控件. 今天要将的是,利用缓动函数动态绘制电池. ...
- sql语句 怎么从一张表中查询数据插入到另一张表中?
sql语句 怎么从一张表中查询数据插入到另一张表中? ----原文地址:http://www.phpfans.net/ask/MTc0MTQ4Mw.html 比如我有两张表 table1 字段 un ...
- docker启动各种容器命令大全
安装步骤梳理 搜索镜像 拉取镜像 查看镜像 启动镜像 停止容器 移除容器 安装tomcat docker hub好查找tomcat镜像 docker search tomcat 从docker hu ...