This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <assert.h> | |
| #include <stdlib.h> | |
| #define MAX(A, B) ((A)>(B)?(A):(B)) | |
| static void print_paren(int n); | |
| static void do_print(int n, int* idx, int k); | |
| static void print_result(int n, int* idx); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class BinarySearch { | |
| private BinarySearch() { | |
| } | |
| public static void main(String[] args) { | |
| Integer[] data = {1,2,3,4,5,6,7,8,9}; | |
| for (int i=0; i<=10; ++i) { | |
| System.out.println(String.format("%d: %d", i, binarySearch(data, i))); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import sys | |
| def toGrey(num): | |
| return (num >> 1) ^ num | |
| def generate(n): | |
| for i in xrange(2**n): | |
| yield toGrey(i) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class MultisetPermutations { | |
| private MultisetPermutations() { | |
| } | |
| public static void main(String[] args) { | |
| for (List<Integer> li : permutate(Arrays.asList(1, 1, 2, 2))) { | |
| System.out.println(li); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class Permutations { | |
| private Permutations() { | |
| } | |
| public static void main(String[] args) { | |
| for (List<Integer> li : permutate(Arrays.asList(1, 2, 4, 9))) { | |
| System.out.println(li); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class Derangement { | |
| public static void main(String[] args) { | |
| List<Integer> list = new ArrayList<>(); | |
| for (int i=0; i<4; ++i) { | |
| list.add(i); | |
| } | |
| for (List<Integer> li : derangement(list)) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) { | |
| Arrays.sort(num); | |
| final ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); | |
| if (num.length == 0) { | |
| result.add(new ArrayList<Integer>()); | |
| return result; | |
| } | |
| btrack(num, new boolean[num.length], 0, result); | |
| return result; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public ArrayList<ArrayList<Integer>> combine(int n, int k) { | |
| final ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); | |
| if (k == 0) { | |
| result.add(new ArrayList<Integer>()); | |
| return result; | |
| } | |
| for (int i=n; i>=1; --i) { | |
| for (ArrayList<Integer> ai : combine(i-1, k-1)) { | |
| ai.add(i); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class Solution { | |
| public String countAndSay(int n) { | |
| List<Integer> i = new ArrayList<Integer>(); | |
| i.add(1); | |
| while (--n > 0) { | |
| i = say(i); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| private static final int MATCHED = 1, DELETED = 1, INSERTED = 1; | |
| public int minDistance(String word1, String word2) { | |
| if (word1.length() == 0) return word2.length() * INSERTED; | |
| if (word2.length() == 0) return word1.length() * DELETED; | |
| if (word1.equals(word2)) return 0; // not much used though | |
| final int rows = word1.length(); |