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 ...
随机推荐
- HDU1158:Employment Planning(线性dp)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1158 这题又是看了题解,题意是一项工作需要n个月完成,雇佣一个人需要m1的钱,一个人的月工资为sa,辞退一 ...
- 用python简便地抓取刘昊然的写真(17行代码)
17行python代码抓取刘昊然图片之家的写真 用python来爬取网页信息是很简便的.因为它有很多库来帮助我们实现我们想要的功能.本实验用到的库有:requests和bs4中的BeautifulSo ...
- pycharm修改配置
恢复pycharm的初始设置
- Selenium WebDriver 2.34.0 发布,支持Firefox22
Selenium WebDriver 2.34.0 发布,支持Firefox22http://automationqa.com/forum.php?mod=viewthread&tid=270 ...
- Linux Shell编程 exit、break、continue
exit语句 在系统中exit 命令用于退出当前用户的登录状态.在 Shell 脚本中exit 语句是用来退出当前脚本. exit 的语法如下: exit [返回值] 如果在 exit 之后定义了返回 ...
- rest_framework解析器组件源码流程
rest_framework解析器组件源码流程 解析器顾名思义就是对请求体进行解析.为什么要有解析器?原因很简单,当后台和前端进行交互的时候数据类型不一定都是表单数据或者json,当然也有其他类型的数 ...
- Mysql:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Mysql:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) Linux: MyS ...
- tesseract 3.05 release 编译
tesseract 3.05 release版本的对应配置好的vs2015工程.偷懒必备,毕竟依赖那么多库,环境配置还是要费点事的.https://github.com/peirick/VS2015_ ...
- RN app打包
最近使用React Native做起了移动应用,之前做过一点react,有一点react基础,后来听说RN还不错,就做起了RN项目.为了让辛辛苦苦开发的项目想在手机端运行,就涉及到发布打包. 防止自己 ...
- Jave基础之选择排序
选择排序(Selection sort) 基本介绍 选择排序: 每一次从未排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后再从剩余未排序的元素中选出最小(或最大的)放在已排序序 ...