[LeetCode]367. Valid Perfect Square判断完全平方数
方法有很多,我觉得比较容易记住的是两个,一个是二分法,在1-num/2中寻找目标数
另一个是数学方法:
public boolean isPerfectSquare(int num) {
/*
有很多方法,二分法,搜索法等等
最简单的方法需要知道一个数学定理
完全平方数等于奇数序列相加,1=1,4=1+3,9=1+3+5
*/
if (num<=0) return false;
int i=1;
while (num>0){
num-=i;
i+=2;
if(num==0) return true;
}
return false;
}
[LeetCode]367. Valid Perfect Square判断完全平方数的更多相关文章
- [LeetCode] 367. Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [leetcode]367. Valid Perfect Square验证完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- 367. Valid Perfect Square判断是不是完全平方数
[抄题]: Given a positive integer num, write a function which returns True if num is a perfect square e ...
- Leetcode 367. Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- 【easy】367. Valid Perfect Square 判断是不是平方数
class Solution { public: bool isPerfectSquare(int num) { /* //方法一:蜜汁超时…… if (num < 0) return fals ...
- 367. Valid Perfect Square
原题: 367. Valid Perfect Square 读题: 求一个整数是否为完全平方数,如1,4,9,16,……就是完全平方数,这题主要是运算效率问题 求解方法1:812ms class So ...
- [LeetCode] Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- 【LeetCode】367. Valid Perfect Square 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:完全平方式性质 方法二:暴力求解 方法三:二 ...
- 【leetcode】367. Valid Perfect Square
题目描述: Given a positive integer num, write a function which returns True if num is a perfect square e ...
随机推荐
- Markdown 语法详解
Markdown 学习 标题 三级标题 四级标题 最多支持六级标题 "#... + 标题名称" 字体 hello, world "** 内容 **" hello ...
- layui获取弹出层内容
一. 弹出层: <body class="childrenBody"> <form class="layui-form"> <di ...
- Python中__new__方法为什么有人说是构造方法?有什么作用?
__new__方法是Python新式类引入的,通常用于控制生成一个新实例的过程.它是类级别的静态方法,是在创建实例对象前执行,如果自定义类中没有重写该方法,则Python自动调用父类的方法,当所有父类 ...
- Mybatis学习03
title: Mybatis学习03 date: 2020-01-19 13:03:20 tags: Mybatis学习的第二天,内容有日志和分页. <!--more--> 1.日志 1. ...
- SUCTF pythonigx
0x00知识点 nginx配置 配置文件存放目录:/etc/nginx主配置文件:/etc/nginx/conf/nginx.conf管理脚本:/usr/lib64/systemd/system/ng ...
- HTML 实战生成一张页面
1 HTML简介 1.1 解释 HTML是用来描述网页的一种语言. HTML即超文本标记语言(Hyper Text Markup Language): HTML不是一种编程语言,而是一种标记语言(ma ...
- WebRequest抓取网页数据出现乱码问题
今天项目里突然有个功能用不起来了,本机确实好的 ,这个很无语 不知道为啥 经过写日志发现html 变成了这样的东西,很是头疼,刚开始各种编码转换,发现这并不是编码的问题 后面观察目标网站多了一个gzi ...
- css3实现立体魔方效果
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- CSP-S 2019 Solution
Day1-T1 格雷码(code) 格雷码是一种特殊的 \(n\) 位二进制串排列法,要求相邻的两个二进制串恰好有一位不同,环状相邻. 生成方法: \(1\) 位格雷码由两个 \(1\) 位的二进制串 ...
- 【题解】「UVA10116」Robot Motion
Simple Translation 让你模拟一个机器人行走的过程,如果机器人走入了一个循环,输出不是循环的长度和是循环的长度,如果最终走出来了,输出走的步数. Solution 直接模拟即可,本题难 ...