当前位置:网站首页>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);
}
}
};
边栏推荐
- [Problem solving] QT update component appears "To continue this operation, at least one valid and enabled repository is required"
- leetcode: 253. How many meeting rooms are required at least
- SQL语句的写法:Update、Case、 Select 一起的用法
- 第十六章 源代码文件 REST API 教程(一)
- MySQL【触发器】
- Cisco - Small Network Topology (DNS, DHCP, Web Server, Wireless Router)
- 《中国综合算力指数》《中国算力白皮书》《中国存力白皮书》《中国运力白皮书》在首届算力大会上重磅发出
- [Beiya data recovery] IBM System Storage storage lvm information lost data recovery solution
- 16、学习MySQL 正则表达式
- Win10无法访问移动硬盘怎么解决
猜你喜欢

【HMS core】【Media】【视频编辑服务】 在线素材无法展示,一直Loading状态或是网络异常

快解析结合千方百剂

Centos7 install mysql version rapidly

centos7安装mysql急速版

世间几乎所有已知蛋白质结构,都被DeepMind开源了

CloudCompare&PCL 点云按网格划分(点云分幅)

Find My Technology | Prevent your pet from getting lost, Apple Find My technology can help you

技术分享| 融合调度系统中的电子围栏功能说明

CCF GLCC officially opened | Kyushu Cloud open source experts bring generous bonuses to help universities promote open source

NPDP|作为产品经理,如何快速提升自身业务素养?
随机推荐
Why does the decimal point appear when I press the space bar in word 2003?
JCMsuite应用:倾斜平面波传播透过光阑的传输
js深拷贝和浅拷贝具体使用区别_es6深拷贝和浅拷贝
Oracle database user creation, restart, import and export
Almost all known protein structures in the world are open sourced by DeepMind
【 HMS core 】 【 Media 】 online video editing service 】 【 material can't show, or network anomalies have been Loading state
如何和程序员谈恋爱
技术分享| 小程序实现音视频通话
Makefile 语法及使用笔记
【问题解决】QT更新组件出现 “要继续此操作,至少需要一个有效且已启用的储存库”
[LeetCode] 38. Appearance sequence
License server system does not support this version of this feature
Crawler - basic use of selenium, no interface browser, other uses of selenium, cookies of selenium, crawler cases
郑轻新生校赛和中工选拔赛题解
在腾讯,我的试用期总结!
字符串类的设计与实现_C语言字符串编程题
华为手机切换屏幕效果_华为p40页面切换效果怎么换
1401 - Web technology 】 【 introduction to graphical Canvas
C# winforms 输入颜色转换颜色名
杭电校赛(逆袭指数)