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 / qbspider.py
Created March 21, 2017 01:51
Python Web Spider Snippet
# a practice code inspired by Cui Qingcai: http://cuiqingcai.com/990.html
import urllib
import urllib2
import re
page = 1
url = 'http://www.qiushibaike.com/hot/page/' + str(page) + '/?s=4966590'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = { 'User-Agent' : user_agent }
@raymondino
raymondino / rainbowSort.java
Last active March 21, 2017 16:49
sort: dutchFlag (or rainbow)
public class Solution {
public int[] rainbowSort(int[] array) {
if (array != null && array.length != 0 && array.length != 1) {
int i = 0; // i always points to the first 0 --> [0, i-1] is -1 area
int j = 0; // j always points to the first unexplored element --> [i, j-1] is 0 area
int k = array.length - 1; // k always points to last unexplored element -->
// [j, k] is the unexplored area; [k+1, array.length-1] is 1 area
// index j is really the one that moves from left to right of the array.
// index i and k are updated passively, according to j.
@raymondino
raymondino / selectionSort.java
Created March 22, 2017 14:46
Sort: selection sort
public class Solution {
public int[] solve(int[] array) {
// corner case check:
if(array != null && array.length != 0) {
//selection sort
// i pointer to the index of the smallest element
// j pointer of all the elements (i, end]
for(int i = 0; i < array.length - 1; ++i) {
for(int j = i; j < array.length; ++j) {
if(array[i] > array[j]) {
@raymondino
raymondino / quickSort.java
Created March 22, 2017 14:47
sort: quick sort
public class Solution {
public int[] quickSort(int[] array) {
if(array != null && array.length != 0 && array.length != 1) {
qs(array, 0, array.length - 1);
}
return array;
}
public void qs(int[] array, int left, int right) {
// corner case:
@raymondino
raymondino / mergeSort.java
Created March 22, 2017 14:47
sort: merge sort
public class Solution {
public int[] mergeSort(int[] array) {
if(array == null || array.length == 0 || array.length == 1) {
return array;
}
return split(array, 0, array.length - 1);
}
public int[] split(int[] array, int left, int right) {
// base case:
@raymondino
raymondino / findAllSubset.java
Last active April 25, 2017 14:29
using depth first search to get all the subset of a set
import java.util.*;
public class Solution {
public static void main(String[] args) {
String input = "abc";
Deque<String> solution = new ArrayDeque<String>(); // solution is as a stack that keeps track of the searching branch
findAllSubset(input, 0, solution);
}
public static void findAllSubset(String input, int index, Deque<String> solution) {
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
public class Solution {
public int lengthOfLongestSubstring(String s) {
// null check
if(s == null || s == "")
return 0;
int i = 0;
int j = 0;
int max = 0;
int m = 0;
HashMap<Character, Integer> pos = new HashMap<Character, Integer>();
@raymondino
raymondino / leetcode4.java
Created July 25, 2017 19:53
find the median/kth smallest element from two sorted arrays
public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int l = nums1.length + nums2.length;
if(l % 2 == 1) // if l is odd, find the (l/2)th smallest number
return findKthSmallestElement(nums1, 0, nums2, 0, l/2 + 1);
else // if l is even, find the (l/2)th and (l/2 + 1)th smallest number, then take the average
// note that (l/2)th is not the index of the array, but the actual element counts.
return
( findKthSmallestElement(nums1, 0, nums2, 0, l/2) +
findKthSmallestElement(nums1, 0, nums2, 0, l/2 + 1)
// leetcode 561 --> reviewing sorting algorithms
public class Solution {
public int arrayPairSum(int[] nums) {
// null check
if(nums == null || nums.length <= 1) {
return nums[0];
}
quickSort(nums, 0, nums.length - 1);
// mergeSort(nums, 0, nums.length - 1);
int sum = 0;