Skip to content

Instantly share code, notes, and snippets.

@adamkorg
adamkorg / leetcode_272.cpp
Created April 5, 2020 10:33
Leetcode 272: Closest Binary Search Tree Value II
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
@adamkorg
adamkorg / leetcode_271.cpp
Created April 5, 2020 10:16
Leetcode 271: Encode and Decode Strings
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
using namespace std;
static const int hexStrLen() { return sizeof(int)*2; }
@adamkorg
adamkorg / leetcode_270.cpp
Created April 4, 2020 10:51
Leetcode 270: Closest Binary Search Tree Value
#include <iostream>
#include <cmath>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
@adamkorg
adamkorg / leetcode_269.cpp
Created April 4, 2020 10:39
Leetcode 269: Alien Dictionary
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
using namespace std;
typedef unordered_map<char, unordered_set<char>> GraphMap;
@adamkorg
adamkorg / leetcode_268d.cpp
Created April 3, 2020 09:56
Leetcode 268: Missing Number (Gauss' formula)
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
int missingNumber(vector<int>& nums) {
int len=nums.size();
int res = (len*(len+1))/2;
for (int val : nums) res -= val;
@adamkorg
adamkorg / leetcode_268c.cpp
Created April 3, 2020 09:54
Leetcode 268: Missing Number (xor 1pass)
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
int missingNumber(vector<int>& nums) {
int num=nums.size();
for (int i=0; i<nums.size(); ++i)
num ^= (i ^ nums[i]);
@adamkorg
adamkorg / leetcode_268b.cpp
Created April 3, 2020 09:53
Leetcode 268: Missing Number (xor 2pass)
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
int missingNumber(vector<int>& nums) {
int xorAll=0, xorMissing=0, len=nums.size();
for (int i=0; i<len; ++i) xorMissing ^= nums[i];
for (int i=0; i<len+1; ++i) xorAll ^= i;
@adamkorg
adamkorg / leetcoce_268a.cpp
Created April 3, 2020 09:51
Leetcode 268: Missing Number (hash set)
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
int missingNumber(vector<int>& nums) {
unordered_set<int> s;
for (int n : nums) s.insert(n);
for (int i=0; i<=nums.size(); ++i) {
@adamkorg
adamkorg / leetcode_267.cpp
Created April 2, 2020 17:39
Leetcode 267: Palindrome Permutation II
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
void combinations(string& letters, string& comb, bool odd, char oddch, vector<string>& res) {
if (letters.size() == 0) {
string rev = comb;
@adamkorg
adamkorg / leetcode_266c.cpp
Created April 2, 2020 17:13
Leetcode 266: Palindrome Permutation (hash map)
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
bool canPermutePalindrome(string s) {
unordered_map<char, int> m;
bool odd = (s.size() %2 != 0);
int countOdd=0;