当前位置:网站首页>leetcode: 254. Combinations of factors
leetcode: 254. Combinations of factors
2022-08-04 14:37:00 【OceanStar study notes】
题目来源
题目描述

class Solution {
public:
vector<vector<int>> getFactors(int n){
}
};
题目解析
题意
给定一个正整数n,Write the form of multiplying all factors,It also specifies that the factors are arranged in order from small to large
思路
- Because of the need to list all the cases,So it can be solved by backtracking
- As stated in the title1和ncannot be counted as a factor by itself,那么可以从2开始遍历到n,如果当前的数i可以被n整除,说明i是n的一个因子,Store it in a one-bit array out 中,然后递归调用 n/i,not at this time2开始遍历,而是从i遍历到 n/i
- The stop condition is whenn等于1时,如果此时 out 中有因子,Store this combination in the result res 中
class Solution {
public:
vector<vector<int>> getFactors(int n) {
vector<vector<int>> res;
helper(n, 2, {
}, res);
return res;
}
void helper(int n, int start, vector<int> out, vector<vector<int>>& res) {
if (n == 1) {
if (out.size() > 1) res.push_back(out);
return;
}
for (int i = start; i <= n; ++i) {
if (n % i != 0) continue;
out.push_back(i);
helper(n / i, i, out, res);
out.pop_back();
}
}
};

优化
class Solution {
public:
vector<vector<int>> getFactors(int n) {
vector<vector<int>> res;
helper(n, 2, {
}, res);
return res;
}
void helper(int n, int start, vector<int> out, vector<vector<int>> &res) {
for (int i = start; i <= sqrt(n); ++i) {
if (n % i != 0) continue;
vector<int> new_out = out;
new_out.push_back(i);
helper(n / i, i, new_out, res);
new_out.push_back(n / i);
res.push_back(new_out);
}
}
};
边栏推荐
- 解题-->在线OJ(十八)
- 关于redis的几件小事(五)redis保证高并发以及高可用
- 编程思想_编程有必要给孩子学吗?
- 理论篇1:深度学习之----LetNet模型详解
- [in-depth study of 4 g / 5 g / 6 g project - 50] : URLLC - 16 - the 3 GPP URLLC agreement, specification, technical principle of depth interpretation - 10 - high reliability technology - 1 - low codin
- How to automatically renew the token after it expires?
- AlphaFold 如何实现 AI 在结构生物学中的全部潜力
- OAID是什么
- leetcode: 253. How many meeting rooms are required at least
- leetcode:251. 展开二维向量
猜你喜欢

leetcode: 212. Word Search II

输入输出流总结

Rust 从入门到精通04-变量

Phasecraft连下两城,助力英国量子技术商业化加速!

蓝牙技术|上半年全国新增 130 万台充电桩,蓝牙充电桩将成为市场主流

【Today in History】August 4: First female Turing Award winner; NVIDIA acquires MediaQ; first Cybersecurity Challenge completed

【 HMS core 】 【 Media 】 online video editing service 】 【 material can't show, or network anomalies have been Loading state

leetcode:241. 为运算表达式设计优先级

九州云出席领航者线上论坛,共话5G MEC边缘计算现状、挑战和未来

【Web技术】1401- 图解 Canvas 入门
随机推荐
This week to discuss the user experience: Daedalus Nemo to join Ambire, explore the encryption of the ocean
JCMsuite应用:倾斜平面波传播透过光阑的传输
MySQL【窗口函数】【共用表表达式】
leetcode:212. 单词搜索 II
ICML 2022 | 图神经网络的局部增强
Cisco - Small Network Topology (DNS, DHCP, Web Server, Wireless Router)
Chinese valentine's day, of course, to learn SQL optimization better leave work early to find objects
NPDP|作为产品经理,如何快速提升自身业务素养?
技术分享| 融合调度系统中的电子围栏功能说明
快解析结合千方百剂
Redis 复习计划 - Redis主从数据一致性和哨兵机制
2042. 检查句子中的数字是否递增-力扣双百代码-设置前置数据
CF1527D MEX Tree (mex & tree & inclusive)
How to fall in love with a programmer
leetcode:259. 较小的三数之和
eNSP-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)
G. Mountaineering Squad (violence & dfs)
自监督学习未来是掩码自编码器?KAIST最新《自监督学习掩码自编码器》研究进展
【历史上的今天】8 月 4 日:第一位图灵奖女性得主;NVIDIA 收购 MediaQ;首届网络安全挑战大赛完成
vim 常用操作命令