Skip to content

Instantly share code, notes, and snippets.

@jingrui
jingrui / combinationSum.java
Last active December 15, 2015 08:49
combinationSum. leetcode.online judge. tough debugging question.
public class Solution {
public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) {
ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();
Arrays.sort(candidates);
ArrayList<Integer> al = new ArrayList<Integer>();
helper(candidates,target,al,ret,0);
return ret;
}
public void helper(int[] can, int t, ArrayList<Integer> al, ArrayList<ArrayList<Integer>> ret, int i ){
if (t == 0){
@jingrui
jingrui / ValidSudoku.java
Created March 24, 2013 22:43
check if valid sudoku. leetcode onlinejudge.
public class Solution {
public boolean isValidSudoku(char[][] board) {
// Start typing your Java solution below
// DO NOT write main() function
for(int i = 0; i < board.length; i++){
HashSet hm = new HashSet();
for(int j = 0; j < board.length; j++){
if (hm.contains(board[i][j]) && (board[i][j] != '.'))
return false;
hm.add(board[i][j]);
@jingrui
jingrui / SearchInsertPosition.java
Created March 24, 2013 22:26
Search Insert Position. leetcode.
public class Solution {
public int searchInsert(int[] A, int target) {
// Start typing your Java solution below
// DO NOT write main() function
return helper(A,target,0,A.length-1);
}
public int helper(int[]a , int t, int s, int e){
if (e < s)
return s;
int m = (s+e)/2;
@jingrui
jingrui / SearchforRange.java
Created March 24, 2013 22:15
Given a sorted array of integers, find the starting and ending position of a given target value. If the target is not found in the array, return [-1, -1]. http://leetcode.com/onlinejudge
public class Solution {
public int[] searchRange(int[] A, int target) {
int[] ret = new int[2];
ret[0] = helper(A,target-0.1,0,A.length-1);
ret[1] = helper(A,target+0.1,0,A.length-1);
return ret;
}
public int helper(int[] a, double t,int s, int e){
if (e < s){
if (s >= a.length)
@jingrui
jingrui / SearchRotatedSortedArray.java
Last active December 15, 2015 08:49
Search in Rotated Sorted Array. http://leetcode.com/onlinejudge
public class Solution {
public int search(int[] A, int target) {
// Start typing your Java solution below
// DO NOT write main() function
int s = 0;
int e = A.length -1;
return sear(A,target,s,e);
}
public int sear(int[] A, int tar, int s, int e){
if (e < s)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class Solution {
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class Solution {
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class Solution {
1. cmu machine learning online class
http://alex.smola.org/teaching/cmu2013-10-701/index.html
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class Solution {