自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(67)
  • 收藏
  • 关注

原创 TCP粘包

TCP粘包及其解决方法

2022-07-09 18:25:33 427 1

原创 OpenCV-- Mat类的一些属性记录

#include <iostream>#include <opencv2/opencv.hpp>using namespace std;using namespace cv;int main() { cv::Mat img(3, 4, CV_16UC4, Scalar_<uchar>(1, 2, 3, 4)); cout << img << endl; cout << "dims: " <<.

2020-12-09 15:59:47 111

原创 关于mysql 远程连接的坑

一般来说,我们使用grand all privileges on *.* to 'root'@'%' identified by 'passwd' with grant option;flush privileges;就可以解决。但是经过我的实验证明,在linux环境下装的mysql,配置文件默认是 有这个选项: bindaddress: 127.0.0.1 这个限制了只能使用本地访问。需要把这一行注释掉,然后service mysql restart; 重启即可实现远程连接。...

2020-10-27 16:30:27 165

原创 使用C++11 写一段死锁程序

#include <thread>#include <mutex>#include <unistd.h>#include <iostream>using namespace std;static int a = 0;static int b = 0;static mutex mtx_a;static mutex mtx_b;void thread_func(){ mtx_b.lock(); cout <&l.

2020-10-12 09:55:10 288

原创 简化路径

思路就是利用栈的思想,同时涉及到子串的分割,C++ 的string 不提供分割方法,因此利用python代码会更简洁和易于理解,class Solution: def simplifyPath(self, path: str) -> str: stack = [] path = path.split("/") for i in path: if i == '..': if stack

2020-10-10 21:18:48 112

原创 跳跃游戏1 and 跳跃游戏2

class Solution {public: // 贪心算法 // 怎么贪心 bool canJump(vector<int>& nums) { int maxPos = 0; for(int i = 0; i < nums.size(); i++){ if(i > maxPos){ return false; } .

2020-10-10 15:26:59 88

原创 leetcode--缺失的第一个正数(原地hash)

class Solution {public: int firstMissingPositive(vector<int>& nums) { int len = nums.size(); for(int i = 0; i < len; i++){ while(nums[i] > 0 && nums[i] <= len && nums[nums[i]-1] != nums[.

2020-10-10 13:56:15 75

原创 数组去重问题

关于数组去重的问题,首先要进行排序。 经典题目: 三数之和,,,,,,, 组合总数 ||

2020-10-10 13:33:48 60

原创 leetcode-- 外观数列(C++)

总的思路就是递归里写循环。主要这有一个递归的关系。class Solution {public: // 递归 string countAndSay(int n) { if(n == 1){ return "1"; } string previous = countAndSay(n-1); string result = ""; int count = 1; for(

2020-10-10 13:23:31 97

原创 二分查找--- 在排序数组中查找元素的第一个位置和最后一个位置。

class Solution {public: // 缩小搜索空间法 vector<int> searchRange(vector<int>& nums, int target) { vector<int> res(2, -1); if(nums.size() == 0){ return res; } int left = 0; int ri.

2020-10-10 10:46:46 228

原创 二分查找----搜索旋转排序数组(C++)

这道题的边界条件真是非常复杂,也不是复杂,就是如果不debug,很难写出全ac的程序。下面给出一版:其中也是打了很多补丁。#include <iostream>#include <vector>using namespace std;class Solution {public: int search(vector<int>& nums, int target) { int left = 0; int r

2020-10-10 10:32:25 197

原创 leetcode-删除排序数组中的重复项 1- 2(C++)

第一题 :class Solution {public: int removeDuplicates(vector<int>& nums) { if(nums.size() == 0){ return 0; } // 覆盖法 int j = 0; for(int i = 0; i < nums.size(); i++){ if(nums[i.

2020-10-09 20:44:11 53

原创 leetcode -24 两两交换链表的结点(C++)

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* swapPairs(ListNode* head) { ListNode* dummy = new.

2020-10-09 20:14:14 66

原创 std::greater<T> 与 std::less<T> 区别

首先,这两个是仿函数,是一个类重载了operator(), 定义在头文件<functional>中,用在 sort() 的第三个参数greater<int> 用在 sort(vec.begin(), vec.end(), greater<int>) , 说明是从大到小排序less<int>用在 sort(vec.begin(), vec.end(), greater<int>) , 说明是从小到大排序用在 priority...

2020-10-09 19:42:11 620

原创 最长公共子串问题

最长公共子串与最长公共子序列区别:最长公共子串要求在原字符串中是连续的,而子序列只需要保持相对顺序一致,并不要求连续。最长公共子串描述有两个字符串(可能包含空格),请找出其中最长的公共连续子串,输出其长度。例如:输入: abcde bcd输出: 3代码暴力法public int getLCS(String s, String t){ if(s == null || t == null){ return 0; } int l1 = s.length

2020-10-09 14:00:05 338

原创 C++ 虚函数与继承的一些问题

#include <stdio.h>class B {public: int B1; virtual void fun1() { B1++; printf("in B fun1 B1 %p\n", this); } B() { B1 = 0; this->fun1(); }// B(const B& b1) {}};class C : public B {.

2020-10-03 16:22:33 82

原创 leetcode--下一个排列(C++)

#include <vector>#include <iostream>using namespace std;class Solution {public: void nextPermutation(vector<int>& nums) { int i = nums.size() - 2; while(i >= 0 && nums[i+1] <= nums[i]){ .

2020-09-26 17:27:31 74

原创 leetcode--删除有序链表中的重复元素2(C++)

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: // 今天手撕一下这道题目 // 只要有重复, 我们就一个不留,全删了。 // 怎么做呢? ListNode*.

2020-09-26 14:43:36 174

原创 leetcode---括号生成(C++)

括号生成,相当于每个位置都有'(', ')'两种选择。然后是递归条件,我们用两个变量,left, right分别表示'(' , ')'已经使用的个数。如果right > left , 就说明当前使用right 多于left,这种情况是可以进行剪枝的。当left < 0 或right < 0 时,也要return, 当left和right都等于0时,这时结果保存,return.代码如下:class Solution {public: // 什么是有效的 // 回溯

2020-09-25 08:39:36 176

原创 可重入锁与不可重入锁(Java举例)

不可重入锁与重入锁Java多线程的wait()方法和notify()方法这两个方法是成对出现和使用的,要执行这两个方法,有一个前提就是,当前线程必须获其对象的monitor(俗称“锁”),否则会抛出IllegalMonitorStateException异常,所以这两个方法必须在同步块代码里面调用。wait():阻塞当前线程notify():唤起被wait()阻塞的线程不可重入锁所谓不可重入锁,即若当前线程执行某个方法已经获取了该锁,那么在方法中尝试再次获取锁时们就会获取不到被阻塞。我们尝试设计

2020-09-24 21:24:37 581

原创 tcp为什么三次握手而不是两次或者四次(给出面试版答案)

对于这个问题,网上有很多解释,千奇百怪,长篇大论,试图从原理上讲清楚,但是越想讲清楚,越讲不清楚。所谓大道至简,大美天成。能不能精简的总结下呢?在知乎回答的车小胖的答案中给的非常精简。三次握手握的是啥?答: 是通信双方原始数据原点的序列号----------------------对就是序列号。因为只有序列号对应上了,才能保证可靠传输。假设中间丢包了,在任意阶段,那么序列号都对应不上,那么就证明不是本次连接中的报文。因此其他答案中“那些为了防止已失效的连接请求报文段突然又传送到了服务端,

2020-09-24 20:45:08 140

原创 leetcode--盛水最多的容器(C++)

class Solution {public: int maxArea(vector<int>& height) { // 双指针 int max_area = 0; int left = 0; int right = height.size() - 1; while(left < right){ int tmp = min(height[left], height[.

2020-09-23 19:56:36 119

原创 leetcode--整数反转(C++)

class Solution {public: int reverse(int x) { // 反转结果 int rev = 0; while(x != 0){ // 取余 int pop = x % 10; x /= 10; // 防止溢出 if( rev > INT_MAX / 10 || (rev =.

2020-09-23 19:45:54 59

原创 leetcode--- Z型变换(C++)

class Solution {public: string convert(string s, int numRows) { if(numRows < 2){ return s; } vector<string> strs(numRows); int flag = -1; int start = 0; for(int i = 0; i < s.leng.

2020-09-23 19:39:15 74

原创 leetcode-- 最长回文子串(C++)

class Solution {public: // 做一下这个题目 // 最长回文子串 // 直观思路: DP // 状态转移方程是啥? // dp[i][j] 表示 s[i:j]的这个子串是否回文串 // 我们只能保证它是回文的时候记录它的长度 string longestPalindrome(string s) { if(s.length() == 0){ return s; } .

2020-09-23 19:13:07 90

原创 leetcode--三数之和(C++)

class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { // 第一个,第二个,第三个都不能重复 // 排序加双指针 vector<vector<int>> res; sort(nums.begin(), nums.end()); // 固定首元素 .

2020-09-23 18:41:02 129

原创 leetcode--- 无重复字符的最长子串(C++)

滑动窗口法class Solution {public: int lengthOfLongestSubstring(string s) { int left = 0; int right = 0; int maxlen = 0; set<char> window; while(right < s.size()){ while(window.find(s[right])

2020-09-23 13:48:21 57

原创 leetcode--两数相加(C++)

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListN.

2020-09-23 13:20:40 66

原创 leetcode- - 两数之和(C++)

一遍hashmap代码:class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { // 一次hashmap map<int, int> m; for(int i = 0; i < nums.size(); i++){ if(m.find(target - nums[i]) !

2020-09-23 13:11:01 62

原创 全排列(C++)版 经典回溯模板

#include <vector>#include <iostream>using namespace std;static vector<vector<int>> res;void dfs(vector<int>& choice, vector<int> path){ if(choice.empty()){ res.push_back(path); return; }.

2020-09-23 10:15:53 393

原创 约瑟夫环 迭代法

#include <iostream>using namespace std;int main(){ int m, n; cin >> n >> m; int last = 0; for(int i = 2; i <= n; i++){ last = (last + m) % i; } cout << " 最后退出的编号为: " << last << end.

2020-09-21 17:15:30 541 3

原创 链表中环的问题终极版

判断链表中是否有环如果有环,找出环的入口计算环的长度计算链表的长度判断两个无环链表是否相交求相交的第一个节点

2020-09-21 16:23:55 60

原创 C++ new关键字抛出bad_alloc

C++ newC++ new 关键字底层调用了malloc来分配内存,但是会出现申请内存失败的情况。当new 申请内存失败时,会抛出bad_alloc异常,那么针对这种情况,我们有两种处理方式。方法1. try-catch 捕获异常当new获取内存失败时,抛出bad_alloc异常,我们可以捕获异常,但是这种方法非常繁琐,我们在程序中可能会大量用到new,我们要加很多try-catch块。对程序员不友好。#include <iostream>#include <new>u

2020-09-19 15:47:02 1670

原创 使用C++实现生产者消费者模式

使用互斥锁加条件变量核心要素: 一个互斥锁,两个条件变量 实现 有界阻塞队列#include <iostream>#include <queue>#include <stdlib.h>#include <pthread.h>#include <unistd.h>#define NUM 8class BlockQueue{private: std::queue<int> q; int cap;

2020-09-14 20:25:25 403 3

原创 跳跃游戏(python)

本篇文章记录一下leetcode 上"跳跃游戏"这两道题目第一道题目是要让我们判断是否可以跳到最后位置:我们可以遍历这个数组,然后更新可以跳到的最远位置,如果可以跳到最后,返回true,否则返回false。...

2020-09-08 20:02:32 860

原创 从快速排序到TopK问题 (Python)

快速排序class Solution: def quickSort(self, nums): # 递归函数 def quick_helper(left, right): if left >= right: return p = partition(left, right) quick_helper(left, p-1) quic

2020-09-07 14:13:33 635

原创 全排列问题引发的回溯思考

回溯问题其实就像是多叉树的遍历,有几个关键的点。一个是候选点列表的改变,一个是最终保存的结果。一个是路径。回溯的关键在于回退到上一步的状态。那么我们以全排列为切入点,理解一下回溯思想。我们以python为例:既然是多叉树的遍历,那么我们可以逐步缩小候选列表,也可以用剪枝的方式实现。剪枝的方式简单一些:剪枝:def permutation(nums): res = [] # 最终结果 # 路径: track # 候选列表: 动态变化 nums def

2020-08-07 15:54:33 185

原创 判断一个字符串数组中有多少个循环单词的个数

循环单词定义:str1:picturestr2 : turepicstr1 与str2 是循环单词代码:def func(strs): count = 0 flags = [False for _ in range(len(strs))] for i in range(len(strs)): for j in range(i+1, len(strs)): if flags[j] == True: ..

2020-08-06 15:45:54 249

原创 编译opencv 的cpp程序

g++ main.cpp `pkg-config --libs --cflags opencv` -std=c++11 -o main

2020-07-27 15:57:07 244

原创 C++ 实现线程安全的单例模式

#include <iostream>class Singleton{ public: ~Singleton()=default; Singleton(const Singleton&) = delete; Singleton& operator=(const Singleton&) = delete; // 局部静态变量 static Singleton& getInstance(){ static Singleton instance;.

2020-06-30 08:17:42 104

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除