首页 > 其他 > 详细

Leetcode 90 Subsets II

时间:2015-07-12 11:07:51      阅读:224      评论:0      收藏:0      [点我收藏+]

Given a collection of integers that might contain duplicates, nums, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

 

For example,
If nums = [1,2,2], a solution is:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

Leetcode 78 Subsets

区别在于这边需要临时搭一个数组的复制,单次只能去掉一个元素而不是使用 ans - [x] 去除ans内所有为x的元素。

def subsets_with_dup(nums)
    sub([nums.sort],0)
end

def sub(ans,start=0)
    t = ans.length
    (start...t).each do |i|
        ans[i].each do |x|
            temp = ans[i].dup
            temp.length.times do |j|
                if temp[j] == x
                    temp.delete_at(j) 
                    break
                end
            end
            ans << temp unless ans.include?(temp)
        end
    end
    return ans if ans[-1].empty?
    sub(ans,t)
end

 

Leetcode 90 Subsets II

原文:http://www.cnblogs.com/lilixu/p/4640598.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!