博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode-Largest Divisble Subset
阅读量:4680 次
发布时间:2019-06-09

本文共 1112 字,大约阅读时间需要 3 分钟。

Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0.

If there are multiple solutions, return any subset is fine.

Example 1:

nums: [1,2,3]Result: [1,2] (of course, [1,3] will also be ok)

Example 2:

nums: [1,2,4,8]Result: [1,2,4,8]

Credits:

Special thanks to for adding this problem and creating all test cases.

Analysis:

Sort the array, then for nums[i], if nums[i] % nums[j] == 0, then nums[i] is in the largest divisible set of nums[j]. We just need to find out the largest subset of nums[i].

Solution:

1 public class Solution { 2     public List
largestDivisibleSubset(int[] nums) { 3 List
resList = new LinkedList
(); 4 if (nums.length == 0) return resList; 5 6 Arrays.sort(nums); 7 int[] size = new int[nums.length]; 8 int[] pre = new int[nums.length]; 9 10 int maxSize = 0;11 int maxInd = -1;12 13 for (int i=0;i

 

转载于:https://www.cnblogs.com/lishiblog/p/5700578.html

你可能感兴趣的文章
centos下/etc/sysconfig/下找不到iptables文件
查看>>
linux安装jdk
查看>>
JAVA Http Basic auth
查看>>
Python3.6全栈开发实例[002]
查看>>
转载: 8天学通MongoDB——第四天 索引操作
查看>>
HDU_4939 stupid tower defense 2014多校7 多变量型DP
查看>>
一段简单的CSS Tab标签切换代码
查看>>
9.29第三次作业
查看>>
JavaScript + ASP.NET
查看>>
mysql 性能优化方案 (转)
查看>>
min-max 容斥
查看>>
2019-06-06 Java学习日记 day27 反射
查看>>
《大道至简》3
查看>>
黑马程序员——关于接口和抽象类
查看>>
【译】为迁移到Windows Azure制定规划
查看>>
面试题之求二叉树的深度
查看>>
struts2---访问WEB
查看>>
jQuery学习(三)
查看>>
[VJ]输出m/n,若是循环体只输出第一节
查看>>
【问题】background:url(imagePath)不能显示图片
查看>>