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 ...
随机推荐
- JDBC13 ORM02 Map封装
用Map封装一条信息 conn=Utils.getConn(); ps=conn.prepareStatement("select Empname,birthday,salary from ...
- python 利用 for ... else 跳出双层嵌套循环
背景 周末在写一个爬虫时,遇到这样一种场景:从搜索结果中下载指定数量的文件 例如:搜索结果中共分为10页展示,加起来一共50条数据,现在要做的是从50条数据中下载指定数量的数据 为了实现这个功能,开始 ...
- 面试最后HR都要问一句"有没有什么问题要问我"?
HR最想听到的问题是: 1.询问职位发展方面的问题: (1)比如:这个职位的未来的发展空间如何,公司是如何对员工进行绩效考评的...等等这类问题,可以让HR觉得你是一个有长远目标和规划的人. 2.询问 ...
- java触发器的学习
public class OpenVirtualService { public void open(){ //虚机开通 //业务逻辑 ...
- servlet--http接口简单的创建及调用
很久没有用servlet的交互技术,生疏的遭不住.现在简单的说说servlet中http接口的创建及调用,便于大家理解,使用. 先说说服务端,就是提供服务方的代码: pom.xml <depen ...
- SQL——处理列中NULL值
处理NULL值 - 数据库中某列为NULL值,使用函数在列值为NULL时返回固定值. SQLServer:ISNULL(col,value) 示例:SELECT ISNULL(co ...
- SQL——AUTO INCREMENT(字段自增)
AUTO INCREMENT -- 在新记录插入表中时生成一个唯一的数字.插入表数据时,该字段不需规定值. 在每次插入新记录时,自动地创建主键字段的值.在表中创建一个 auto-incremen ...
- 特效 css3 渐变背景框
.box{ 子级 position: relative; width: 300px; height: 400px; display: flex; justify-content: center; al ...
- 自定义cursor鼠标 图片
1.CSS3自定义鼠标样式 最近想要使用自定义鼠标样式,看了cursor的样式不好看,就想到cursor属性能不能自定义图片,翻看了下CSS3文档,发现是可以的 格式为:cursor:url('图片u ...
- eatwhatApp开发实战(九)
之前我们为app在item项上添加了点击出现修改对话框,对店名进行修改的功能,其中我们会发现我们点击item和点击item上的按钮会有点击冲突.这次我们来修正下这个问题,同时介绍item项的长按点击O ...