Skip to content

Instantly share code, notes, and snippets.

View daifu's full-sized avatar

Daifu Richard Ye daifu

View GitHub Profile
@daifu
daifu / longestValidParentheses.java
Created June 17, 2013 15:58
Longest Valid Parentheses - Leetcode
/*
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
For "(()", the longest valid parentheses substring is "()", which has length = 2.
Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.
*/
public class Solution {
public int longestValidParentheses(String s) {
// Start typing your Java solution below
@daifu
daifu / nextPermutation.java
Created June 13, 2013 08:19
Next Permutation - leetcode
/*
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
@daifu
daifu / searchInsert.java
Last active December 18, 2015 05:39
Search Insert Position - Leetcode
/*
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
@daifu
daifu / searchRange.java
Created June 6, 2013 07:24
Search for a Range - Leetcode
/*
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].
@daifu
daifu / findSubstring.java
Last active December 18, 2015 00:19
Substring with Concatenation of All Words
/*
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.
For example, given:
S: "barfoothefoobarman"
L: ["foo", "bar"]
You should return the indices: [0,9].
(order does not matter).
@daifu
daifu / app.js
Created May 31, 2013 21:41 — forked from johnkpaul/app.js
//in your application, rather than using window.location to get the current url
App.getLocation = function(){
return window.location.protocol + '//' + window.location.host
+ '/' + Backbone.history.options.root + Backbone.history.getFragment()
}
@daifu
daifu / removeDuplicates.java
Last active December 17, 2015 20:59
Remove Duplicates from Sorted Array
/*
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
*/
@daifu
daifu / removeElement.java
Created May 29, 2013 17:08
Remove Element
/*
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
*/
public class Solution {
public int removeElement(int[] A, int elem) {
// Start typing your Java solution below
// DO NOT write main() function
int cur = 0;
@daifu
daifu / reverseKGroup.java
Created May 26, 2013 06:15
Reverse Nodes in k-Group
/*
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
@daifu
daifu / mergeKLists.java
Created May 23, 2013 17:51
Merge k Sorted Lists
/*
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Algorithm:
1. Needs a dummy node that helps to insert a node at the beginning of the list.
2. Use devise and conquer algorithm
a. Keep one list to be return.
b. Continue to merge other lists to the list to be return.
*/