当前位置:网站首页>leetcode 279. Perfect Squares 完全平方數(中等)
leetcode 279. Perfect Squares 完全平方數(中等)
2022-06-22 00:26:00 【InfoQ】
一、題目大意
二、解題思路
三、解題方法
3.1 Java實現
class Solution {
public int numSquares(int n) {
// 找小於n的完全平方數
List<Integer> squares = new ArrayList<>();
for (int i = 1; i < n + 1; i++) {
int tmp = i * i;
if (tmp < n + 1) {
squares.add(tmp);
} else {
break;
}
}
int[] dp = new int[n + 1];
for (int i = 1; i < n + 1; i++) {
dp[i] = Integer.MAX_VALUE;
}
for (int i = 1; i < n + 1; i++) {
for (int square : squares) {
if (i < square) {
break;
}
dp[i] = Math.min(dp[i], dp[i - square] + 1);
}
}
return dp[n];
}
}
四、總結小記
- 2022/6/21 堅持每天一道leetcode,養成一個習慣
边栏推荐
- Buuctf misc spy Apocalypse
- 使用CRD扩展Kubernetes API
- 【设置静态路由】“内网专用外网用WIFi“
- 再次认识 WebAssembly
- AttributeError: ‘WebDriver‘ object has no attribute ‘w3c‘
- You have a chance, here is a stage
- Mathematical knowledge: number of approximations - approximations
- Katalon recoder common commands
- Six little-known SQL technologies in SQL tutorial can help you save 100 hours per month
- Get the value value according to the key in the data
猜你喜欢

buuctf misc 小易的U盘

Im instant messaging source code + software +app with detailed package video building tutorial

Recruitment brochure for traditional product manager international qualification certification (NPDP) in the second half of 2022

HMS Core机器学习服务身份证识别功能,实现信息高效录入

数据库每日一题---第19天:排名靠前的旅行者

buuctf misc zip

火线沙龙第26期-多云安全专场
![[next] the component definition is missing display name in the next JS package](/img/19/2da1ea19987959f8636aa1dbbaae26.png)
[next] the component definition is missing display name in the next JS package
![[taro] the solution of taro wechat applet input focusing the cursor incorrectly on the Apple phone](/img/67/6f46d415475d024950e2502c1f5dc2.png)
[taro] the solution of taro wechat applet input focusing the cursor incorrectly on the Apple phone
![[安洵杯 2019]吹着贝斯扫二维码](/img/38/7bfa5e9b97658acfe24d3aab795bce.png)
[安洵杯 2019]吹着贝斯扫二维码
随机推荐
Katalon framework testing web (XVIII) framework operation
MySQL性能分析工具的使用
SQL tutorial: five SQL skills that data scientists need to master
关于 NFT 和版权的纠结真相
数学知识:最大公约数—约数
WMS warehouse management system source code
buuctf misc zip
【typescript】typscript中感叹号和问号的区别
pytorch可视化
buuctf misc 间谍启示录
Win10使用用户初始密码,连接Win Server失败
【php】mvcs概念(通俗易懂)
vim自动命令事件大全
rabbit:do_ run_ postlaunch_ phase/0 line 932
编写自己的kubernetes controller
rabbit:do_run_postlaunch_phase/0 line 932
关于一次Web线下面试的思考
[actf freshman competition 2020]swp
Introduction to some code static checking tools
[next] the component definition is missing display name in the next JS package