笔记汇总

单例模式

#include 

class Singleton {
    private:
        Singleton();
        Singleton(const Singleton &other);
    public:
        static Singleton *getInstance();
        static Singleton *m_instance;
};

Singleton *Singleton::m_instance = nullptr;

//线程非安全, 懒汉,多线程情况下可能这个对象会被创建两次
Singleton* Singleton::getInstance() {
    if(m_instance == nullptr) {
        m_instance = new Singleton();
    }
    return m_instance;
}

//线程安全,但是锁的代价很高
Singleton *Singleton::getInstance() {
    Lock lock; // 加锁,treadA获取之后,threadB来了也读不了
    if(m_instance == nullptr) {
        m_instance = new Singleton();
    }
    return m_instance;
}

//双检查锁,DCL,但是会发生reorder,正常情况下最后复制给m_instance,但是reorder之后就会先赋值
//这样就出错了。因为可能还没构造就赋值了,这样返回就错了。
Singleton* Singleton::getInstance() {
    // 我们在这里不锁,如果不是nullptr直接返回就行了
    // 这样在高并发只读的情况下非常优秀,避免了上面那种直接加锁的情况
    // 很屌!
    if(m_instance == nullptr) {
        Lock lock;
        if(m_instance == nullptr) {
            m_instance = new Singleton();
        }
    }
    return m_instance;
}

建树

#include 
#include 
#include 
#include 

#define N 5

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x): val(x), left(NULL), right(NULL) {}
    TreeNode(): val(0), left(NULL), right(NULL) {}
};

int main() {
    // vector nodes;
    // auto root = new TreeNode(0);
    // nodes.push_back(root);
    // for (int i = 1; i <= 5; i ++) {
    //     TreeNode *a = new TreeNode(i);
    //     nodes.push_back(a);
    // }

    // int pr = 0;
    // for (int i = 1; i <= 5;) {
    //     nodes[pr]->left = nodes[i++];
    //     nodes[pr++]->right = nodes[i++];
    // }
    vector node = {0, 1, 2, 5, 4, 3};
    TreeNode allnode[N + 1];
    for (int i = 1; i <= N; i++) {
        allnode[i].val = node[i];
        if (2 * i > N) {
            allnode[i].left = nullptr;
        }
        else {
            allnode[i].left = &allnode[2 * i];
        }
        if (2 * i + 1 > N) {
            allnode[i].right = nullptr;
        }
        else {
            allnode[i].right = &allnode[2 * i + 1];
        }
    }
    TreeNode *tree = &allnode[1];
    stack stk;
    stk.push(tree);
    cout << "preOrder:";
    while (!stk.empty()) {
        auto node = stk.top();
        stk.pop();
        cout << node->val << ' ';
        if (node->right)
            stk.push(node->right);
        if (node->left)
            stk.push(node->left);
    }
    cout << endl;
    cout << "inOrder:";
    stack _stk;
    auto temp = tree;
    while (temp || !stk.empty()) {
        while (temp) {
            stk.push(temp);
            temp = temp->left;
        }
        temp = stk.top();
        stk.pop();
        cout << temp->val << ' ';
        temp = temp->right;
    }

    cout << endl;
    cout << "cengxu:";
    queue q;
    q.push(tree);
    while (!q.empty()) {
        int len = q.size();
        while (len --) {
            auto _node = q.front();
            q.pop();
            cout << _node->val << ' ';
            if (_node->left)
                q.push(_node->left);
            if (_node->right)
                q.push(_node->right);
        }
    }
    cout << endl;
    return 0;
}

LRU缓存

#include 
#include 
#include 
#include 

using namespace std;

class LRUcache {
public:
    LRUcache(int capacity) {
        cap = capacity;
    }
    int get(int key) {
        auto it = _map.find(key);
        if (it == _map.end())
            return -1;
        _list.splice(_list.begin(), _list, it->second);
        return it->second->second;
    }
    void put(int key, int value) {
        auto it = _map.find(key);
        if (it != _map.end()) {
            it->second->second = value;
            _list.splice(_list.begin(), _list, it->second);
            return;
        }
        if (_list.size() == cap) {
            auto last = _list.back();
            _map.erase(last.first);
            _list.pop_back();
        }
        _list.push_front(make_pair(key, value));
        _map[key] = _list.begin();
    }
private:
    int cap;
    list> _list;
    unordered_map>::iterator> _map;
};

int main() {
    LRUcache *obj = new LRUcache(3);
    obj->put(1, 1);
    obj->put(2, 2);
    int p1 = obj->get(2);
    obj->put(2, 3);
    obj->put(3, 3);
    int p2 = obj->get(3);
    obj->put(4, 4);
    int p3 = obj->get(1);
    cout << p1 << ' ' << p2 << ' ' << p3;
    return 0;
}

链表

#include 
#include 
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x): val(x), next(NULL){}
};

class Solution{
public:
    ListNode* reverseListnode(ListNode* head) {
        ListNode *pre = head;
        ListNode *cur = NULL;
        ListNode *temp = NULL;

        while(pre) {
            temp = pre->next;

            pre->next = cur;
            cur = pre;
            pre = temp;
        }
        return cur;
    }
};

ListNode* buildListnode(vector& res, int begin, int end) {
    if(begin > end)
        return NULL;
    ListNode *root = new ListNode(res[begin]);
    root->next = buildListnode(res, begin + 1, end);
    return root;
}

int main() {
    int node;
    vector res;
    while(cin >> node) {
        res.push_back(node);
    }
    ListNode *head = buildListnode(res, 0, res.size() - 1);
    Solution solution;
    ListNode *ans = solution.reverseListnode(head);
    while(ans->next) {
        cout << ans->val << ' ';
        ans = ans->next;
    }
    cout << ans->val << endl;
    return 0;
}

string类

#include  
#include 
using namespace std;

//基本类方法
class String {
    public:
        String(const char *str = NULL);//普通的构造函数, 注意=NULL
        String(const String &other);//拷贝构造函数
        ~String();
        String &operator=(const String &rhs);//=运算符构造,赋值构造函数
        void Show();
    private:
        char *m_data; //保存字符串
};
//方法实现
String::String(const char* str) {
    if(str == NULL) {
        m_data = new char[1];
        *m_data = '\0';
    }
    else {
        int num = strlen(str);
        m_data = new char[num + 1];
        strcpy(m_data, str);
    }
}
String::~String() {
    delete[] m_data;
    m_data = NULL;
    cout << this << "析构" << endl;
}
String::String(const String& other) {
    int len = strlen(other.m_data);
    m_data = new char[len + 1];
    strcpy(m_data, other.m_data);
    cout << "拷贝构造" << endl;
}
//等号运算符重载
//赋值构造函数,将=右边的本类对象的值复制给等号左边的的对象,它不属于构造函数,等号左右两边的对象必须已经被创建
//若没有显示写=运算符重载,系统也会创建一个默认的=运算符重载,只做一个简单的拷贝工作
String & String::operator=(const String &rhs) {
    //首先检测等号右边的是否就是左边的对象本身,如果是,则返回就行
    if(&rhs == this) {
        return *this;
    }
    delete[] m_data;
    int len = strlen(rhs.m_data);
    m_data = new char[len + 1];
    strcpy(m_data, rhs.m_data);
    cout << "=运算符重载,赋值构造函数" << endl;
    return *this;
}
void String::Show() {
    cout << m_data << endl;
}

//测试
int main() {
    //拷贝构造
    String str = "str";
    String str1 = str;
    str1.Show();
    //等号运算符重载构造函数
    // String s1 = "str";
    // String s2;
    // s2 = s1;
    // s2.Show();
    return 0;
}

快排

#include 
#include 
using namespace std;

// void quickSort(int *arr, int l, int r) {
//     int left = l;
//     int right = r;
//     if(left >= right)
//         return;//这一步很重要的。。
//     int pivot = arr[left];
//     while(left < right) {
//         while(left < right && arr[right] >= pivot) {//是while不是if,因为一旦当arr[right]>=pivot时候,我们希望right一直减小,而不是只减少一次
//             right--;
//         }
//         if(left < right && arr[right] < pivot) {//这里也要注意&&后面的可以省略,因为一旦跳出上面的while就肯定满足这里的条件,但是left pivot) {
//             arr[right] = arr[left];
//         }
//         if(left >= right) {
//             arr[left] = pivot;
//         }
//     }
//     quickSort(arr, l, right - 1);
//     quickSort(arr, left + 1, r);
// }

// int main() {
//     int arr[] = {19, 97, 9, 17, 1, 8, 0};
//     int l = 0;
//     int r = sizeof(arr) / sizeof(arr[0]) - 1;
//     quickSort(arr, l, r);
//     cout << "排序后:" << endl;
//     for (int i = 0; i <= r; ++i) {
//         cout << arr[i] << endl;
//     }
// }

// void quickSort(vector &res, int l, int r) {
//     int left = l, right = r;
//     if(left >= right)
//         return;
//     int pivot = res[left];
//     while(left < right) {
//         while(left < right && res[right] >= pivot) {//是while不是if而且一定注意判断right在判断left前面
//             right--;//一定注意如果是升序:pivot为左边,那么应该是high->low!!!!!!!!!!!!!!!!!!!!!!!
//         }//否则导致基准值错误的情况发生https://blog.csdn.net/lengyishanasme/article/details/99256796
//         if(left < right && res[right] < pivot) {
//             res[left] = res[right];
//         }
//         while(left < right && res[left] <= pivot) {
//             left++;
//         }
//         if(left < right && res[left] > pivot) {
//             res[right] = res[left];
//         }
        
//         if(left >= right) {
//             res[left] = pivot;
//         }
//     }
//     quickSort(res, l, right - 1);
//     quickSort(res, left + 1, r);
// }
void quickSort(vector &res, int l, int r) {
    int left = l, right = r;
    if(left >= right)
        return;
    int pivot = res[left];
    while(left < right) {
        while(left < right && res[right] >= pivot) {
            right--;
        }
        if(left < right && res[right] < pivot) {
            res[left] = res[right];
        }
        while(left < right && res[left] <= pivot) {
            left++;
        }
        if(left < right && res[left] > pivot) {
            res[right] = res[left];
        }
        if(left >= right) {
            res[left] = pivot;
        }
    }
    quickSort(res, l, right - 1);
    quickSort(res, left + 1, r);
}
int main() {
    int n;
    cin >> n;
    vector res;
    while(n--) {
        int temp;
        cin >> temp;
        res.push_back(temp);
    }
    int r = res.size()-1;
    int l = 0;
    quickSort(res, l, r);
    for (int i = 0; i <= r; i++) {
        cout << res[i] << ' ';
    }

    return 0;
}

归并

#include 
#include 
using namespace std;

// void merge(vector &nums, int l, int m, int r)
// {
//     int leftsize = m - l;
//     int rightsize = r - m + 1;
//     vector leftnums(leftsize, 0);
//     vector rightnums(rightsize, 0);
//     for (int i = l; i < m; ++i)
//     {
//         leftnums[i - l] = nums[i];
//     }
//     for (int i = m; i <= r; ++i)
//     {
//         rightnums[i - m] = nums[i];
//     }
//     int i = 0, j = 0, k = l;
//     while (i < leftsize && j < rightsize)
//     {
//         if (leftnums[i] < rightnums[j])
//         {
//             nums[k] = leftnums[i];
//             i++;
//             k++;
//         }
//         else
//         {
//             nums[k] = rightnums[j];
//             j++;
//             k++;
//             //count += m - i + 1;
//         }
//     }
//     while (i < leftsize)
//     {
//         nums[k] = leftnums[i];
//         i++;
//         k++;
//     }
//     while (j < rightsize)
//     {
//         nums[k] = rightnums[j];
//         j++;
//         k++;
//     }
// }

// void mergeSort(vector &nums, int l, int r)
// {
//     if (l == r)
//         return;
//     int mid = l + (r - l) / 2;
//     mergeSort(nums, l, mid);
//     mergeSort(nums, mid + 1, r);
//     merge(nums, l, mid + 1, r);
// }
void merge(vector &nums, int l, int m, int r) {
    int leftSize = m - l;
    int rightSize = r - m + 1;
    vector leftNums(leftSize);
    vector rightNums(rightSize);
    for (int i = l; i < m; i++) {
        leftNums[i-l] = nums[i];
    }
    for (int i = m; i <= r; i++) {
        rightNums[i - m] = nums[i];
    }
    int i = 0, j = 0, k = l;
    while(i < leftSize && j < rightSize) {
        if(leftNums[i] < rightNums[j]) {
            nums[k++] = leftNums[i++];
        }
        else {
            nums[k++] = rightNums[j++];
        }
    }
    while(i < leftSize) {
        nums[k++] = leftNums[i++];
    }
    while(j < rightSize) {
        nums[k++] = rightNums[j++];
    }
}
void mergeSort(vector &nums, int l, int r) {
    if(l >= r)
        return;
    int mid = l + (r - l) / 2;
    mergeSort(nums, l, mid);
    mergeSort(nums, mid + 1, r);
    merge(nums, l, mid+1, r);
}

int main() {
    vector nums = {8, 7, 2, 11, 4, 15, 36, 48, 1, 10, 123, 23, 14, 5};
    int l = 0;
    int r = 13;
    mergeSort(nums, l, r);
    for (int i = 0; i <= r; ++i) {
        cout << nums[i] << endl;
    }
    return 0;
}

冒泡

#include 
#include 
#include 
using namespace std;

// void maopao(int *arr, int length) {
//     int temp;
//     for (int i = 0; i < length - 1; ++i) {
//         for (int j = 0; j < length - i - 1; ++j) {
//             if(arr[j+1] < arr[j]) {
//                 temp = arr[j];
//                 arr[j] = arr[j+1];
//                 arr[j+1] = temp;
//             }
//         }
//     }
// }

// int main() {
//     int arr[] = {3,2,6,13,12,42,12,5,1,7};
//     int length = sizeof(arr) / sizeof(arr[0]);
//     maopao(arr, length);
//     cout << endl
//          << "排序后:" << endl;
//     for (int i = 0; i < length; ++i) {
//         cout << arr[i] << endl;
//     }
// }

void maopao(vector& res) {
    int len = res.size();
    int t;
    for (int i = 0; i < len-1; i++) {
        for (int j = 0; j < len - i - 1; j++) {
            if(res[j+1] < res[j]) {
                t = res[j];
                res[j] = res[j+1];
                res[j+1] = t;
            }
        }
    }
}

int main() {
    int n;
    cin >> n;
    vector res;
    while(n--) {
        int temp;
        cin >> temp;
        res.push_back(temp);
    }
    maopao(res);
    for (int i = 0; i < res.size(); i++)
        cout << res[i] << ' ';
    return 0;
}

你可能感兴趣的:(笔记汇总)