Skip to content

Instantly share code, notes, and snippets.

View raymondino's full-sized avatar
keep up all the good work!

Rui Yan raymondino

keep up all the good work!
View GitHub Profile
@raymondino
raymondino / cc9.8
Created August 9, 2017 17:10
use 25, 10, 5, 1 to form 100, how many ways?
import java.util.*;
public class Solution {
public static void main(String[] args) {
Counter c = new Counter();
int[] coins = {1, 5, 10, 25};
money(coins, 0, 100, c);
System.out.println(c.c);
}
@raymondino
raymondino / cc9.6
Created August 8, 2017 23:52
print all legal parentheses given n pairs of brackets.
import java.util.*;
public class Solution {
public static void main(String[] args) {
ArrayDeque<Character> sb = new ArrayDeque<Character>();
printBrackets(10, 0, 0, sb);
}
public static void printBrackets(int n, int l, int r, ArrayDeque<Character> sb) {
if(l + r == 2 * n) {
@raymondino
raymondino / cc9.5
Created August 8, 2017 23:34
print all permutations of a string with distinct characters
import java.util.*;
public class Solution {
public static void main(String[] args) {
String s = "abc";
permutation(0, s);
}
public static void permutation(int index, String s) {
// check null
@raymondino
raymondino / cc9.4
Created August 8, 2017 22:50
print out all the subsets
import java.util.*;
public class Solution {
public static void main(String[] args) {
int[] a = {1, 2, 3};
LinkedList<Integer> sub = new LinkedList<Integer>();
subset(a, 0, sub);
}
@raymondino
raymondino / cc9.2
Created August 8, 2017 21:26
move robot to any points in the plane. what if there are points that cannot be used.
import java.util.*;
public class Solution {
public static void main(String[] args) {
Counter c = new Counter();
Board b = new Board(10, 10);
Point s = b.getPoint(0, 0);
Point d = b.getPoint(2, 2);
// no limited points
@raymondino
raymondino / cc9.1
Created August 8, 2017 20:20
how many possible different ways to finish n steps with hops of 1, 2, and 3.
import java.util.*;
public class Solution {
public static void main(String[] args) {
Counter c = new Counter();
hop(c, 5);
System.out.println(c.c);
}
public static void hop(Counter c, int n) {
@raymondino
raymondino / cc5.7
Last active August 7, 2017 17:38
find the missing number that is in an array with all integers in binary form in O(n)
import java.util.*;
public class Solution {
public static void main(String[] args) {
int[] a = {0, 1, 2, 3, 4, 6, 7, 8};
System.out.println(find(a, 8));
}
public static int find(int[] a, int n) {
for(int i = 0; i <=n; i++) {
@raymondino
raymondino / cc5.5
Created August 7, 2017 15:24
decide how many bit flips to convert one integer to another
import java.util.*;
public class Solution {
public static void main(String[] args) {
System.out.println(flip(0, 1));
}
public static int flip(int a, int b) {
int r = a ^ b;
int c = 0 ;
@raymondino
raymondino / cc5.2
Created August 4, 2017 22:39
print a (0, 1) decimal value in its binary form.
import java.util.*;
public class Solution {
public static void main(String[] args) {
System.out.println(convert(0.8125));
}
// put m in n starting from ith to jth index
public static String convert(double x) {
@raymondino
raymondino / cc5.1
Created August 4, 2017 22:17
insert m into n within [j, i] (j >i + 1) with bit operation
import java.util.*;
public class Solution {
public static void main(String[] args) {
System.out.println(insert(5, 16, 10, 18));
}
// put m in n starting from ith to jth index
public static String insert(int m, int n, int i, int j) {