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. flush方法和close方法的区别

    package com.yhqtv.demo05.Writer; import java.io.FileWriter; /* * @author XMKJ yhqtv.com Email:yhqtv@ ...

  2. python入门及数字、字符串类型

    目录 python开发框架 开发 1. 开发语言 2. 语言比对 3. python安装 4. Python开发IDE:pycharm ,eclipse python入门 1. 第一句Python 2 ...

  3. (数据科学学习手札83)基于geopandas的空间数据分析——geoplot篇(下)

    本文示例代码.数据及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 在上一篇文章中我们详细学习了geop ...

  4. angular 实现依赖注入

    1:首先获取module对象var myAppModule = angular.module('myApp', []); 2:定义对象(类似spring中xml声明bean对象<bean id= ...

  5. python selenium unittest Fixture(setUp/tearDown)笔记

    Fixture用途: 1.做测试前后的初始化设置,如测试数据准备,链接数据库,打开浏览器等这些操作都可以使用fixture来实现 2.测试用例的前置条件可以使用fixture实现 Fixture使用: ...

  6. Python 常用编码规范

    一.简明概述 1.编码 如无特殊情况, 文件一律使用 UTF-8 编码 如无特殊情况, 文件头部必须加入#-*-coding:utf-8-*-标识 2.代码格式 2.1.缩进 统一使用 4 个空格进行 ...

  7. Kappa(cappa)系数只需要看这一篇就够了,算法到python实现

    1 定义 百度百科的定义: 它是通过把所有地表真实分类中的像元总数(N)乘以混淆矩阵对角线(Xkk)的和,再减去某一类地表真实像元总数与被误分成该类像元总数之积对所有类别求和的结果,再除以总像元数的平 ...

  8. oracle分析函数Rank, Dense_rank, row_number

    http://www.cnblogs.com/wuyisky/archive/2010/02/24/oracle_rank.html 目录=============================== ...

  9. 如何安放你的大文件,MongoDB GridFS可以帮助你

    1 简介 众所周知(你不知也当你知),MongoDB是以文档(Document)组织数据的.除了常用于存储Json数据,它也是可以存储普通文件的.我们可以把一些文件以BSOON的格式存入MongoDB ...

  10. 王艳 201771010127《面向对象程序设计(java)》第十六周学习总结

    一:理论部分 1.程序:是一段静态的代码,它是应用程序执行的蓝本. 2.进程:是程序的一次动态执行,它对应了从代码加载.执行至执行完毕的一个完整过程. 3.多线程:是进程执行过程中产生的多条执行线索. ...