一、Perfect Squares 完全平方数
一原题
Given a positive integer n, find the least number of perfect square numbers (for example, , , , , ...) which sum to n. For example, given n = , return because = + + ; given n = , return because = + .
二、中文讲解
、这道题说是给我们一个正整数,求它最少能由几个完全平方数组成。这道题是考察四平方和定理
、根据四平方和定理,任意一个正整数均可表示为4个整数的平方和,其实是可以表示为4个以内的平方数之和,
那么就是说返回结果只有1,,3或4其中的一个,首先我们将数字化简一下,由于一个数如果含有因子4,那么我们
可以把4都去掉,并不影响结果,比如2和8,3和12等等,返回的结果都相同
、还有一个可以化简的地方就是,如果一个数除以8余7的话,那么肯定是由4个完全平方数组成
、那么做完两步后,一个很大的数有可能就会变得很小了,大大减少了运算时间,下面我们就来尝试的将其拆为
两个平方数之和,如果拆成功了那么就会返回1或2,因为其中一个平方数可能为0. (注:由于输入的n是正整数,所以不存在两个平方数均为0的情况)。注意下面的!!a + !!b这个表达式,可能很多人不太理解这个的意思,其实很简单,感叹号!表示逻辑取反,那么一个正整数逻辑取反为0,再取反为1,所以用两个感叹号!!的作用就是看a和b是否为正整数,都为正整数的话返回2,只有一个是正整数的话返回1
三、代码
class Solution:
# @param {int} n a positive integer
# @return {int} an integer
def numSquares(self, n):
# Write your code here
while n % 4 == 0:
n /= 4
if n % 8 == 7:
return 4 for i in xrange(n+1):
temp = i * i
if temp <= n:
if int((n - temp)** 0.5 ) ** 2 + temp == n:
return 1 + (0 if temp == 0 else 1)
else:
break
return 3
其他代码解法:
class Solution {
public:
int numSquares(int n) {
while (n % == ) n /= ;
if (n % == ) return ;
for (int a = ; a * a <= n; ++a) {
int b = sqrt(n - a * a);
if (a * a + b * b == n) {
return !!a + !!b;
}
}
return ;
}
};
一、Perfect Squares 完全平方数的更多相关文章
- [LeetCode] 0279. Perfect Squares 完全平方数
题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...
- [LintCode] Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- [LeetCode] 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 ...
- Leetcode279. Perfect Squares完全平方数
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数的个数最少. 示例 1: 输入: n = 12 输出: 3 解释: 12 ...
- LeetCode 279. 完全平方数(Perfect Squares) 7
279. 完全平方数 279. Perfect Squares 题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数 ...
- Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares)
Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ar ...
- CF914A Perfect Squares
CF914A Perfect Squares 题意翻译 给定一组有n个整数的数组a1,a2,…,an.找出这组数中的最大非完全平方数. 完全平方数是指有这样的一个数x,存在整数y,使得x=y^2y2 ...
- LeetCode Perfect Squares
原题链接在这里:https://leetcode.com/problems/perfect-squares/ 题目: Given a positive integer n, find the leas ...
随机推荐
- 解决cp: omitting directory 提示信息
解决cp: omitting directory 提示信息 执行cp时出现“cp: omitting directory ” 提示信息, 可以使用cp -r 参数来递归拷贝这些文件.
- C#中的里氏替换原则
里氏转换原则 子类可以赋值给父类对象 父类对象可以强制转化为对应的子类对象 里氏替换原则直观理解就是"子类是父类",反过来就说不通了. 就像男人是人对的,但人是男人就不对了. 这样 ...
- eclipse 创建maven web错误Cannot change version of project facet Dynamic web module to 3.1解决方案
Dynamic Web Module 选择“3.1”,java选择“1.8”,报错:Cannot change version of project facet Dynamic web module ...
- C语言小板凳(1)
①strlen()函数作用:计算字符串的长度,当遇到"\n"字符时结束,即遇到数值"0"时结束计算,有一点特别要注意当这个函数用来计算数组的长度的时候遇到数值0 ...
- intelligent_cam
https://github.com/shengkaisun/intelligent_cam/tree/772fe0e4d315f83ba01134389c6b618b1ce40aaf intelli ...
- Trie树学习
这几天在看Hadoop的排序,用到了有TotalSortPartition,其中用到了一种叫做trie树的数据结构,每次看到这种自己之前没有听过的数据结构就想去看一下原理,然后再网上看几篇博客,有时间 ...
- 微信支付v3开发(6) 收货地址共享接口
请看新版教程 微信支付开发(7) 收货地址共享接口V2 本文介绍微信支付下的收货地址共享接口的开发过程. 一. 简单介绍 微信收货地址共享,是指用户在微信浏览器内打开网页,填写过地址后,兴许能够免填 ...
- 通过pyenv进行多版本python管理
1.安装pyenv brew install pyenv 2.配置.zshrc文件 export PYENV_ROOT=/usr/local/var/pyenv if which pyenv > ...
- 把flask部署到服务器
1.新建一个wsgi.py文件 # -*- coding:utf-8 -*- import sys from os.path import abspath from os.path import di ...
- windows svchost.exe 引起的出现的莫名其妙的窗口失去焦点
我不知道你们遇到没,反正我是遇到了,现在我就把解决方法给你们,当然都是从网上整理下来的 这个失去焦点可以分为两种,一种是病毒,一种是系统自带的问题 首先你得知道自己的窗口被什么给挤掉了焦点 先看看这篇 ...