剑指offer
https://github.com/longf0720/atoffer
单例 是最为最常见的设计模式之一。对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计模式为单例。例如,对于 class Mouse (不是动物的mouse哦),我们应将其设计为 singleton 模式。
你的任务是设计一个 getInstance
方法,对于给定的类,每次调用 getInstance
时,都可得到同一个实例。
//懒汉模式:不到万不得已就不会去实例化类,也就是说在第一次用到类实例的时候才会去实例化
1 int lock_ = 0; 2 class Solution { 3 private: 4 static Solution *instance; 5 6 private: 7 static void Lock() { 8 while (lock_) {} 9 lock_ = 1; 10 } 11 static void UnLock() { 12 if (lock_) lock_=0; 13 } 14 15 public: 16 /** 17 * @return: The same instance of this class every time 18 */ 19 static Solution* getInstance() { 20 // write your code here 21 if (instance == NULL) { 22 Solution::Lock(); 23 if (instance == NULL) { 24 instance = new Solution(); 25 } 26 Solution::UnLock(); 27 } 28 return instance; 29 } 30 }; 31 32 Solution* Solution::instance = NULL;
//饿汉模式:饿了肯定要饥不择食。所以在单例类定义的时候就进行实例化。
//不用加锁
1 class singleton 2 { 3 protected: 4 singleton() 5 {} 6 private: 7 static singleton* p; 8 public: 9 static singleton* initance(); 10 }; 11 singleton* singleton::p = new singleton; 12 singleton* singleton::initance() 13 { 14 return p; 15 }
翻转一个链表
给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null
1 /** 2 * Definition of ListNode 3 * 4 * class ListNode { 5 * public: 6 * int val; 7 * ListNode *next; 8 * 9 * ListNode(int val) { 10 * this->val = val; 11 * this->next = NULL; 12 * } 13 * } 14 */ 15 typedef ListNode* List; 16 class Solution { 17 public: 18 /** 19 * @param head: The first node of linked list. 20 * @return: The new head of reversed linked list. 21 */ 22 ListNode *reverse(ListNode *head) { 23 // write your code here 24 if (head == NULL) { 25 return NULL; 26 } 27 if (head->next == NULL) { 28 return head; 29 } 30 List current = head, origin_next = NULL; 31 head = NULL; 32 while (current != NULL) { 33 origin_next = current->next; 34 if (head == NULL) { 35 head = current; 36 head->next = NULL; 37 } else { 38 // 头插法 39 ListNode *tmp = head; 40 head = current; 41 head->next = tmp; 42 } 43 current = origin_next; 44 } 45 return head; 46 } 47 };
根据前序遍历和中序遍历树构造二叉树.
1 /** 2 * Definition of TreeNode: 3 * class TreeNode { 4 * public: 5 * int val; 6 * TreeNode *left, *right; 7 * TreeNode(int val) { 8 * this->val = val; 9 * this->left = this->right = NULL; 10 * } 11 * } 12 */ 13 14 class Solution { 15 /** 16 *@param preorder : A list of integers that preorder traversal of a tree 17 *@param inorder : A list of integers that inorder traversal of a tree 18 *@return : Root of a tree 19 */ 20 public: 21 TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { 22 // write your code here 23 if (preorder.size() == 0 || inorder.size() == 0) { 24 return NULL; 25 } else if (preorder.size() != inorder.size()) { 26 return NULL; 27 } else { 28 int root = preorder.at(0); 29 // cout<<"root="<<root<<endl; 30 // find root in inorder, and split it 31 int l_node_counter = 0; 32 for (auto item : inorder) { 33 if (item == root) { 34 break; 35 } 36 l_node_counter++; 37 } 38 if (l_node_counter == preorder.size()) { 39 // cann't find root in inorder 40 // throw std::exception("Invalid Input"); 41 perror("Invalid input."); 42 exit(-1); 43 } 44 // cout<<"node amount in left tree is:"<<l_node_counter<<endl; 45 // build left Tree 46 vector<int> l_preorder; 47 vector<int> l_inorder; 48 for (int i = 1; i <= l_node_counter; i++) { 49 l_preorder.push_back(preorder.at(i)); 50 } 51 for (int i = 0; i < l_node_counter; i++) { 52 l_inorder.push_back(inorder.at(i)); 53 } 54 TreeNode *lchild = buildTree(l_preorder, l_inorder); 55 // build right Tree 56 vector<int> r_preorder; 57 vector<int> r_inorder; 58 for (int i = l_node_counter + 1; i < preorder.size(); i++) { 59 r_preorder.push_back(preorder.at(i)); 60 } 61 for (int i = l_node_counter + 1; i < inorder.size(); i++) { 62 r_inorder.push_back(inorder.at(i)); 63 } 64 TreeNode *rchild = buildTree(r_preorder, r_inorder); 65 // build root 66 TreeNode *rootNode = new TreeNode(root); 67 if (rootNode == NULL) { 68 perror("new TreeNode failed, memory not enough"); 69 } else { 70 rootNode->left = lchild; 71 rootNode->right = rchild; 72 } 73 return rootNode; 74 } 75 } 76 };