Skip to content

Instantly share code, notes, and snippets.

@airekans
airekans / string_reverse.cpp
Created August 6, 2013 15:50
[OJ] Careerup Top 150(1.2): Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)
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')
@airekans
airekans / remove_duplicate.cpp
Created August 6, 2013 16:17
[OJ] Careercup Top 150(1.3): Solution: 1. O(n^2): loop directly to remove duplicate, then compact. 2. O(lg n): sort then remove duplicate, then compact.
// 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;
}
@airekans
airekans / bit_convert.cpp
Last active December 20, 2015 18:59
[OJ] Careercup Top 150(5.5): Solution: Write a function to count the number of 1 in a number, then we use XOR to get the number of different bits between A and B.
// 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;
@airekans
airekans / nth_last.cpp
Created August 9, 2013 11:47
[OJ] Careercup Top 150(2.2). I define the nth to last element to be the the node needs to move n steps to get to the last element. So if I want to find the second to last element of a 3-node list, it would be the first node.
struct Node
{
int data;
Node* next;
};
Node* nth_last(Node* list, const int n)
{
Node* nth = list;
int count = 0;
@airekans
airekans / queue_in_stacks.cpp
Created August 9, 2013 11:55
[OJ] Careercup Top 150(3.5):
#include <stack>
class MyQueue
{
std::stack<int> m_first;
std::stack<int> m_second;
public:
void Push(int value)
{
m_first.push(value);
@airekans
airekans / is_tree_balanced.cpp
Last active December 20, 2015 20:49
[OJ] Careercup Top 150(4.1): balance tree checking
struct Node
{
int data;
Node* left;
Node* right;
};
bool is_balanced(const Node* node)
{
int depth = 0;
@airekans
airekans / next_in_order.cpp
Last active December 20, 2015 20:49
[OJ] Careercup Top 150(4.5): next in order
struct Node
{
int data;
Node* left;
Node* right;
Node* parent;
};
Node* next_in_order(const Node* node)
{
@airekans
airekans / prime_circular.cpp
Created August 10, 2013 02:34
[OJ] 素数环: 输入一个整数n, 将整数1, 2, 3, ..., n弄成一个环,使得相邻的两个数的和是素数。输出所有的环,从1开始逆时针的输出。 Solution: 回溯,然后剪枝。
// 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)
@airekans
airekans / find_union_set.cpp
Created August 11, 2013 13:48
Union Find Set(并查集): 基本操作(优势):1. 查找某个元素所在的集合; 2. 合并两个不相交的集合。 目前看到比较多的实现都是基于数组的。
// 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)
{
@airekans
airekans / trie.cpp
Created August 13, 2013 06:41
Trie implementation
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;