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 ...
随机推荐
- java多线程总结(二)
线程一般有6个状态: 新建状态:NEW 可运行状态:RUNNABLE 休眠状态:TIMED_WAITING 等待状态:WAITING 阻塞状态:BLOCKED 终止状态“TERMINATED 当我们使 ...
- iOS学习之应用偏好设置
如今,即便是最简单的计算机程序也会包含一个偏好设置窗口,用户可以在其中设置应用专属的选项.在MAC OS X中,Preferences...菜单通常位于应用菜单中.选择该菜单项会弹出一个窗口,用户可以 ...
- redis的一些命令
字符串操作 EX在设置值的时候设置过期时间,ttl查看过期时间 expire能单独设置过期时间 查看所有的key key * 列表操作 lpush从列表左边添加值,rpush从列表右边添加值 lran ...
- caffe训练自己的数据集
默认caffe已经编译好了,并且编译好了pycaffe 1 数据准备 首先准备训练和测试数据集,这里准备两类数据,分别放在文件夹0和文件夹1中(之所以使用0和1命名数据类别,是因为方便标注数据类别,直 ...
- OpenCL 学习step by step (5) 使用二维NDRange workgroup
http://www.cnblogs.com/mikewolf2002/archive/2012/09/07/2675634.html 在本教程中,我们使用二维NDRange来设置workgroup, ...
- linux驱动分层分离思想
转:https://blog.csdn.net/zqixiao_09/article/details/51088887 前面我们学习I2C.USB.SD驱动时,有没有发现一个共性,就是在驱动开发时,每 ...
- 【Head First Servlets and JSP】笔记
1.谈到服务器的时候,可能是指物理主机(硬件),也可能是指Web服务应用(软件). 2.谈到客户的时候,通常指人类用户,或者是浏览器应用,或者两者都包括,浏览器应用做些什么?发送请求.解释HTML和呈 ...
- 阻塞方法与InterruptedException
什么是阻塞方法?为什么会抛出InterruptedException? 一般方法的完成只取决于它所要做的事情,以及是否有足够多可用的计算资源(CPU 周期和内存). 而阻塞方法的完成还取决于一些外部的 ...
- React生命周期及事件详解
引用原文:http://blog.csdn.net/limm33/article/details/50942808 一.组件的详细说明和生命周期ComponentSpecs and Lifecycle ...
- sql 加密解密函数
if object_ID ( 'fn_ACITEncryption' ) is not null drop function fn_ACITEncryption go create ...