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. css中一些文本属性的用法

    代码 /* text-transform用法 */ .p1 { /* 默认值 */ text-transform: none; } .p2 { /* 每个单词的首字母大写 */ text-transf ...

  2. JSON对象与JavaScript对象的区别

    //js对象的字面量表示法: var people1={ name:'hehe', age:18 }; //json的格式是: var people1={ "name":'hehe ...

  3. JS中 [] == ![]结果为true,而 {} == !{}却为false

     为什么? 先转换再比较      (==) 仅比较而不转换  (===) ==转换规则?   ==比较运算符会先转换操作数(强制转换),然后再进行比较 ①如果有一个操作数是布尔值,则在比较相等性之前 ...

  4. Cheatsheet: 2019 07.01 ~ 09.30

    Other Intro Guide to Dockerfile Best Practices QuickJS Javascript Engine Questions for a new technol ...

  5. Python中字符串的格式化

    字符串的格式化 格式化是对字符串进行格式表示的方式.使用槽({})按顺序格式化字符串. 格式化方式 <模板字符串>.format(<逗号分割参数>) "{ }:计算机 ...

  6. C语言获取当前时间

    #include <stdio.h> #include <time.h> void main () { time_t rawtime; struct tm * timeinfo ...

  7. maven 配置自动本地/线上不同配置自动打包

    工程结构:在resource下新建开发,线上不同文件夹存放不同配置文件 pom.xml配置文件 <!-- maven配置不同环境打包 --> <build> <plugi ...

  8. HTML知识梳理(笔记)

    HTML常见元素 meta 定义和用法<meta> 元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. <meta> 标 ...

  9. 02.自定义banner、全局配置文件、@Value获取自定义配置、@ConfigurationProperties、profiles配置

    自定义banner src/main/resource 下新建 banner.txt,字符复制到banner.txt 中 生成字符网站推荐: http://patorjk.com/software/t ...

  10. spark的知识的链接

    IDEA 创建scala spark的Mvn项目:https://blog.csdn.net/u014646662/article/details/84618032 Spark详解03Job 物理执行 ...