279. Perfect Squares
2016-03-21 11:10
393 查看
Given a positive integer n, find the least number of perfect square numbers (for example,
For example, given n =
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
背包
1, 4, 9, 16, ...) which sum to n.
For example, given n =
12, return
3because
12 = 4 + 4 + 4; given n =
13, return
2because
13 = 4 + 9.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
背包
public class Solution { public int numSquares(int n) { int [] a = new int [n+1]; int i = 1; while(i*i<=n){ a[i*i]=1; i++; } for(int b = 2;b<=n;b++){ i=1; if(a[b]==0){a[b]=a[b-1]+1;} while(i*i<b){ int x= 1+a[b-i*i]; if(x<a[b])a[b]=x; i++; } } return a ; } }
相关文章推荐
- 分享 13 个帮助你简化开发的 jQuery 插件
- 用jquery控制图片的最大宽度
- css图片自适应浏览器宽度
- jQuery中的$(window).load()与$(document).ready()
- jquery.lazyloading图片延迟加载插件,通用
- jquery 判断图片加载完毕例子
- js 屏蔽鼠标右键,复制,黏贴,保存
- 使背景图片不缩放水平居中样式
- <a>标签中href=\"javascript:;\"表示什么意思??
- css样式属性加前加*什么意思
- jquery中this与$(this)的用法区别.
- CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate)
- CSS3中的动画功能之transtions和animations详解
- Daum Equation Editor(公式编辑器)
- js 如何获得ckeditor的值
- javascript 全局变量,私有变量。
- JSON 教程
- 把 JSON 文本转换为 JavaScript 对象
- 在Javascript中 声明时用\"var\"与不用\"var\"的区别
- 使用JS实现单击连接保存图片