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 ...
随机推荐
- [hdu3486]rmq+枚举优化
题意:给n个数,求最小的段数,使得每一段的最大值之和大于给定的k.每一段的长度相等,最后若干个丢掉. 思路:从小到大枚举段数,如果能o(1)时间求出每一段的和,那么总复杂度是O(n(1+1/2+1/3 ...
- mysql 库表整体相关查询
select table_schema,table_name from information_schema.columns where column_name = '字段名'; 查询某张表有几条记录 ...
- 关于 Blob
博客地址:https://ainyi.com/88 对于 Blob,前端开发中可能比较少遇到:数据库中可使用 Blob 概念,例如 Mysql 存储二进制数据的类型就是 Blob,也就是说图片可存储于 ...
- 力扣题解-面试题22. 链表中倒数第K个节点
题目描述 输入一个链表,输出该链表中倒数第k个节点.为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点.例如,一个链表有6个节点,从头节点开始,它们的值依次是1.2.3.4.5. ...
- Vue+Vuex实现自动登录 升级版
Vue+Vuex实现自动登录 升级版 之前实现的版本在这里:Vue+Vuex实现自动登录 在之前实现的版本中,如果你进行测试,可以看到在浏览器的local Storage中,确实里面有了我 ...
- P5410 【模板】扩展 KMP
P5410 [模板]扩展 KMP #include<bits/stdc++.h> using namespace std; ; int q, nxt[maxn], extend[maxn] ...
- SSL F5
应用交付领域经常提到SSL加速,但SSL加速到底是什么意思?SSL加速和F5指什么意思呢?在网上查询和整理了一些关于SSL加速和F5的相关解释,仅供参考:SSL加速:加密套接层协议(简称SSL)是网络 ...
- 30分钟快速上手Docker,看这篇就对了!
一.历史演化 1.演化史 2.物理机时代 2.1.图解 一个物理机上安装操作系统,然后直接运行我们的软件.也就是说你电脑上直接跑了一个软件,并没有开虚拟机什么的,资源极其浪费. 2.2.缺点 部署慢 ...
- Java并发:线程安全分析
java中的线程安全是什么: 就是线程同步的意思,就是当一个程序对一个线程安全的方法或者语句进行访问的时候,其他的不能再对他进行操作了,必须等到这次访问结束以后才能对这个线程安全的方法进行访问 什么叫 ...
- springmvc拦截器和概念,配置!!!
用于拦截请求,过滤后再拦截 实现HandlerInterceptor接口 配置拦截器 package cn.zys.lanjieqi; import javax.servlet.http.HttpSe ...