CC150 8.3

8.3 Write a method that returns all subsets of a set.

powerSet(i) = 
  [powerSet(i - 1)] * ITEMi + // Add new item into each existing set
  [pwerSet(i - 1)] + // Existing set
  ITEMi // single new item.
powerSet(1) = ITEM1.

<T> Set<Set<T>> powerSet(Set<T> set)
{
  if (set == null || set.isEmpty())
    return Collections.emptyList();
    
  Set<Set<T>> toReturn = Sets.new();
  
  T t = set.removeFirstElement();
  Set<Set<T>> smallResults = powerSet(set);
  toReturn.addAll(smallResults);
  
  for (Set<T> s : smallResults)
  {
    Set<T> withI = Sets.copy(s).add(t);
    toReturn.add(withI);
  }
  
  toReturn.add(Sets.of(t));
  
  // Add empty one in outer method.
  // if needed.
  toReturn.add(Sets.empty());
    
  return toReturn;
}
时间: 2024-10-04 09:25:29

CC150 8.3的相关文章

CC150 需整理汇总

汉诺塔问题:P141 用两个stack设计一个队列 p142 结合上题,队列实现max操作,要求尽量提高效率.(编程之美) 找出二叉树中指定节点的下一个节点(中序后继),假定每个节点有父指针.p154 二叉树某两个节点的公共祖先.p155 判断T2是否是T1的子树.p159 打印二叉树节点数值总和等于某个给定节点的所有路径. p161 打印0-1之间double数字的二进制表示 p164 编写一个函数,确定需要改变几个位,才能将整数A转成整数B.p171 9.3 寻找magic index.p2

(斐波那契总结)Write a method to generate the nth Fibonacci number (CC150 8.1)

根据CC150的解决方式和Introduction to Java programming总结: 使用了两种方式,递归和迭代 CC150提供的代码比较简洁,不过某些细节需要分析. 现在直接运行代码,输入n(其中用number代替,以免和方法中的n混淆)的值,可以得出斐波那契数. 代码如下: /* CC150 8.1 Write a method to generate the nth Fibonacci number Author : Mengyang Rao note : Use two me

CC150 - 11.3

Question: Given a sorted array of n integers that has been rotated an unknown number of times, write code to find an element in the array. You may assume that the array was originally sorted in increasing order. 1 package POJ; 2 3 import java.util.Ar

CC150 - 11.2

Question: Write a method to sort an array of strings so that all the anagrams are next to each other. 1 package POJ; 2 3 import java.util.Arrays; 4 import java.util.Comparator; 5 import java.util.Hashtable; 6 import java.util.LinkedList; 7 import jav

[cc150] Find all valid combinations of n-pairs of parentheses

Implement an algorithm to print all valid ( properly opened and closed) combinations of n-pairs of parentheses. 思路:比如 n = 3, ((())) 就是一个valid combination. 这题和前一个 all permutations 有点类似,更 tricky 的地方在于存在大量的重复. 最好的办法是 使用一个 char 缓冲区,长度为2*n. 从左到右往这个缓冲区里填字符

[cc150] 硬币问题

Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies (1 cent), find how many ways to represent n cents. 思路: 从最大面值的硬币开始分析,然后依次看更小面值的硬币.假设 n = 100, 所有 valid 排列组合中 num_quarters = 0 的是一类, num_quarters = 1 的是一类,

[CC150] 八皇后问题

Write an algorithm to print all ways of arranging eight queens on an 8*8 chess board so that none of them share the same row, column or diagonal. 思路: 本质上是DFS, 从第一行开始一行行地放棋子,每次放棋子之前根据当前的棋盘检查一下约束. Code (from book): void placeQueen(int row, Integer[] co

CC150 1.1 字符串查唯一bit manipulation详解

public boolean isUniqueChars2(String str) { int checker = 0; for (int i = 0; i < str.length(); i++) { int val = str.charAt(i) - 'a'; //保证val的值不超过26 if ((checker & (1 << val)) > 0) { return false; } else { checker |= (1 << val);//chec

[cc150] check palindrome of a singly linked list

Problem: Implement a function to check if a singly linked list is a palindrome. 思路: 最简单的方法是 Reverse and compare. 另外一种非常经典的办法是用 Recursive 的思路,把一个list看成这种形式: 0 ( 1 ( 2 ( 3 ) 2 ) 1 ) 0 0 ( 1 ( 2 ( 3 3 ) 2 ) 1 ) 0 CC150里面给出的Code,非常简洁,贴在下面: length == 1 对应

4.6---找二叉树中序后继(CC150)

因为,没有重复值,所以只需要做一个标记就OK了. public class Successor { static boolean flag = false; static int result = 0; public int findSucc(TreeNode root, int p) { // write code here inOrder(root,p); return result; } public static void inOrder(TreeNode root,int p){ if