279 Perfect Squares 完美平方数
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...) 使得他们的和等于 n。你需要让平方数的个数最少。
比如 n = 12,返回 3 ,因为 12 = 4 + 4 + 4 ; 给定 n = 13,返回 2 ,因为 13 = 4 + 9。
详见:https://leetcode.com/problems/perfect-squares/description/
Java实现:
方法一:递归实现
- class Solution {
- public int numSquares(int n) {
- int res = n, num = 2;
- while (num * num <= n) {
- int a = n / (num * num), b = n % (num * num);
- res = Math.min(res, a + numSquares(b));
- ++num;
- }
- return res;
- }
- }
方法二:动态规划
如果一个数x可以表示为一个任意数a加上一个平方数bxb,也就是x=a+bxb,那么能组成这个数x最少的平方数个数,就是能组成a最少的平方数个数加上1(因为b*b已经是平方数了)。
- class Solution {
- public int numSquares(int n) {
- int[] dp = new int[n+1];
- // 将所有非平方数的结果置最大,保证之后比较的时候不被选中
- Arrays.fill(dp, Integer.MAX_VALUE);
- // 将所有平方数的结果置1
- for(int i = 0; i * i <= n; i++){
- dp[i * i] = 1;
- }
- // 从小到大找任意数a
- for(int a = 0; a <= n; a++){
- // 从小到大找平方数bxb
- for(int b = 0; a + b * b <= n; b++){
- // 因为a+b*b可能本身就是平方数,所以我们要取两个中较小的
- dp[a + b * b] = Math.min(dp[a] + 1, dp[a + b * b]);
- }
- }
- return dp[n];
- }
- }
C++实现:
方法一:
- class Solution {
- public:
- int numSquares(int n) {
- while(n%4==0)
- {
- n/=4;
- }
- if(n%8==7)
- {
- return 4;
- }
- for(int a=0;a*a<=n;++a)
- {
- int b=sqrt(n-a*a);
- if(a*a+b*b==n)
- {
- return !!a+!!b;
- }
- }
- return 3;
- }
- };
方法二:
- class Solution {
- public:
- int numSquares(int n) {
- vector<int> dp(n+1,INT_MAX);
- dp[0]=0;
- for(int i=0;i<=n;++i)
- {
- for(int j=1;i+j*j<=n;++j)
- {
- dp[i+j*j]=min(dp[i+j*j],dp[i]+1);
- }
- }
- return dp.back();
- }
- };
方法三:
- class Solution {
- public:
- int numSquares(int n) {
- vector<int> dp(1, 0);
- while (dp.size() <= n)
- {
- int m = dp.size(), val = INT_MAX;
- for (int i = 1; i * i <= m; ++i)
- {
- val = min(val, dp[m - i * i] + 1);
- }
- dp.push_back(val);
- }
- return dp.back();
- }
- };
参考:https://www.cnblogs.com/grandyang/p/4800552.html
279 Perfect Squares 完美平方数的更多相关文章
- 搜索(BFS)---完美平方数
完美平方数 279. Perfect Squares (Medium) For example, given n = 12, return 3 because 12 = 4 + 4 + 4; give ...
- 63.Perfect Squares(完美平方数)
Level: Medium 题目描述: Given a positive integer n, find the least number of perfect square numbers (f ...
- 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, 1 ...
- 【LeetCode】279. Perfect Squares 解题报告(C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 四平方和定理 动态规划 日期 题目地址:https: ...
- (BFS) leetcode 279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- 279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- leetcode@ [279]Perfect Squares
https://leetcode.com/problems/perfect-squares/ Given a positive integer n, find the least number of ...
- 279. Perfect Squares(动态规划)
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
随机推荐
- Web 后端编程的几个关键点(总结中...)
基础 服务端结构 服务器如何部署,负载均衡,代理技术,如何向B端提供服务? 分布式架构 与前端界面的交互形式 数据 CURD 表之间的关联 较为棘手 如何将一对多 多对多的概念进行 面向对象 描述 前 ...
- html5视频播放器 一 (改写默认样式)
一个项目用到了html5视频播放器,于是就写了一个,走了很多坑,例如在chrome中加载视频出现加载异常等 先看看效果 是不是感觉换不错,以下是我播放器改写样式的布局. <!DOCTYPE ht ...
- Oracle 设置用户密码永不过期
--1.查看用户的proifle,一般是default select username,profile from dba_users; --2.查看概要文件(default)的密码有效期设置 sele ...
- ArcGIS for Android离线数据编辑实现原理
来自:http://blog.csdn.net/arcgis_mobile/article/details/7565877 ArcGIS for Android中现已经提供了离线缓存图片的加载功能,极 ...
- Django学习系列之Cookie、Session
Cookie和Session介绍 cookie 保存在客户端 session 保存在服务端 session依赖于cookie,比如服务端想往客户端写东西的时候就把cookie写到客户端浏览器 djan ...
- Android插屏动画效果
公司研发SDK,须要类似有米插屏的动画效果,研究了下,写了一个DEMO,凝视非常具体了. <span style="font-size:24px;">package c ...
- MariaDB ----单表查询
1>按一定条件查询某字段的数据 (where) ; ( 查询 id > 的数据) #补充: ; 注意“ select * from students1: (此命令需谨慎使用, 数据量大 ...
- OSS与文件系统的对比
基本概念介绍_开发指南_对象存储 OSS-阿里云 https://help.aliyun.com/document_detail/31827.html 强一致性 Object 操作在 OSS 上具有 ...
- HDU 5832A water problem
大数 判断整除 /* *********************************************** Author :guanjun Created Time :2016/8/14 1 ...
- linux常见基础问题
1,32位与64位的区别,怎么查看系统版本? 32位相比于64位处理速度更慢一些,64位同样也比32位更占内存.用户体验上没有区别:用uname -a 查看系统版本信息 2,swap分区的作用是什么 ...