This file contains 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 <stdio.h> | |
void swap(int *a, int *b) { | |
int tmp = *a; | |
*a = *b; | |
*b = tmp; | |
} | |
/* Precondition: n > 0 */ | |
void reverse(int arr[], int n) { |
This file contains 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 Map<Integer, int[]> pows = new HashMap<Integer, int[]>(); | |
{ | |
pows.put(1, new int[]{1, 1, 1, 0}); | |
} | |
private int[] pow(int n) { | |
if (pows.containsKey(n)) return pows.get(n); |
This file contains 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 String multiply(String num1, String num2) { | |
return toString(mult(toIntArray(num1), toIntArray(num2))); | |
} | |
private int[] toIntArray(String s) { | |
int[] result = new int[s.length()]; | |
for (int i=0; i<result.length; ++i) { | |
result[i] = Character.digit(s.charAt(s.length()-1-i), 10); | |
} |
This file contains 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 Test.QuickCheck | |
takeR :: Int -> [a] -> [a] | |
takeR n _ | n <= 0 = [] | |
takeR n l | otherwise = go (drop n l) l | |
where go [] y = y | |
go (_:xs) (_:ys) = go xs ys | |
prop_length s n = (length s >= n && n >= 0) ==> length (takeR n s) == n | |
where types = (s :: [Char], n :: Int ) |
This file contains 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(); |
This file contains 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 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 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 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 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); | |
} |
OlderNewer