Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LeetCode367. Valid Perfect Square判断是否为完全平方数 #算法# 第十二周

LeetCode367. Valid Perfect Square判断是否为完全平方数 #算法# 第十二周

作者头像
梦飞
发布于 2022-06-23 03:26:03
发布于 2022-06-23 03:26:03
38100
代码可运行
举报
文章被收录于专栏:csdn文章同步csdn文章同步
运行总次数:0
代码可运行

原题

Given a positive integer num, write a function which returns True if num is a perfect square else False. 给出一个正整数,写一个函数,若该数为完全平方数,则返回True;否则返回False。 Note: Do not use any built-in library function such as sqrt. 不能使用内建函数,如sqrt。 Example 1:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: 16
Output: true

Example 2:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: 14
Output: false

思路

(1)1+3+5+7+.+(2*n-1) = n^2 (2)完全平方数的末位一定为0,1,4,5,6,9中的一个。

代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
public:
    bool isPerfectSquare(int num) {
        int rest = num % 10;
        if(rest == 2 || rest == 3 || rest == 7 || rest == 8)
            return false;
        int sum = 0;
        for(int i = 1; sum < num; i = i + 2){
            sum += i;
            if(sum == num)
                return true;
        }
        return false;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-12-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
关关的刷题日记88 – Leetcode 367.Valid Perfect Square
关关的刷题日记88 – Leetcode 367.Valid Perfect Square 题目 Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Exampl
WZEARW
2018/04/12
5950
leetcode-367-Valid Perfect Square
题目描述: Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2: Input: 14 Returns: False 要完成的函数: bool
chenjx85
2018/05/21
6140
leetcode-507-Perfect Number
题目描述: We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself. Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not. Example: Input:
chenjx85
2018/05/22
3830
Leetcode 279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9. 计算一个数最少由多少个完全平方数相加而成。
triplebee
2018/01/12
4730
​LeetCode刷题实战507:完美数
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
程序员小猿
2022/03/03
1880
画解算法:507. 完美数
https://leetcode-cn.com/problems/perfect-number/
灵魂画师牧码
2019/07/23
7980
LeetCode 0367 - Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else False.
Reck Zhang
2021/08/11
2950
Dynamic Programming - 279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.
ppxai
2020/09/23
4120
C#版 - Leetcode367. 有效的完全平方数 - 题解
在线提交: https://leetcode.com/problems/valid-perfect-square/
Enjoy233
2019/03/05
8890
【leetcode刷题】T196-有效的完全平方数
https://leetcode-cn.com/problems/valid-perfect-square/
木又AI帮
2019/11/12
3830
有效的完全平方数(C++)
给定一个 正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 true,否则返回 false。
GeekLiHua
2025/01/21
730
LeetCode 367. 有效的完全平方数(二分查找)
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。
Michael阿明
2020/07/13
4270
LeetCode 367. 有效的完全平方数(二分查找)
777. Valid Perfect Square二分查找
Given a positive integer num, write a function which returns Trueif num is a perfect square else False.
和蔼的zhxing
2018/09/04
2890
LeetCode 367. 有效的完全平方数
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。
IT人一直在路上
2019/09/18
3780
【Leetcode -367.有效的完全平方数 -374.猜数字大小】
题目:给你一个正整数 num 。如果 num 是一个完全平方数,则返回 true ,否则返回 false 。
YoungMLet
2024/03/01
1080
C#版 - Leetcode 633. 平方数之和 - 题解
在线提交: https://leetcode.com/problems/sum-of-square-numbers/
Enjoy233
2019/03/05
5960
画解算法:279. 完全平方数
https://leetcode-cn.com/problems/perfect-squares/
灵魂画师牧码
2019/08/01
1.2K0
画解算法:279. 完全平方数
LeetCode Weekly Contest 25 之 507.Perfect Number
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/66473957
用户1147447
2019/05/26
3870
【超直白】leetcode 279 完全平方数
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, …)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。
未名编程
2024/10/12
1430
gcd,哈希问题-LeetCode 357、355、365、367、380
给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n 。
算法工程师之路
2019/12/01
5450
相关推荐
关关的刷题日记88 – Leetcode 367.Valid Perfect Square
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验