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 有效的完全平方数的更多相关文章

  1. LeetCode 367.有效的完全平方数(C++)

    给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False. 说明:不要使用任何内置的库函数,如  sqrt. 示例 1: 输入:16 输出:True ...

  2. Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square)

    Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square) 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 ...

  3. 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 ...

  4. 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. ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 基于C语言的Q格式使用详解

    用过DSP的应该都知道Q格式吧: 目录 1 前言 2 Q数据的表示 2.1 范围和精度 2.2 推导 3 Q数据的运算 3.1 0x7FFF 3.2 0x8000 3.3 加法 3.4 减法 3.5 ...

  2. [hdu5195]线段树

    题意:给一个DAG,最多可以删去k条边,求字典序最大的拓扑序列.思路:贪心选取当前可选的最大编号即可,同时用线段树维护下.一个节点可以被选,当且仅当没有指向它的边. #include <iost ...

  3. 新能力 | 云开发CMS内容管理系统,5分钟搞定小程序管理后台

    小程序·云开发的云调用能力,让用户可以免鉴权快速调用微信的开放能力,极大节约了开发成本.现在,大家期待已久的云开发 CMS 内容管理系统,终于上线啦!顺便提示,接下来还可以二次开发哦! 云开发 CMS ...

  4. flask之response

    import os from flask import Flask,render_template,redirect,jsonify,send_file app=Flask(__name__) #开发 ...

  5. Django ORM性能优化之count和len方法的选择(非常详细推荐干货)

    接下来我将从源码层面分情况和应用分析我们在计算queryset数据集时是用orm的count函数计算长度还是用len函数计算数据集长度. 首先,我们知道ORM查询queryset数据集是惰性查询的,只 ...

  6. Java多线程相关面试题及答案-整理

    1.什么是线程? 线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位.程序员可以通过它进行多处理器编程,你可以使用多线程对 运算密集型任务提速.比如,如果一个线程完成 ...

  7. Fabric CA的部署与使用

    Fabric CA是Hyperledger Fbric的证书认证中心,提供以下功能:用户信息的登记与注册,数字证书的颁发与管理. 前言 之前使用CA服务一直是在docker容器中运行下载好的CA镜像, ...

  8. poj3683 2-SAT 同上一道

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10151   Accep ...

  9. 设计模式:Filter+Servlet+反射

    传统设计 分类管理需要:增加,删除,编辑,修改,查询5个服务端功能. 一个路径对应一个Servlet的思路,就需要设计5个Servlet类,并且在web.xml中配置5个路径. CategoryAdd ...

  10. Java并发编程volatile关键字

    volatile理解 Java语言是支持多线程的,为了解决线程并发的问题,在语言内部引入了 同步块 和volatile 关键字机制.volatile具有synchronized关键字的“可见性”,vo ...