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
/* | |
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 |
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
/* | |
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 |
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
/* | |
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 |
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
/* | |
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]. |
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
/* | |
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). |
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
//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() | |
} |
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
/* | |
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]. | |
*/ |
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
/* | |
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; |
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
/* | |
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, |
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
/* | |
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. | |
*/ |