Skip to content

Instantly share code, notes, and snippets.

@Jimexist
Jimexist / array_shift.c
Created May 4, 2013 12:28
A simple code to shift an array of integers to the left for a number of times
#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) {
@Jimexist
Jimexist / leetcode_climb_stairs.java
Created July 6, 2013 09:09
Solution in Java to solve the Climb Stairs problem in Leetcode.com
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);
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);
}
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 )
@Jimexist
Jimexist / EditDistance.java
Last active December 20, 2015 06:58
Edit distance using DP
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();
@Jimexist
Jimexist / CountAndSay.java
Created August 24, 2013 10:05
Count And Say
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);
@Jimexist
Jimexist / KSubSet.java
Created September 7, 2013 05:21
Generate subset with k elements
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);
@Jimexist
Jimexist / SubWithDup.java
Created September 7, 2013 07:46
Subset with duplication
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;
@Jimexist
Jimexist / Derangement.java
Created September 7, 2013 09:40
Derangement, permutation of integer array where for any i, a[i] != i
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)) {
@Jimexist
Jimexist / Permutations.java
Last active December 22, 2015 12:39
Permutations
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);
}