自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 剑指 Offer 14- II. 剪绳子 II

力扣class Solution {public: int cuttingRope(int n) { if (n <= 3) return n - 1; long ret = 1; if (n % 3 == 1){ ret = 4; n = n - 4; } if (n % 3 == 2){ ret = 2;

2021-08-13 19:57:49 103

原创 剑指 Offer 16. 数值的整数次方

力扣快速幂,采用二分思想class Solution {public: double myPow(double x, int n) { double ans=1; bool negative=n<0;//记录是否是负数 while(n) { if(n%2) ans*=x; x*=x; n/=2; } if(negative

2021-08-13 19:52:34 104

原创 剑指 Offer 14- I. 剪绳子

力扣class Solution {public: //贪心算法 //当n > 5时,尽可能多地剪长度为3地绳子;当n = 4时,把绳子剪成两段长度为2的绳子 int cuttingRope(int n) { if(n <= 3) return 1 * (n-1); int res = 1; if(n % 3 == 1) res *= 4,n -= 4; if(n % 3 == 2) res *= 2,

2021-08-13 17:32:01 107

原创 剑指 Offer 15. 二进制中1的个数

力扣class Solution {public: int hammingWeight(uint32_t n) { int res = 0; for(int i = 0; i < 32 ;i++){ //n与2^i相与,若此位为0,则与运算结果为0 if(n & (1 << i)) res++; } return res; }};...

2021-08-13 14:45:25 62

原创 剑指 Offer 13. 机器人的运动范围

力扣class Solution {public: int get_sum(pair<int, int> p) { int s = 0; while (p.first) { s += p.first % 10; p.first /= 10; } while (p.second) { s += p.second % 10; p.

2021-08-12 12:14:35 48

原创 剑指 Offer 12. 矩阵中的路径

力扣class Solution {public: //递归 + 回溯 bool dfs(vector<vector<char>>& board,string word,int u,int x, int y) { if(board[x][y] != word[u]) return false; if(u == word.size()-1) return true; //定义一个数组用来表示上下左右移动

2021-08-11 20:43:26 56

原创 剑指 Offer 11. 旋转数组的最小数字

力扣class Solution {public: //旋转数组将递增数组分为了两个递增序列,且第一个序列的元素大于等于第二个序列的元素 int minArray(vector<int>& numbers) { int n = numbers.size() - 1; if(n < 0) return -1; //去除尾部与首部相同的元素 while(n > 0 && numbe

2021-08-11 19:54:40 49

原创 2021-08-10等差数列划分

力扣class Solution {public: int numberOfArithmeticSlices(vector<int>& nums) { int n = nums.size(), l = 0, ans = 0; for(int i = 2; i < n; i++) nums[i] - nums[i-1] == nums[i-1] - nums[i-2] ? ans += ++l : l = 0; retur

2021-08-10 11:02:48 66

原创 剑指 Offer 07. 重建二叉树

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ //前序遍历是 根左右,因此第一个元素即为根结点 [[根] [左子树的前序遍历] [右子树的前序遍历]] //中

2021-08-10 10:36:25 78

原创 初探伪线程

什么是伪线程一种基于socket的与外部通信的统一架构,免除了每个用户空间模块编写基于socket代码的时候都需要编写一套基础框架。伪线程与线程的区别伪线程并不是真正意义上的操作系统线程,不在操作系统的调度队列中对于使用伪线程机制的模块来说,一般情况下该模块会创建一个真实的线程,然后在这个线程中进行伪线程的调度伪线程相当于在操作系统线程之上的一个线程调度机制伪线程并不能代替线程,一些需要并发,或有可能挂起的操作仍然需要使用线程伪线程有几个队列,优先级分别是?Pending队列:最高优先

2021-08-09 11:16:27 528

原创 剑指 Offer 06. 从尾到头打印链表

力扣/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: vector<int> reversePrint(ListNode* head) { vector&l

2021-08-08 16:10:01 41

原创 2021-08-08第 N 个泰波那契数

力扣class Solution {public: int tribonacci(int n) { vector<int> res = {0,1,1}; if(n > 0 && n < 3) return res[n]; int i = 3; while(i <= n) { int sum = res[i-1] + res[i-2] + res[

2021-08-08 15:57:44 44

原创 剑指 Offer 05. 替换空格

力扣class Solution {public: string replaceSpace(string s) { string res; for(auto x : s) { if(x == ' ') { res += "%20"; }else{ res += x; } }

2021-08-08 15:41:02 43

原创 剑指 Offer 04. 二维数组中的查找

力扣class Solution {public: bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) { if(matrix.empty() || matrix[0].empty()) return false; int m = matrix.size() - 1; int n = matrix[0].size() - 1;

2021-08-08 15:40:03 47

原创 剑指 Offer 03 数组中重复的数字

力扣class Solution {public: int findRepeatNumber(vector<int>& nums) { //大小为0 ~ n-1范围内的数字,对应于从0开始的数组下标,每个下标都可对应一个数字 //遍历数组,若数组中的元素nums[i] != i,则将其换到对应的下标位置,即nums[nums[i]] //交换过程中,若发现nums[nums[i]]的位置已经是正确元素,则此元素重复

2021-08-08 15:38:30 75

原创 2021-08-06 最多能完成排序的块

方法基于两个事实:记0到当前位置i的最大值为max(arr[0~i])由于数组是0-n的排列, 故0-i间的最大值不会小于i, 即max(arr[0~i]) >= i如果当前最大值大于当前位置i, 那么i的右边一定有小于当前最大值的元素, 则无法在i切分数组, 即max(arr[0~i]) <= i综上, 当且仅当 max(arr[0~i] == i) 时, 可以切分数组作者:moreality链接:https://leetcode-cn.com/problems/max-ch.

2021-08-06 20:57:17 59

原创 2021-08-05 PIM-DM

1.PIM的英文全称?为什么叫这个名字?PIM 是Protocol Independent Multicast(协议无关组播)的简称,表示可以利用静态路由或者任意单播路由协议(包括RIP、OSPF、IS-IS、BGP 等)所生成的单播路由表为IP 组播提供路由。组播路由与所采用的单播路由协议无关,只要能够通过单播路由协议产生相应的组播路由表项即可。PIM 借助RPF(Reverse PathForwarding,逆向路径转发)机制实现对组播报文的转发。当组播报文到达本地设备时,首先对其进行RPF 检查:若

2021-08-06 11:13:20 746

原创 2021-08-05数组嵌套

力扣暴力法,超出时间限制了//暴力法(超出时间限制)//对数组中每一个数都去构造集合S,找出最大的集合Sclass Solution {public: int arrayNesting(vector<int>& nums) { int n = nums.size(); int max = 0; for(int i = 0;i < n ;i++) { unordered_map&

2021-08-05 22:04:23 124

原创 2021-08-05托普利茨矩阵

//根据定义,当且仅当矩阵中每个元素都与其左上角相邻的元素(如果存在)相等时,该矩阵为托普利茨矩阵。因此,我们遍历该矩阵,将每一个元素和它左上角的元素相比对即可。class Solution {public: bool isToeplitzMatrix(vector<vector<int>>& matrix) { int m = matrix.size(), n = matrix[0].size(); for (int i = 1

2021-08-05 21:18:32 57

原创 2021-08-05 数组的度

力扣//维护一个哈希表,其中存储了每个数字出现的次数、其第一次出现的位置以及最后一次出现的位置//最短连续子数组必然包含出现频次最高的数字的所有个体,minLen = vec[2] - vec[1] + 1class Solution {public: int findShortestSubArray(vector<int>& nums) { unordered_map<int,vector<int>> map;

2021-08-05 20:52:04 53

原创 2021-08-04 IGMP

IGMPv1:查询和响应机制由两种报文实现:普遍组查询报文(General Query):查询器向共享网络上所有主机和路由器发送的查询报文,用于查询哪些组播组存在成员成员关系报告报文(Report):主机向查询器发送的报告报文,用于申请加入某个组播组或者应答查询报文。IGMPv2为了改善组成员离开机制,新增了两种报文:成员离开报文(Leave):成员离开组播组时主动向查询器发送的报文,用于宣告自己离开了某个组播组。成员离开报文目的地址为224.0.0.2(所有的路由节点地址,224.0.

2021-08-05 11:19:42 1822

原创 2021-08-04寻找重复数

力扣你设计的解决方案必须不修改数组 nums 且只用常量级 O(1) 的额外空间。对每一个元素,遍历其后元素,判断是否有相等的超出时间限制class Solution {public: int findDuplicate(vector<int>& nums) { for(int i = 0;i<nums.size();i++) { for(int j = i+1;j<nums.size();j++)

2021-08-04 20:40:26 37

原创 2021-08-04错误的集合

力扣利用哈希表,存储每一个元素,遇见重复的便记录,然后再遍历哈希表,看缺少谁class Solution {public: vector<int> findErrorNums(vector<int>& nums) { vector<int> res; unordered_map<int,int> hashtable; for(auto x : nums) {

2021-08-04 20:10:45 50

原创 2021-08-04组播基础

1. 什么是组播?组播、单播、广播的区别单播是主机间一对一的通讯模式,网络中的设备根据网络报文中包含的目的地址选择传输路径,将单播报文传送到指定的目的地,只对接收到的数据进行转发,不会进行复制。它能够针对每台主机及时的响应,现在的网页浏览全部都是采用单播模式。广播是主机间一对所有的通讯模式,设备会将报文发送到网络中的所有可能接收者。设备简单地将它收到的任何广播报文都复制并转发到除该报文到达的接口外的每个接口。广播处理流程简单,不用选择路径。组播是主机间一对多的通讯模式, 组播是一种允许一个或多个组播

2021-08-04 14:47:47 771

原创 2021-08-03有序矩阵中第 K 小的元素

力扣二分查找法矩阵中最小值为左上角值,最大值为右下角值,令这两个值为二分查找的初始边界left和right对于mid = left + right/2,在矩阵中查找小于等于它的值的数量num,如果num >k,则应该在(left,mid)中继续查找,否则在(mid+1,right)中查找class Solution {public: bool check(vector<vector<int>>& matrix, int mid,int k) {

2021-08-03 22:51:21 53

原创 2021-08-02搜索二维矩阵Ⅱ

力扣//左下角开始遍历class Solution {public: bool searchMatrix(vector<vector<int>>& matrix, int target) { int m = matrix.size(); int n = matrix[0].size(); int i = m-1; int j = 0; while(i >= 0 &&am

2021-08-02 20:07:06 58

原创 2021-08-01最大连续1的个数

力扣class Solution {public: int findMaxConsecutiveOnes(vector<int>& nums) { int res = 0; int cur = 0; int count = 0; while(cur <nums.size()) { if(nums[cur] == 1) {

2021-08-01 17:01:03 34

原创 2021-08-01重塑矩阵

力扣class Solution {public: vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) { vector<int> temp; for(auto x :mat) { for(auto ch : x) {

2021-08-01 16:49:46 30

原创 2021-08-01移动0

力扣class Solution {public: void moveZeroes(vector<int>& nums) { int n = nums.size(), left = 0, right = 0; while (right < n) { if (nums[right]) { swap(nums[left], nums[right]); le

2021-08-01 16:11:26 45

原创 2021-07-18计数二进制字串

力扣class Solution {public: int countBinarySubstrings(string s) { int ptr = 0, n = s.size(), last = 0, ans = 0; while (ptr < n) { char c = s[ptr]; int count = 0; while (ptr < n && s[ptr]

2021-07-18 11:32:56 59

原创 2021-07-18回文数

力扣class Solution {public: bool isPalindrome(int x) { if(x<0) return false; vector<int> v; //提取单个数字 while(x) { int temp = x%10; v.push_back(temp); x /= 10; }

2021-07-18 11:05:51 79

原创 2021-07-18回文子串

力扣中心点法对于长度为奇数的字符串,中心点即为len/2而对于长度为偶数的字符串,中心为应该是其len/2以及len/2+1将字符串的每一个字符都当作中心点遍历一次,还要将每相邻两个字符当作中心点遍历一次,一共是2*len-1次class Solution {public: int countSubstrings(string s) { int res = 0; int len = s.size(); //单个中心点为len个,双个中心

2021-07-18 10:45:56 56

原创 2021-07-16服务封装

#ifndef _EasyTcpServer_hpp_#define _EasyTcpServer_hpp_#ifdef _WIN32 #define WIN32_LEAN_AND_MEAN #define _WINSOCK_DEPRECATED_NO_WARNINGS #include<windows.h> #include<WinSock2.h> #pragma comment(lib,"ws2_32.lib")#else #include<unistd

2021-07-16 22:15:39 108

原创 2021-07-16客户端封装

#ifndef _EasyTcpClient_hpp_#define _EasyTcpClient_hpp_#ifdef _WIN32 #define WIN32_LEAN_AND_MEAN #define _WINSOCK_DEPRECATED_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #include<windows.h> #include<WinSock2.h> #pragma comment(lib,"ws2

2021-07-16 22:14:54 85

原创 2021-07-16同构字符串

力扣维护两个哈希表,将两字符串互相映射,若有一个不满足条件,则返回falseclass Solution {public: bool isIsomorphic(string s, string t) { if(s.length() != t.length()) return false; unordered_map<char,char> table; unordered_map<char,char> table2;

2021-07-16 16:07:08 40

原创 2021-07-16最长回文串

力扣class Solution {public: int longestPalindrome(string s) { int res = 0; unordered_map<int,int> table; for(auto x:s) { table[x]++; } for(auto x : table) { res += x.

2021-07-16 11:22:49 37

原创 2021-07-16有效的字母异位词

力扣暴力法class Solution {public: bool isAnagram(string s, string t) { if(s.size() != t.size()) return false; sort(s.begin(),s.end()); sort(t.begin(),t.end()); return s==t; }};维护一个频次数组class Solution {public:

2021-07-16 10:39:48 55

原创 2021-07-15字符串循环移位

翻转字符串Reverse(int* arr, int b, int e){ for(; b < e; b++, e--) { int temp = arr[e]; arr[e] = arr[b]; arr[b] = temp; }}RightShift(int* arr, int N, int K){ K %= N; Reverse(arr, 0, N – K - 1);

2021-07-15 09:50:31 56

原创 2021-07-15字符串循环移位包含

对于给定的两个字符串s1和s2,s1循环移位的结果其实就是s1s1,因此,只需要判断s1s1中是否包含s2即可{ string temp = s1; temp += s1; return strstr(temp,s2);}

2021-07-15 09:45:51 95

原创 2021-07-14服务端linux/MacOS移植

//#pragma comment(lib,"ws2_32.lib") //声明静态链接库#ifdef _WIN32#define WIN32_LEAN_AND_MEAN#define _WINSOCK_DEPRECATED_NO_WARNINGS#include<windows.h>#include<WinSock2.h>#else#include<unistd.h>#include<arpa/inet.h>#include<

2021-07-14 21:50:54 74

空空如也

空空如也

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

TA关注的人

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