279. 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.
代码如下:
public class Solution {
public int numSquares(int n) {
if(n<=2)
return n;
int min=n,t=0;
for(int i=2;i*i<=n;i++)
{
int c=i*i;
if(n%c==0)
t=n/c;
else
t=n/c+numSquares(n%c);
if(t<min)
min=t;
}
return min;
}
}
279. Perfect Squares的更多相关文章
- leetcode@ [279]Perfect Squares
https://leetcode.com/problems/perfect-squares/ Given a positive integer n, find the least number of ...
- (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 完全平方数
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: ...
- 279 Perfect Squares 完美平方数
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...) 使得他们的和等于 n.你需要让平方数的个数最少.比如 n = 12,返回 3 ,因为 12 = 4 + 4 + 4 : ...
- [leetcode] #279 Perfect Squares (medium)
原题链接 题意: 给一个非整数,算出其最少可以由几个完全平方数组成(1,4,9,16……) 思路: 可以得到一个状态转移方程 dp[i] = min(dp[i], dp[i - j * j] + ) ...
- [刷题] 279 Perfect Squares
要求 给出一个正整数n,寻找最少的完全平方数,使他们的和为n 示例 n = 12 12 = 4 + 4 + 4 输出:3 边界 是否可能无解 思路 贪心:12=9+1+1+1,无法得到最优解 图论:从 ...
- LeetCode 279. 完全平方数(Perfect Squares) 7
279. 完全平方数 279. Perfect Squares 题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数 ...
随机推荐
- 10 notorious computer virus
The history of computer virus is the same as computer history. With more and more powerful computers ...
- 【STL】-auto_ptr的用法
初始化: #include<memory> //auto_ptr header void f() { auto_ptr<classA> ptr(new classA); } 拷 ...
- UIScrollerView常见属性
CGSize contentSize :设置UIScrollView的滚动范围 CGPoint contentOffset :UIScrollView当前滚动的位置 UIEdgeInsets cont ...
- yum源的更新问题
我们知道在linux下安装软件的方法有多种多样,其中利用yum的方式来安装较为简单,但需要等待的时间比较长.下面介绍一下如何更新yum的源的问题. 首先需要保证的是linux的机器能上网.然后按照下面 ...
- jQuery中 end(); 的用法
jQuery中的end()方法的意思 选取某个元素,查找选取其子元素,然后再回过来选取这个元素.用例子说明了一下: 比如HTML代码: <p><span>Hello</s ...
- Python inspect
inspect — Inspect live objects New in version 2.1. The inspect module provides several useful functi ...
- bistu新生-1005
#include "stdio.h"#include "string.h"int main(){ char ku[]={'0','1','2','3','4', ...
- 为自己的系统定制openstack ceilometer
一.目的 最近研究了一下ceilometer,目的做一个监控系统,对系统中发生的事件进行处理.ceilometer对openstack各个组件信息的收集方式主要由 推 和 拉 两种. “推”: 就是 ...
- python中的面向对象编程
在python中几乎可以完成C++里所有面向对象编程的元素. 继承:python支持多继承: class Derived(base1, base2, base3): pass 多态:python中的所 ...
- Python变量与常量
变量是计算机内存中的一块区域,变量可以存储规定范围内的值,而且值可以改变.基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中.常量是一块只读的内存区域,常量一旦被初始化就不能被 ...