Skip to content

Instantly share code, notes, and snippets.

class Solution {
public:
vector<int> plusOne(vector<int> &cdigits) {
if (cdigits.empty()) return vector<int>();
auto digits = cdigits;
auto current = digits.rbegin();
while (current != digits.rend()) {
auto tmp = *current + 1;
if (tmp == 10) {
*current = 0;
@codinfox
codinfox / Q27.cpp
Created January 15, 2015 16:48
Remove Element
class Solution {
public:
int removeElement(int A[], int n, int elem) {
size_t insert = 0, current = 0;
while (current < n) {
if (A[current] != elem)
A[insert++] = A[current];
current++;
}
return (int)insert;
@codinfox
codinfox / Q26.cpp
Created January 15, 2015 16:36
Remove Duplicates from Sorted Array
class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n < 2) return n;
size_t insert = 1, current = 1;
while (current < n) {
if (A[current] != A[insert - 1]) {
A[insert++] = A[current];
}
current++;
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
stack<TreeNode*> nodeStack;
vector<int> result;
//base case
if(root==NULL)
return result;
nodeStack.push(root);
while(!nodeStack.empty())
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
class Solution {
public:
int atoi(const char *str) {
size_t current = 0;
size_t length = strlen(str); // To avoid memory overflow
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class Solution {
private:
public:
vector<int> twoSum(vector<int> &numbers, int target) {
map<int, int> lookup;
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Solution {
public:
int numDecodings(string s) {
if (s.size() == 0) return 0;
if (s[0] == '0' || (s[1] == '0' && s[0] > '2')) return 0;
class Solution {
private:
int treeHeight(TreeNode * root, bool& balanced) {
if (root == NULL) { balanced = true; return 0; }
bool left, right;
int left_height = treeHeight(root->left, left) + 1;
int right_height = treeHeight(root->right, right) + 1;
balanced = left && right;
if (!balanced) return 0;
int result = left_height - right_height;
@codinfox
codinfox / foodHeuristic.py
Created September 16, 2014 01:20
Using the length of minimal spanning tree as the heuristic for food search problem. The total number of expanded nodes is 7002 in A* algorithm. Used DP to improve performance.
def foodHeuristic(state, problem):
"""
Your heuristic for the FoodSearchProblem goes here.
This heuristic must be consistent to ensure correctness. First, try to come up
with an admissible heuristic; almost all admissible heuristics will be consistent
as well.
If using A* ever finds a solution that is worse uniform cost search finds,
your heuristic is *not* consistent, and probably not admissible! On the other hand,
@codinfox
codinfox / cornerheuristic.py
Last active September 21, 2015 13:14
An inconsistent version of corner heuristic for berkeley pacman project
def cornersHeuristic(state, problem):
corners = problem.corners # These are the corner coordinates
walls = problem.walls # These are the walls of the maze, as a Grid (game.py)
cornerFlags = state[1]
current = state[0]
heur = 0
unexplored = []
for idx in range(4):