class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
vector<int> dp(nums.size(),0); //dp[i] 代表 nums[i]在nums里面能够整除的数字个数 1,2,3, 里面 dp[2]代表index=2的数 3 能够整数的数有1个;
vector<int> idx(nums.size(),0); //记录每个索引
if(nums.empty())return vector<int>();
sort(nums.begin(),nums.end()); //小到大排序
int imax=0,index=-1;
dp[0]=1;
for(int i=0;i<nums.size();++i)
{
dp[i]=1; //重要, 每个数i能被自己整除, 所以初始化为1
idx[i]=-1; //重要, 每个i位置上的索引初始化为-1代表没有被选中, 不赋值导致下面死循环
for(int j=i-1;j>=0;--j)
{
if(nums[i]%nums[j]==0) // 判断 i/j 是否整除
{
if(1+dp[j]>dp[i]) //循环1到j 找出 dp [1->j]里面最大的数
{
dp[i]=dp[j]+1; //既然j能整除的数字有dp[j]个, 而i又能整除j, 毫无疑问i也能整除j能整除的所有数字, dp[i]=dp[j]+1
idx[i]=j; //更新索引为j, 可能有很多个符合条件的j, 这里只能记录第一个
}
}
}
if(dp[i]>imax) //imax记录目前为止dpi的最大值
{
imax=dp[i];
index=i;
}
}
vector<int> r;
while(index!=-1) //循环完后index是最后一个要被加入结果集的索引
{
r.push_back(nums[index]);
index=idx[index]; //相当于从后面往前面遍历
}
return r;
}
};

  

368. Largest Divisible Subset的更多相关文章

  1. Leetcode 368. Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  2. 368. Largest Divisible Subset -- 找出一个数组使得数组内的数能够两两整除

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  3. 【leetcode】368. Largest Divisible Subset

    题目描述: Given a set of distinct positive integers, find the largest subset such that every pair (Si, S ...

  4. 【LeetCode】368. Largest Divisible Subset 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/largest-d ...

  5. 368 Largest Divisible Subset 最大整除子集

    给出一个由无重复的正整数组成的集合, 找出其中最大的整除子集, 子集中任意一对 (Si, Sj) 都要满足: Si % Sj = 0 或 Sj % Si = 0.如果有多个目标子集,返回其中任何一个均 ...

  6. LeetCode "Largest Divisible Subset" !

    Very nice DP problem. The key fact of a mutual-divisible subset: if a new number n, is divisible wit ...

  7. [LeetCode] Largest Divisible Subset 最大可整除的子集合

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  8. Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  9. [Swift]LeetCode368. 最大整除子集 | Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

随机推荐

  1. windows 10系统在右键中添加管理员打开cmd

    需要修改注册表内容,新建文件,后缀名改为reg,文件中粘贴下边的代码 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory ...

  2. awk使用学习

    awk使用中常用的几个方法: 一.在某一列中查询符合条件的值,并返回该行数据 [root@localhost bus_route]# cat 123.log one two three four1 2 ...

  3. IoT experitment

    Abstract: In order to solve the problems of complex experiment management, complicated teaching task ...

  4. vue教学视频(小程序教学视频)

    写在前面 最近通过了解众多面试者的面试情况 总结出以下几点: 对框架的要求越来越高特别是vue和小程序 不会vue和小程序连面试机会都没有 会vue的比不会vue的薪资高4-5k 小程序有发展的趋势 ...

  5. C++之 自定义数据类型 枚举、结构体、共用体

    1.枚举 枚举类型的声明形式为:   enum  枚举类型名   {  变量值列表 };    变量值列表里都是整型变量,另外不要忘记最后面的分号! 比如,enum  weekday   { sun, ...

  6. LINUX 学习笔记 账号与群组的管理

    LINUX 账号与群组的管理 UID:UserID 保存文件:/etc/passwd GID:GroupID 保存文件:/etc/group /etc/passwd 文件结构 一行代表一个账号,里面还 ...

  7. [Kafka] [All about it]

    Overview 设计目标: 以O(1) 常数级时间复杂度的访问性能,提供消息持久化能力. 高吞吐率. 支持 kafka server 间的消息分区,及分布式消费,同时保证每个partition内部的 ...

  8. 使用iframe框架后的页面跳转时目标页面变为iframe的子页面的问题

    <frameset rows="4,200,10,*,120" cols="*" framespacing="0" framebord ...

  9. 胜利大逃亡,bfs,广度优先搜索

    题目描述: Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会.魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius ...

  10. WEB 设计规范

    WEB端设计规范 一.网页尺寸       一般网站宽为996px:国内网站大部分还是以1000个像素为界限,因超过1000像素适合在大屏幕上浏览,小屏幕会显得拥挤.国内尺寸设置比较保守,这样可以保证 ...