368. 最大整除子集

给出一个由无重复的正整数组成的集合,找出其中最大的整除子集,子集中任意一对 (Si,Sj) 都要满足:Si % Sj = 0 或 Sj % Si = 0。

如果有多个目标子集,返回其中任何一个均可。

示例 1:

输入: [1,2,3]

输出: [1,2] (当然, [1,3] 也正确)

示例 2:

输入: [1,2,4,8]

输出: [1,2,4,8]

class Solution {
public int max(int a,int b){
return a>b?a:b;
}
public List<Integer> largestDivisibleSubset(int[] nums) {
int n=nums.length;
if(n==0)
return new ArrayList();
int[] last=new int[n];
for(int i=0;i<n;i++)
last[i]=-1;
int[] dp=new int[n];
int ans=0;
Arrays.sort(nums);
for(int i=0;i<n;i++)
for(int j=0;j<i;j++){
if(nums[i]%nums[j]==0&&dp[j]+1>dp[i]){
dp[i]=dp[j]+1;
if(dp[ans]<dp[i])
ans=i;
last[i]=j;
}
}
List<Integer> ansList=new ArrayList();
while(ans!=-1){
ansList.add(nums[ans]);
ans=last[ans];
}
return ansList;
}
}

Java实现 LeetCode 368 最大整除子集的更多相关文章

  1. Leetcode 368.最大整除子集

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

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

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

  3. 【JavaScript】Leetcode每日一题-最大整除子集

    [JavaScript]Leetcode每日一题-最大整除子集 [题目描述] 给你一个由 无重复 正整数组成的集合 nums ,请你找出并返回其中最大的整除子集 answer ,子集中每一元素对(an ...

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

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

  5. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  9. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. 【SPOJ – SUBST1】New Distinct Substrings 后缀数组

    New Distinct Substrings 题意 给出T个字符串,问每个字符串有多少个不同的子串. 思路 字符串所有子串,可以看做由所有后缀的前缀组成. 按照后缀排序,遍历后缀,每次新增的前缀就是 ...

  2. [POJ1038]状压DP

    题意:给一个n*m的区域,里面有一些障碍物,往里面放2*3和3*2的矩形,矩形之间不能重叠,不能覆盖到障碍物,求能放置的最大个数.(n<=150,m<=10) 思路:看到m=10就应该往状 ...

  3. [hdu5255]枚举

    思路:这题与csu1392题目类似,方法类似.枚举最高位,最低位和中间数字的长度,然后列等式,计算中间的数字,看长度是不是跟枚举的一致,需要注意的是中间数字可以有前导0,如果根据等式算出来的中间数字为 ...

  4. 简述SpringCloud框架

    1.什么是SpringCloud? SpringCloud是一系列框架的有序集合,它利用SpringBoot的开发便利性简化了分布式系统的开发,比如服务发现.服务网关.服务路由.链路追踪等.Sprin ...

  5. Centos最小安装

    网络配置 虚拟机安装使用的NAT模式 #enoXXX 为自己的网卡名称, 使用ip addr 可以查看 将/etc/sysconfig/network-scripts/ifcfg-enoXXX中的ON ...

  6. neo4j 图数据库安装及介绍

    neo4j 图数据库安装及介绍 一.neo4j图数据库介绍 图数据库,顾名思义就是利用了"图的数据结构来作为数据存储逻辑体现的一种数据库",所以要想学好图数据库当然需要了解一些关于 ...

  7. python第二课list基本命令

    列表: stus= 'ada,dsfas.,saf,sdfas,saf'  #字符串new_stus = ['段','加','linux','123','数组']   #列表 取值方便#列表,数组,l ...

  8. React:Composition

    在日常的UI构建中,经常会遇到一种情况:组件本身更多是作为一个容器,它所包含的内容可能是动态的.未预先定义的.这时候它的内容取决另一个组件或外部的输入.比如弹层. props.children: Re ...

  9. SSH三大框架知识点

    Hibernate ****************************************************************************************** ...

  10. MySQL zip解压 安装过程和配置

    MYSQL官网下载地址:https://dev.mysql.com/downloads/mysql/   1.下载mysql-5.7.19-winx64.zip,解压到指定的文件夹, 例如:E:\so ...