This file contains 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
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; |
This file contains 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
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; |
This file contains 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
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++; |
This file contains 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
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()) |
This file contains 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
#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 |
This file contains 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
#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; |
This file contains 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
#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; |
This file contains 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
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; |
This file contains 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
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, |
This file contains 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
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): |
NewerOlder