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 ...
随机推荐
- Sql server用QQ邮箱发送邮件
一.配置数据库邮件 https://jingyan.baidu.com/article/3ea51489a135f752e71bba5b.html
- mysql 建立表之间关系 一对一 练习2
创建db5数据库 create database db5 charset=utf8; use db5; 例二:一个管理员唯一对应一个用户 用户表: id user password 1 egon xx ...
- python安装virtualenv
pip install virtualenv 为了使用更方便用,安装另外一个,windows下要-win,linux下不用 pip install virtualenvwrapper-win 使用方法 ...
- 从零到一创建ionic移动app:应用anjularjs编写ionic项目
推荐两篇文章,带你入门 ionic中文项目(作为了解ionic基础结构用):http://blog.csdn.net/i348018533/article/details/47258449/ ioni ...
- Educational Codeforces Round 11C. Hard Process two pointer
地址:http://codeforces.com/contest/660/problem/C 题目: You are given an array a with n elements. Each el ...
- python中多重继承与获取对象
1.python中多重继承 除了从一个父类继承外,Python允许从多个父类继承,称为多重继承. 多重继承的继承链就不是一棵树了,它像这样: class A(object): def __init__ ...
- 物理机内存模型与java内存模型
多线程缓存一致性问题 程序在运行过程中,会将运算需要的数据从主存复制一份到CPU的高速缓存当中,那么CPU进行计算时就可以直接从它的高速缓存读取数据和向其中写入数据,当运算结束之后,再将高速缓存中的数 ...
- 20145222黄亚奇《网络对抗》MSF基础应用
实践目标 掌握metasploit的基本应用方式. 具体需要完成(1)ms08_067;(2)ms11_050:(3)Adobe(4)成功应用任何一个辅助模块. 实验内容 掌握metasploit的基 ...
- 欧拉筛法(phi,d,prime)
筛法求素数的核心就是让每个合数被它的最小质因子筛掉,那么剩下来的就是素数了. 于是在这个过程中我们可以顺便求出每个数的φ().d().e(). ϕ:小于等于该数的与它互质的数的个数(一个数与其自身互质 ...
- iOS 开发 申请定位
在iOS8以后,苹果已经强制开发者在请求定位服务时获得用户的授权,此外iOS状态栏中还有指示图标,提示用户当前应用是否正在使用定位服务.另外在iOS8以后,苹果进一步改善了定位服务,让开发者请求定位服 ...