63.Perfect Squares(完美平方数)
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(完美平方数)的更多相关文章
- 279 Perfect Squares 完美平方数
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...) 使得他们的和等于 n.你需要让平方数的个数最少.比如 n = 12,返回 3 ,因为 12 = 4 + 4 + 4 : ...
- 搜索(BFS)---完美平方数
完美平方数 279. Perfect Squares (Medium) For example, given n = 12, return 3 because 12 = 4 + 4 + 4; give ...
- Project Euler 98:Anagramic squares 重排平方数
Anagramic squares By replacing each of the letters in the word CARE with 1, 2, 9, and 6 respectively ...
- LeetCode(279)Perfect Squares
题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...
- 洛谷P1206 [USACO1.2]回文平方数 Palindromic Squares
P1206 [USACO1.2]回文平方数 Palindromic Squares 271通过 501提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 暂时没有 ...
- Palindromic Squares 回文平方数
1.2.4 Palindromic Squares 回文平方数 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 139 Solved: 66[Submit ...
- 洛谷 P1206 [USACO1.2]回文平方数 Palindromic Squares
P1206 [USACO1.2]回文平方数 Palindromic Squares 题目描述 回文数是指从左向右念和从右向左念都一样的数.如12321就是一个典型的回文数. 给定一个进制B(2< ...
- [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 ...
- [LeetCode] Perfect Number 完美数字
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
随机推荐
- 用vbs脚本简易实现 番茄工作法
番茄工作法: 专注于某一段时间,减少打断,提高时间的感知和掌控. 25min工作+5min休息 周期:4x(25+5)+20 VBS代码实现如下: Dim fso,f,count,time,shell ...
- Djano中static和media文件路径的设置
对于常用的css.js.image和常用的工具类在django项目中要设置一个全局的路径,对所有的app都可以访问到这个路径下的文件 1在django项目的setting文件中设置对应的static和 ...
- Linux centos7安装git
1.下载git wget https://github.com/git/git/archive/v2.14.1.zip 2.安装依赖 yum -y install zlib-devel openssl ...
- Ubunton
Ubunton 装完机后 root账户进不去 没密码 在自己账号下 sudo passwd 输入两次密码就是root的密码 之后就可以su root 密码进入了 外部客户端sftp方式连接: sudo ...
- javascript实现下拉菜单的显示与隐藏
demo.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- 【串线篇】Mybatis缓存之缓存查询顺序
1. 不会出现一级缓存和二级缓存中有同一个数据.因为二级缓存是在一级缓存关闭之后才有的 2.任何时候都是先看二级缓存.再看一级缓存,如果大家都没有就去查询数据库,数据库的查询后的结果放在一级缓存中了: ...
- java 时间戳转为时间
Date date = new Date(Long.parseLong(String.valueOf("1560235259477")));SimpleDateFormat for ...
- js中require()的用法----JS如何连接数据库执行sql语句或者建立数据库连接池
var vue = require('vue'); 引入vue的意思,commonjs的写法.node都是用require来载入模块的,可以看看webpack+vue. require()可以调用模块 ...
- 分组函数 partition by 的详解,与order by 区别
partition by关键字是分析性函数的一部分,它和聚合函数(如group by)不同的地方在于它能返回一个分组中的多条记录,而聚合函数一般只有一条反映统计值的记录, partition by ...
- 【leetcode】609. Find Duplicate File in System
题目如下: Given a list of directory info including directory path, and all the files with contents in th ...