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 ...
随机推荐
- STM32 CubeIDE快速创建工程(图文详解)
使用STM32CubeIDE快速创建STM32的HAL库工程. 文章目录 1 STM32CubeIDE Home 2 生成工程 3 程序下载 1 STM32CubeIDE Home 进入到官网的下载界 ...
- hex文件格式总结
hex文件格式总结 文章目录 hex文件格式总结 什么是hex文件? 文件格式 指令类型(Record type) 校验和 :04 02B0 00 92020008 AE :04 0000 05 08 ...
- MYSQL 日月周季年分组
首先准备几条测试数据 DROP TABLE IF EXISTS `test`;CREATE TABLE `test` ( `n_id` int(11) DEFAULT NULL, `d_created ...
- (2)通信中为什么要进行AMC?
AMC,Adaptive Modulation and Coding,自适应调制与编码. 通信信号的传输环境是变化不定的,信道环境时好时差.在这种情景下,我们不可能按照固定的MCS进行信号发送.假如信 ...
- [zoj3591]Nim 游戏
题意:有n堆火柴,选择连续若干堆火柴进行Nim游戏,求让先手胜的选择方案数. 思路:让先手胜等同于这些数的异或值不同于0,不妨转化为求让先手败的方案数.此时记录一个前缀的异或和val[i],那么答案就 ...
- 1020 Tree Traversals (25分)思路分析 + 满分代码
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- SpringData表关系:多对多
一.编写实体类配置关联关系: 1.多对多使用注解@ManyToMany配置:a. 在实体中添加一个集合属性 b.在属性上添加ManyToMany注解 c.@JoinTable 注解配置关联关系(nam ...
- The Apache Tomcat Connector
http://tomcat.apache.org/connectors-doc/generic_howto/quick.html 搭建最简单的tomcat connector 用到了apapche 的 ...
- Notification API,为你的网页添加桌面通知推送
Notification 是什么 MDN: Notifications API 的 Notification 接口用于配置和向用户显示桌面通知.这些通知的外观和特定功能因平台而异,但通常它们提供了一种 ...
- JavaDoc文件如何生成
目录 如何使用 1.通过命令行生成JavaDoc文档 2.IDEA如何配置后生成javadoc文档 javadoc命令是用来生成自己的API文档的 参考信息: @author 作者名 @version ...