题目

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.

分析

完美平方数,给定任意数n,它可表示为多个平方数(如1,4,9,16…)的和,求出表示出任意数n所需的平方数的最少个数。

考察动态规划,

如果一个数x可以表示为一个任意数a加上一个平方数b∗b,也就是x=a+b∗b,那么能组成这个数x最少的平方数个数,就是能组成a最少的平方数个数加上1(因为b∗b已经是平方数了)。

AC代码

class Solution {
public:
/* 如果一个数x可以表示为一个任意数a加上一个平方数bxb,也就是x = a + bxb,
* 那么能组成这个数x最少的平方数个数,就是能组成a最少的平方数个数加上1(因为b*b已经是平方数了)。
*/
int numSquares(int n) {
// 将所有非平方数的结果置最大,保证之后比较的时候不被选中
vector<int> nums(n + 1, INT_MAX); // 将所有整平方数的结果置1
for (int i = 0; i*i <= n; ++i)
{
nums[i*i] = 1;
}//for // 从小到大找任意数a
for (int a = 0; a <= n; ++a)
{
// 从小到大找平方数b*b
for (int b = 0; a + b*b <= n; ++b)
{
// 因为a+b*b可能本身就是平方数,所以我们要取两个中较小的
nums[a + b*b] = min(nums[a] + 1, nums[a + b*b]);
}//for
}//for
return nums[n];
}
};

GitHub测试程序源码

LeetCode(279)Perfect Squares的更多相关文章

  1. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  2. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  3. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  4. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  5. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  6. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  7. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  8. LeetCode(4)Median of Two Sorted Arrays

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  9. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

随机推荐

  1. Jmeter4.0----监控服务器性能(7)

    1.说明 JMeter是一款压力测试工具. 通常在压力测试中我们也需要监控和知道服务器的相关资源情况,jmeter本身不具备这个功能,今天我们主要说一下如何通过JMeter插件来监控服务器CPU.内存 ...

  2. 博弈论 && 题目

    终于我也开始学博弈了,说了几个月,现在才学.学多点套路,不深学.(~~) 参考刘汝佳蓝书p132 nim游戏: 假设是两维的取石子游戏,每次可以在任意一堆拿任意数量个(至少一根,因为这样游戏的状态集有 ...

  3. 牛客网Java刷题知识点之子类继承不了父类里的(private属性、private方法、构造方法)

    不多说,直接上干货! 子类可以继承父类的属性和方法,除了那些private的外还有一样是子类继承不了的---构造器.对于构造器而言,它只能够被子类调用,而不能被子类继承. 调用父类的构造方法我们使用s ...

  4. 《javascript设计模式》笔记之第十章 和 第十一章:门面模式和适配器模式

    第十章:门面模式 一:门面模式的作用 简化已有的api,使其更加容易使用 解决浏览器的兼容问题 二:门面模式的本质 门面模式的本质就是包装已有的api来简化操作   三:门面模式的两个简单例子 下面这 ...

  5. nuxt实践

    利用手脚架搭起来的服务端渲染实例目录结构.nuxtassets 未编译的静态资源如 LESS.SASS 或 JavaScriptcomponents 用于组织应用的 Vue.js 组件middlewa ...

  6. 7.Solution的Build、Rebuild和Clean

    大家好,我是原文,这篇随笔是对原文的翻译以及自己的体会. 做程序员没追求的话是永远找不到女朋友的,当然有追求也找不到,这个先不提,好在有追求的时候我是充实而且开心的.现在我们的问题是,每天调试项目,在 ...

  7. String类、StringBuilder类、StringBuffer类

    String类是不可变类,创建了一个String对象后,该String对象中的字符序列不能被改变. StringBuilder是字符序列可变的字符串类,提供了一系列修改字符串的方法. StringBu ...

  8. 关于ECSHOP中sql注入漏洞修复

    标签:ecshop sql注入漏洞修复 公司部署了一个ecshop网站用于做网上商城使用,部署在阿里云服务器上,第二天收到阿里云控制台发来的告警信息,发现ecshop网站目录下文件sql注入漏洞以及程 ...

  9. SQL Server数据库所有表重建索引

    USE My_Database;DECLARE @name varchar(100) DECLARE authors_cursor CURSOR FOR  Select [name]   from s ...

  10. jmeter的安装和基本使用

    本篇文章主要介绍一下JMeter的安装及基本使用方法. 1.安装 JMeter的官方网址为http://jmeter.apache.org/ 下载地址为http://jmeter.apache.org ...