This file contains hidden or 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
| # 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 } |
This file contains hidden or 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 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. |
This file contains hidden or 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 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]) { |
This file contains hidden or 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 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: |
This file contains hidden or 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 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: |
This file contains hidden or 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 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) { |
This file contains hidden or 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
| /** | |
| * 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) { |
This file contains hidden or 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 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>(); |
This file contains hidden or 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 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) |
This file contains hidden or 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
| // 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; |
OlderNewer