Level:

  Medium

题目描述:

Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.

Example 1:

Input: n = 12
Output: 3
Explanation: 12 = 4 + 4 + 4.

Example 2:

Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.

思路分析:

  根据四平方和定理,任意一个正整数均可表示为4个整数的平方和,其实是可以表示为4个以内的平方数之和,那么就是说返回结果只有1,2,3或4其中的一个,首先我们将数字化简一下,由于一个数如果含有因子4,那么我们可以把4都去掉,并不影响结果,比如2和8,3和12等等,返回的结果都相同,读者可自行举更多的栗子。还有一个可以化简的地方就是,如果一个数除以8余7的话,那么肯定是由4个完全平方数组成,这里就不证明了,因为我也不会证明,读者可自行举例验证。那么做完两步后,一个很大的数有可能就会变得很小了,大大减少了运算时间,下面我们就来尝试的将其拆为两个平方数之和,如果拆成功了那么就会返回1或2,因为其中一个平方数可能为0.

代码:

public class Solution{
public int numSquares(int n){
//首先将数字化简
while(n%4==0){
n=n/4;
}
if(n%8==7)
return 4;
for(int a=0;a*a<=n;a++){
int b=(int)Math.sqrt(n-a*a);
if(a*a+b*b==n){
if(a==0||b==0)
return 1;
else
return 2;
}
}
return 3;
}
}

63.Perfect Squares(完美平方数)的更多相关文章

  1. 279 Perfect Squares 完美平方数

    给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...) 使得他们的和等于 n.你需要让平方数的个数最少.比如 n = 12,返回 3 ,因为 12 = 4 + 4 + 4 : ...

  2. 搜索(BFS)---完美平方数

    完美平方数 279. Perfect Squares (Medium) For example, given n = 12, return 3 because 12 = 4 + 4 + 4; give ...

  3. Project Euler 98:Anagramic squares 重排平方数

    Anagramic squares By replacing each of the letters in the word CARE with 1, 2, 9, and 6 respectively ...

  4. LeetCode(279)Perfect Squares

    题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...

  5. 洛谷P1206 [USACO1.2]回文平方数 Palindromic Squares

    P1206 [USACO1.2]回文平方数 Palindromic Squares 271通过 501提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 暂时没有 ...

  6. Palindromic Squares 回文平方数

    1.2.4 Palindromic Squares 回文平方数 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 139  Solved: 66[Submit ...

  7. 洛谷 P1206 [USACO1.2]回文平方数 Palindromic Squares

    P1206 [USACO1.2]回文平方数 Palindromic Squares 题目描述 回文数是指从左向右念和从右向左念都一样的数.如12321就是一个典型的回文数. 给定一个进制B(2< ...

  8. [LeetCode] Sum of Square Numbers 平方数之和

    Given a non-negative integer c, your task is to decide whether there're two integers a and b such th ...

  9. [LeetCode] Perfect Number 完美数字

    We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...

随机推荐

  1. react native 打包至iphone设备

    1.新建bundle 在自己项目的ios文件夹下新建一个文件夹取名bundle PS:ios文件夹和node_modules文件夹在同一级目录下,这个bundle文件夹名称随意取,后面要用到,但是记得 ...

  2. bounds与frame的区别及setBounds的使用

    转自http://www.cocoachina.com/ios/20140925/9755.html 在iOS开发中经常遇到两个词Frame和bounds,本文主要阐述Frame和bound的区别,尤 ...

  3. presentingViewController、presentedViewController区别

    解释两个属性:presentingViewController 和 presentedViewController A----(present)-----B----(present)-----C 1. ...

  4. Beta阶段成果展示——第八组

    Beta阶段成果展示 游戏公网IP:http://119.29.32.204/krad.html(欢迎大家测试!) Beta阶段体现在成果上的工作主要为界面美化,玩家引导,按键封闭等等. 本文将以截图 ...

  5. php中__call与__callstatic()使用

    php 5.3 后新增了 __call 与__callStatic 魔法方法. __call 当要调用的方法不存在或权限不足时,会自动调用__call 方法. __callStatic 当调用的静态方 ...

  6. rabbitmq windows安装 及 centos安装

     windows安装如下: 安装方法如下网址: https://baijiahao.baidu.com/s?id=1605656085633071281&wfr=spider&for= ...

  7. C# 事务的创建,提交和回滚

    在C#中开启事务的步骤 01.调用SqlConnection对象的BeginTransaction()方法,创建一个SqlTransaction对象,标志事务开始. 02.将创建的SqlTransac ...

  8. MongoDB 存储引擎选择

    MongoDB存储引擎选择 MongoDB存储引擎构架 插件式存储引擎, MongoDB 3.0引入了插件式存储引擎API,为第三方的存储引擎厂商加入MongoDB提供了方便,这一变化无疑参考了MyS ...

  9. canvas 点击图片播放视频

    canvas.js window.onload=function() { var canvas = document.getElementById('canvas'); var ctx= canvas ...

  10. vue的列表交错过渡

    参考文章 https://juejin.im/post/5cccf5b0e51d453a907b4af1