Skip to content

Instantly share code, notes, and snippets.

@pdu
pdu / leetcode_add_binary.cpp
Created December 28, 2012 20:50
Given two binary strings, return their sum (also a binary string). http://www.leetcode.com/onlinejudge
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class Solution {
public:
string addBinary(string a, string b) {
stringstream ss(stringstream::in | stringstream::out);
int addon = 0;
@pdu
pdu / leetcode_add_two_numbers.cpp
Created December 28, 2012 21:18
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. http://www.leetcode.com/onlinejudge
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
@pdu
pdu / leetcode_anagrams.cpp
Created December 29, 2012 04:14
Given an array of strings, return all groups of strings that are anagrams. http://www.leetcode.com/onlinejudge
#include <algorithm>
#include <vector>
#include <string>
#include <tr1/unordered_map>
using namespace std;
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
vector<string> ret;
@pdu
pdu / leetcode_balanced_binary_tree.cpp
Created December 29, 2012 04:41
Given a binary tree, determine if it is AVL balanced tree. http://www.leetcode.com/onlinejudge
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
@pdu
pdu / leetcode_best_time_to_buy_and_sell_stock.cpp
Created December 29, 2012 07:36
Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. http://www.leetcode.com/onlinejudge
#include <algorithm>
using namespace std;
class Solution {
public:
int maxProfit(vector<int> &prices) {
int mini = 0x7fffffff;
int ret = 0;
for (int i = 0; i < prices.size(); ++i) {
ret = max(ret, prices[i] - mini);
@pdu
pdu / leetcode_best_time_to_buy_and_sell_stock2.cpp
Created December 29, 2012 07:48
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock …
class Solution {
public:
int maxProfit(vector<int> &prices) {
int ret = 0;
int buy = 0x7fffffff;
int sell = -1;
for (int i = 0; i < prices.size(); ++i) {
if (sell == -1) {
if (prices[i] <= buy) {
buy = prices[i];
@pdu
pdu / leetcode_best_time_to_buy_and_sell_stock3.cpp
Last active December 10, 2015 08:08
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). http://www.leetcode.com/onlinejudge
#include <algorithm>
using namespace std;
class Solution {
public:
int maxProfit_one(vector<int>::iterator left, vector<int>::iterator right) {
int ret = 0;
int mini = 0x7fffffff;
vector<int>::iterator it;
for (it = left; it != right; ++it) {
@pdu
pdu / inorder_traversal_iterative.cpp
Last active December 10, 2015 08:08
Given a binary tree, return the inorder traversal of its nodes' values. http://www.leetcode.com/onlinejudge
#include <stack>
using namespace std;
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
@pdu
pdu / leetcode_binary_tree_level_order_traversal.cpp
Created December 29, 2012 13:31
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). http://www.leetcode.com/onlinejudge
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
@pdu
pdu / leetcode_binary_tree_level_order_traversal2.cpp
Created December 29, 2012 13:43
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). http://www.leetcode.com/onlinejudge
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {