Skip to content

Instantly share code, notes, and snippets.

class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(num.begin(), num.end());
int result=num[0]+num[1]+num.back();
for(int i=0; i<num.size(); i++)
{
int j=i+1, k=num.size()-1;
@guolinaileen
guolinaileen / Letter Combinations of a Phone Number.java
Last active December 11, 2015 19:48
recursively solve this problem; uninitialized char equals to 0
public class Solution {
public static ArrayList<String> letterCombinations(String digits) {
if(digits==null) return null;
ArrayList<String> result=new ArrayList<String>();
if(digits.length()==0)
{
result.add("");
return result;
}
char [][] maps=new char[10][4];
@guolinaileen
guolinaileen / Remove Nth Node From End of List.java
Last active December 11, 2015 19:48
Try to do this in one pass.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
@guolinaileen
guolinaileen / gist:4651024
Created January 27, 2013 22:34
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
public class Solution {
public boolean isValid(String s) {
int length=s.length();
char [] array=s.toCharArray();
if(length==0) return true;
Stack<Character> stack=new Stack<Character>();
for(int i=0; i<length; i++)
{
if(array[i]=='(' || array[i]=='{' || array[i]=='[' )
{
@guolinaileen
guolinaileen / Generate Parentheses.java
Last active December 11, 2015 20:09
Generate ParenthesesFeb 13 '12 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()"
public class Solution {
public ArrayList<String> generateParenthesis(int n) {
ArrayList<String> result=new ArrayList<String>();
char [] array=new char[2*n];
generate(result, n, n, array, 0);
return result;
}
void generate(ArrayList<String> result, int left, int right, char [] array, int num)
{
if(left==0 && right==0 )
@guolinaileen
guolinaileen / Merge k Sorted Lists.java
Created January 28, 2013 08:18
This method should be changed. PriorityQueue can be used to solve this problem.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
@guolinaileen
guolinaileen / Swap Nodes in Pairs.java
Last active December 26, 2019 08:24
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
public class Solution {
public int maxProfit(int[] prices) {
// Start typing your Java solution below
// DO NOT write main() function
if(prices.length==0) return 0;
int profit=0;
for(int i=1; i<prices.length; i++)
{
if(prices[i]>prices[i-1])
{
public class Solution {
public int maxProfit(int[] prices) {
// Start typing your Java solution below
// DO NOT write main() function
if(prices.length==0) return 0;
int min=prices[0], max=prices[0];
int profit=0;
for(int i=1; i<prices.length; i++)
{
if(prices[i]>max)