This file contains hidden or 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
void reverse(const char* s, char* res) | |
{ | |
int count = 0; | |
reverse_impl(s, res, count); | |
res[count] = '\0'; | |
} | |
void reverse_impl(const char* s, char* res, int& count) | |
{ | |
if (s[0] != '\0') |
This file contains hidden or 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
// O(n^2) solution | |
void remove_duplicate(char* s) | |
{ | |
const int size = strlen(s); | |
for (int i = 0; i < size - 1; ++i) | |
{ | |
if (s[i] == '\0') | |
{ | |
continue; | |
} |
This file contains hidden or 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
// A rather simple but elegant solution. | |
int get_bit_num(unsigned int num) | |
{ | |
int count = 0; | |
while (num != 0) | |
{ | |
num &= num - 1; | |
++count; | |
} | |
return count; |
This file contains hidden or 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
struct Node | |
{ | |
int data; | |
Node* next; | |
}; | |
Node* nth_last(Node* list, const int n) | |
{ | |
Node* nth = list; | |
int count = 0; |
This file contains hidden or 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 <stack> | |
class MyQueue | |
{ | |
std::stack<int> m_first; | |
std::stack<int> m_second; | |
public: | |
void Push(int value) | |
{ | |
m_first.push(value); |
This file contains hidden or 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
struct Node | |
{ | |
int data; | |
Node* left; | |
Node* right; | |
}; | |
bool is_balanced(const Node* node) | |
{ | |
int depth = 0; |
This file contains hidden or 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
struct Node | |
{ | |
int data; | |
Node* left; | |
Node* right; | |
Node* parent; | |
}; | |
Node* next_in_order(const Node* node) | |
{ |
This file contains hidden or 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
// prime circular list | |
const int MAXN = 100; | |
static int primes[MAXN]; | |
void gen_primes(const int n) | |
{ | |
for (int i = 1; i * i <= n; ++i) | |
{ | |
for (int j = 1; i * j <= n; ++j) |
This file contains hidden or 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
// Union Find Set | |
const int MAXN = 100; | |
int father[MAXN] = {0}; | |
int rank[MAXN] = {0}; | |
int find_set(const int x) | |
{ | |
if (father[x] != x) | |
{ |
This file contains hidden or 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
sturct TrieNode | |
{ | |
TrieNode() | |
{ | |
memset(next, 0, 26 * sizeof(TrieNode*)); | |
count = 0; | |
} | |
TrieNode* next[26]; // We assume that only alphabetical char will be present. | |
int count; |