Skip to content

Instantly share code, notes, and snippets.

View necusjz's full-sized avatar
🔫
AFK

necusjz

🔫
AFK
View GitHub Profile
#!/usr/bin/env bash
git filter-branch --force --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
@necusjz
necusjz / 11_MinNumberInRotatedArray.cpp
Last active April 25, 2019 16:57
面试题 11:旋转数组的最小数字
// QuickSort
int Partition(int data[], int length, int start, int end) {
if(data == nullptr || length <= 0 || start < 0 || end >= length) {
throw new std::exception("Invalid parameters");
}
// 生成随机数
int index = RandomInRange(start, end);
// 交换两个数字,不交换指针
Swap(&data[index], &data[end]);
int small = start - 1;
@necusjz
necusjz / 10_Fibonacci.cpp
Created April 25, 2019 16:47
面试题 10:斐波那契数列
long long Fibonacci(unsigned int n) {
if(n <= 0) {
return 0;
}
if(n == 1) {
return 1;
}
return Fibonacci(n-1) + Fibonacci(n-2);
}
@necusjz
necusjz / 09_QueueWithTwoStacks.cpp
Created April 25, 2019 16:40
面试题 9:用两个栈实现队列
template<typename T> void CQueue<T>::appendTail(const T& element) {
stack1.push(element);
}
template<typename T> T CQueue<T>::deleteHead() {
if(stack2.size() <= 0) {
while(stack1.size() > 0) {
T& data = stack1.top();
stack1.pop();
stack2.push(data);
@necusjz
necusjz / 08_NextNodeInBinaryTrees.cpp
Last active April 25, 2019 17:03
面试题 8:二叉树的下一个节点
BinaryTreeNode *GetNext(BinaryTreeNode *pNode) {
if(pNode == nullptr) {
return nullptr;
}
// 初始化下一个节点
BinaryTreeNode *pNext = nullptr;
if(pNode->m_pRight != nullptr) {
BinaryTreeNode *pRight = pNode->m_pRight;
while(pRight->m_pLeft != nullptr) {
pRight = pRight->m_pLeft;
@necusjz
necusjz / 07_ConstructBinaryTree.cpp
Last active April 25, 2019 17:03
面试题 7:重建二叉树
BinaryTreeNode *Construct(int *preorder, int *inorder, int length) {
if(preorder == nullptr || inorder == nullptr || length <= 0) {
return nullptr;
}
return ConstructCore(preorder, preorder + length - 1, inorder, inorder + length - 1);
}
BinaryTreeNode *ConstructCore(int *startPreorder, int *endPreorder, int *startInorder, int *endInorder) {
int rootValue == startPreorder[0];
// 初始化二叉树
@necusjz
necusjz / 06_PrintListInReversedOrder.cpp
Created April 25, 2019 16:19
面试题 6:从尾到头打印链表
void PrintListReversingly_Iteratively(ListNode *pHead) {
std::stack<ListNode*> nodes;
// 注意理解指向指针的指针
ListNode *pNode = pHead;
while(pNode != nullptr) {
nodes.push(pNode)
pNode = pNode->m_pNext;
}
while(!nodes.empty()) {
pNode = nodes.top();
@necusjz
necusjz / 05_ReplaceSpaces.cpp
Created April 25, 2019 14:54
面试题 5:替换空格
void ReplaceBlank(char string[], int length) {
if(string == nullptr || length <= 0) {
return;
}
int originalLength = 0;
int numberOfBlank = 0;
int i = 0;
while(string[i] != '\0') {
++originalLength;
if(sting[i] == ' ') {
@necusjz
necusjz / 03_02_FindDuplicationNoEdit.cpp
Created April 25, 2019 14:46
面试题 3.2:不修改数组找出重复的数字
int getDuplication(const int *numbers, int length) {
if(numbers == nullptr || length <= 0) {
return -1;
}
int start = 1;
int end = length-1;
while(end >= start) {
int middle = ((end - start) >> 1) + start;
int count = countRange(numbers, length, start, middle);
if(count > (middle - start + 1)) {
@necusjz
necusjz / 03_01_FindDuplication.cpp
Last active April 25, 2019 14:38
面试题 3.1:找出数组中重复的数字
bool duplicate(int numbers[], int length, int *duplication) {
if(numbers == nullptr || length <= 0) {
return false;
}
for(int i = 0; i < length; ++i) {
if(numbers[i] < 0 || numbers[i] > length-1) {
return false;
}
}
for(int i = 0; i < length; ++i) {