LeetCode OJ: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.
题目如上,实际上相当于一个常规的背包问题,关于背包问题可以看看这篇帖子 动态规划0—1背包问题,讲的很好(PS:这个帖子用的ppt的背景我就感觉像是我们学校了,真是巧了。。。。).代码如下,下面都是有注释的:
class Solution {
public:
int numSquares(int n) {
vector<int> pSqr(n + , );
for (int i = ; i < n + ; ++i){
pSqr[i] = i;//这里取i一定是最大值了,应为至少也可以全由1来组成,相当于背包问题里面的0
}
for (int i = ; i < n + ; ++i){
for (int j = ; j <= sqrt(i); ++j){//从2开始是有原因的,应为i= 1,2,3的时候就是pSqr的值,这里就不用算了
if (j * j == i){ pSqr[i] = ; break; }
pSqr[i] = min(pSqr[i], + pSqr[i - j * j]);//这一步是关键
}
}
return pSqr[n];
}
};
这个博客上讲的很多也帮了我不少,mark一下。
java代码与上面的基本上都是相同的,代码如下所示:
public class Solution {
public int numSquares(int n) {
int [] dp = new int[n+1];
for(int i = 0; i < n + 1; ++i){
dp[i] = i;
}
for(int i = 0; i < n+1; ++i){
for(int j = 2; j <= Math.sqrt(i); ++j){
if(j*j == i){
dp[i] = 1;
break;
}
dp[i] = Math.min(dp[i], 1 + dp[i - j * j]);
}
}
return dp[n];
}
}
LeetCode OJ:Perfect Squares(完美平方)的更多相关文章
- 279 Perfect Squares 完美平方数
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...) 使得他们的和等于 n.你需要让平方数的个数最少.比如 n = 12,返回 3 ,因为 12 = 4 + 4 + 4 : ...
- [LeetCode] 0279. Perfect Squares 完全平方数
题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...
- [LeetCode] 279. Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- [LeetCode] 507. Perfect Number 完美数字
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- leetcode@ [279]Perfect Squares
https://leetcode.com/problems/perfect-squares/ Given a positive integer n, find the least number of ...
- 【leetcode】Perfect Squares (#279)
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- (BFS) 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 (medium)
原题链接 题意: 给一个非整数,算出其最少可以由几个完全平方数组成(1,4,9,16……) 思路: 可以得到一个状态转移方程 dp[i] = min(dp[i], dp[i - j * j] + ) ...
- Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩
题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...
- 花式求解 LeetCode 279题-Perfect Squares
原文地址 https://www.jianshu.com/p/2925f4d7511b 迫于就业的压力,不得不先放下 iOS 开发的学习,开始走上漫漫刷题路. 今天我想聊聊 LeetCode 上的第2 ...
随机推荐
- 【Spring MVC】spring mvc中相同的url请求返回不同的结果
在项目中凡是使用Spring MVC这种控制器的,大多都是返回JSON数据对象,或者JSP页面. 但是相同的URL请求如何让他自动的选择放回的是什么? 在这里有由于鄙人没有亲自测试过,就不敢乱贴代码, ...
- http post url参数封装(key token 及校验码)
post请求本来是一种很常见的web请求方式,相信许多项目都有一系列的封装工具类. 今天遇着一个特殊的需求. 需要在post的请求url内封装相应的token 与及key相关的值,这就奇怪了,url封 ...
- HDOJ 2203 亲和串 【KMP】
HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- java springboot整合zookeeper入门教程(增删改查)
java springboot整合zookeeper增删改查入门教程 zookeeper的安装与集群搭建参考:https://www.cnblogs.com/zwcry/p/10272506.html ...
- ajax 事件使用
error: function (XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.status); alert(XMLH ...
- win10安装z3求解器
因为课程要求,我不得不接触求解器,之前有在ubuntu上装过一个叫stp的求解器,没怎么用: 今天在我的电脑(win10)上上装了一款更方便的求解器---z3,下面先详细介绍一下怎么安装和配置: 1. ...
- openstack认证实践
环境: 客户端:负责发送请求, 服务器:负责接受请求: 认证服务器keystone:负责认证 具体认证步骤: 1.客户端首先进行签名计算,将得到的签名字符串作为authorization发给keyst ...
- ASP.NET的内置对象
Request 该对象用于检索从浏览器向服务器所发送的请求中的信息.在按下“提交”按钮时,Request对象将读取和提取通过HTTP请求发送的参数.在用户提交表单时,包含在输入控件中的数据将与表单 ...
- JAVA基础补漏--继承
子类的对象在创建时,首先调用父类的构造函数,再调用子类自己的构造函数. 子类的构造函数中,有一个默认的super(),为一个无参调用,这个不显示,但会被首先调用,所有才会有父类构造函数被调用的情况. ...
- ixgbe RSS原理分析
这个月,一直在搞ixgbe RSS,希望能使得收包均衡,结果没成功,但是对网卡的收包原理理解得更深入些. 1.网卡硬件通过网线或者光纤收包. 2.网卡的RSS功能根据网络五元组计算得到32bit的ha ...