自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 go的reflect

reflect反射是指在程序运行期对程序本身进行访问和修改的能力,是一种检查接口变量的类型和值的机制。reflect包封装了反射相关的方法,获取类型信息reflect.TypeOf是静态的,获取值信息reflect.ValueOf是动态的。func TypeOf(i interface{}) Typetype Type interfacefunc ValueOf(i interface{}) Valuetype Value struct在Go的实现中,一个interface类型的变量存储.

2022-04-06 23:16:16 885

原创 golang的context

1.context简介在go的http包的server中,每一个对应的请求都有一个goroutine负责处理,处理函数通常会启动额外的goroutine去处理,如果一个请求被取消或者超时,用来处理该请求的goroutine应该及时退出,这样就不会有大量的goroutine去占用资源。Context类型专用来简化对于处理单个请求的多个goroutine之间与请求域的数据、取消信号、截止时间等相关操作,这些操作可能涉及多个API调用。因此对服务器的请求应该去创建上下文,对服务器的传输调用也应该接收上下文

2022-04-01 23:10:46 1484

原创 golang协程

协程(coroutine)是Go语言中的轻量级线程实现,由Go运行时(runtime)管理。在一个函数调用前加上go关键字,这次调用就会在一个新的goroutine中并发执行。当被调用的函数返回时,这个goroutine也自动结束。需要注意的是,如果这个函数有返回值,那么这个返回值会被丢弃。func longWait() { fmt.Println("Beginning longWait()") time.Sleep(5 * 1e9) fmt.Println("End of longWait

2022-02-15 15:02:28 3168

原创 bst

// BST树代码实现template<typename T, typename Comp = less<T>>class BSTree{public: // 初始化根节点和函数对象+lambda表达式 BSTree(Comp comp = Comp()) :root_(nullptr) , comp_(comp) {} // 层序遍历思想释放BST树所有节点资源 ~BSTree() { if (root_ != nullptr) { qu.

2021-03-13 23:18:57 154

原创 232. 用栈实现队列

class MyQueue {private: stack<int> inStack, outStack; void in2out() { while (inStack.empty() != true) { outStack.push(inStack.top()); inStack.pop(); } }public: MyQueue() {}...

2021-03-05 13:33:00 121

原创 354. 俄罗斯套娃信封问题

class Solution {public: int maxEnvelopes(vector<vector<int>>& envelopes) { if (envelopes.empty()) { return 0; } sort(envelopes.begin(), envelopes.end(), [](auto e1, auto e2) { ...

2021-03-04 23:33:11 166 1

原创 338. 比特位计数

class Solution {public: int countOnes(int x) { int ones = 0; while (x > 0) { x &= (x - 1); ones++; } return ones; } vector<int> countBits(int num) { ...

2021-03-03 09:59:15 114

原创 303. 区域和检索 - 数组不可变

class NumArray {public: vector<int> sums; NumArray(vector<int>& nums) { int n = nums.size(); sums.resize(n + 1); for (int i = 0; i < n; i++) { sums[i + 1] = sums[i] + nums[i]...

2021-03-01 13:11:01 71

原创 896. 单调数列

class Solution {public: bool isMonotonic(vector<int> &A) { bool inc = true, dec = true; int n = A.size(); for (int i = 0; i < n - 1; ++i) { if (A[i] > A[i + 1]) { inc = false; ...

2021-02-28 21:48:27 65

原创 867. 转置矩阵

class Solution {public: vector<vector<int>> transpose(vector<vector<int>>& matrix) { int m = matrix.size(), n = matrix[0].size(); vector<vector<int>> transposed(n, vector<int>(m)); ...

2021-02-25 10:39:49 104

原创 832. 翻转图像

class Solution {public: vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) { int n = A.size(); for (int i = 0; i < n; i++) { int left = 0, right = n - 1; whi...

2021-02-24 10:53:50 96

原创 1052. 爱生气的书店老板

class Solution {public: int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int X) { int total = 0; int n = customers.size(); for (int i = 0; i < n; i++) { if (grumpy[i] == 0) { ...

2021-02-23 17:23:50 81

原创 766. 托普利茨矩阵

class Solution {public: bool isToeplitzMatrix(vector<vector<int>>& matrix) { int m = matrix.size(), n = matrix[0].size(); for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) ...

2021-02-22 09:25:02 111

原创 1438. 绝对差不超过限制的最长连续子数组

class Solution {public: int longestSubarray(vector<int>& nums, int limit) { multiset<int> s; int n = nums.size(); int left = 0, right = 0; int maxlen = 0; while (right < n) { ...

2021-02-21 22:35:16 74

原创 697. 数组的度

class Solution {public: int findShortestSubArray(vector<int>& nums) { // 定义统计哈希表和最大频数 unordered_map<int, int> mp; int max_fre = 0; // 统计频数,并计算最大频数 for(int i = 0; i < nums.size(); i ++) { ...

2021-02-20 10:33:13 82

原创 1004. 最大连续1的个数 III

class Solution {public: int longestOnes(vector<int>& A, int K) { int zero = 0,left = 0,right = 0; while(right < A.size()) { if(A[right] == 0) { zero ++; ...

2021-02-19 11:35:54 89

原创 566. 重塑矩阵

class Solution {public: vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) { vector<vector<int>> ans(r,vector<int>(c,0)); int m = nums.size(); int n ...

2021-02-17 09:53:49 117

原创 剑指 Offer 57 - II. 和为s的连续正数序列

class Solution {public: vector<vector<int>> findContinuousSequence(int target) { int i = 1; // 滑动窗口的左边界 int j = 1; // 滑动窗口的右边界 int sum = 0; // 滑动窗口中数字的和 vector<vector<int>> res; while (i <= target / ...

2021-02-14 23:12:37 93

原创 装饰器模式

装饰器模式主要是增加现有类的方法,通过指针传递参数调用其对应的功能。class car{public: virtual void show() = 0;};class Bmw :public car{public: void show() { cout << "bmw基本配置:" << endl; }};class audi :public car{public: void show() { cout << "audi基本

2021-02-14 22:36:46 93

原创 2021-02-12剑指 Offer 15. 二进制中1的个数

class Solution {public: //法一:移位运算 运算32次 //时间消耗大4ms,击败40.59%; 内存5.9MB,击败88.78% int hammingWeight(uint32_t n) { int count = 0; while(n) { if(n & 1) count++; n = n >> 1;//必须是n=n>>1;...

2021-02-12 23:23:30 104

原创 119. 杨辉三角 II

class Solution {public: vector<int> getRow(int rowIndex) { vector<int> row(rowIndex + 1); row[0] = 1; for (int i = 1; i <= rowIndex; ++i) { for (int j = i; j > 0; --j) { ...

2021-02-12 13:07:41 67

原创 567. 字符串的排列

class Solution {public: bool checkInclusion(string s1, string s2) { int n = s1.length(), m = s2.length(); if (n > m) { return false; } vector<int> cnt1(26), cnt2(26); for (int i = 0;...

2021-02-11 19:41:17 116

原创 978. 最长湍流子数组

class Solution {public: int maxTurbulenceSize(vector<int>& arr) { int n = arr.size(); int ret = 1; int left = 0, right = 0; while (right < n - 1) { if (left == right) {...

2021-02-08 23:08:16 91

原创 665. 非递减数列

class Solution {public: bool checkPossibility(vector<int> &nums) { int n = nums.size(), cnt = 0; for (int i = 0; i < n - 1; ++i) { int x = nums[i], y = nums[i + 1]; if (x > y) ...

2021-02-07 15:29:30 82 1

原创 代理模式

这个模式关注类和对象的组合,继承的概念被用来组合接口和定义组合对象获得新功能的方式。通过代理类控制实际对象的访问权限。#include <iostream>#include<memory>using namespace std;class videoSite{public: virtual void freeMovie() = 0; virtual void vipMovie() = 0; virtual void ticketMovie() = 0;};

2021-02-07 11:26:13 144

原创 1423. 可获得的最大点数

class Solution {public: int maxScore(vector<int>& cardPoints, int k) { int n = cardPoints.size(); int preSum = 0; for(int i = 0; i < k; i ++) preSum += cardPoints[i]; int ans = preSum; ...

2021-02-06 11:30:17 77

原创 1208. 尽可能使字符串相等

class Solution {public: int equalSubstring(string s, string t, int maxCost) { if(t.size() == 0) { return 1; } int left = 0; // 窗口左边界 int cost = 0; // 当前窗口消耗 // i作为窗口右边界 f...

2021-02-05 12:15:27 81

原创 剑指 Offer 32 - II. 从上到下打印二叉树 II

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:vector<vector<int>> ...

2021-02-04 14:27:39 57

原创 643. 子数组最大平均数 I

class Solution {public: double findMaxAverage(vector<int>& nums, int k) { int sum = 0; int n = nums.size(); for (int i = 0; i < k; i++) { sum += nums[i]; } int maxSum = sum;...

2021-02-04 12:46:56 58

原创 480. 滑动窗口中位数

首先定义了结果数组 res 和 multiset;遍历输入中的每个元素;如果 multiset 中的元素超过了 k 个,需要把滑动窗口最左边 i - k 位置元素从 multiset 中删除(multiset 自动维护有序性);把遍历到的当前元素插入到 multiset 中(multiset 自动维护有序性);如果当前的位置达到了下标 k - 1,说明滑动窗口中有 k 个元素,开始求滑动窗口的中位数。class Solution {public: vector<double..

2021-02-03 12:12:44 114

原创 工厂模式

工厂模式主要用于有很多类时,主要封装了对象的创建。简单工厂 :把对象的创建封装在一个接口函数中,通过传入标识,返回创建的对象,不用了解对象创建的过程一个工厂不能生产很多种类的汽车,不符合开闭原则。class car{public: car(string name) :_name(name){} virtual void show() = 0;protected: string _name;};class BMW :public car{public: BMW(st

2021-02-02 22:56:20 91

原创 424. 替换后的最长重复字符

class Solution {public: int characterReplacement(string s, int k) { // 用一个数组来保存当前窗口内每个字符的最大的长度 vector<int> cnt (26,0); // 滑动窗口的最左侧 int left = 0; // 记录当前相同的单个字符的最大个数 int maxCharCnt = 0; ...

2021-02-02 20:04:43 52

原创 单例模式

单例模式:一个类创建对象不管多少个,只能去该类型一个对象的实例。(构造函数私有化,并私有定义一个唯一的实例对象)。饿汉:还没有获取实例对象,实例对象就已经产生了,一定是线程安全的。class single{public: static single* getinstance() { return &instance; }private: static single instance; single() { } single(const single&)

2021-02-01 19:46:00 115

原创 888. 公平的糖果棒交换

class Solution {public: vector<int> fairCandySwap(vector<int>& A, vector<int>& B) { int diff = 0; //A集合元素和与B集合元素和的差值 unordered_set<int> dicB; //B元素的哈希集合 for (auto a : A) diff += a; ...

2021-02-01 11:54:16 45

原创 剑指 Offer 57. 和为s的两个数字

class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { int j = nums.size() - 1; for(int i = 0; i < nums.size() - 1; i++) { while(nums[i] + nums[j] > target)//j一直往前走...

2021-01-31 13:27:14 46

原创 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

class Solution {public: vector<int> exchange(vector<int>& nums) { int low = 0, fast = 0; while (fast < nums.size()) { if (nums[fast] & 1) { swap(nums[low], nums[fast]); ...

2021-01-30 18:12:39 60

原创 724. 寻找数组的中心索引

class Solution {public: int pivotIndex(vector<int> &nums) { int total = accumulate(nums.begin(), nums.end(), 0); int sum = 0; for (int i = 0; i < nums.size(); ++i) { if (2 * sum + nums[...

2021-01-28 11:18:47 69

原创 C++多线程

如何创建启动一个线程std::thread创建一个线程对象,传入线程所需要的的线程函数和参数,线程自动开始执行。void threadHandle1(){ //子线程等待两秒 std::this_thread::sleep_for(std::chrono::seconds(2)); cout << "hello thread1" << endl;}int main(){ //定义了一个线程对象 传入一个线程函数 新线程开始运行 std::thread

2021-01-27 22:46:52 132

原创 剑指 Offer 50. 第一个只出现一次的字符

class Solution {public: char firstUniqChar(string s) { if(s == "") return ' '; unordered_map<char,int> cmap; for(int v : s) { cmap[v]++; } //按字符串的顺序在map中查找到第...

2021-01-27 10:53:54 52

原创 1128. 等价多米诺骨牌对的数量

class Solution {public: int numEquivDominoPairs(vector<vector<int>>& dominoes) { int res = 0; vector<int> ans(100); for(auto v: dominoes) { int val = v[0]>v[1]?v[1]*10+v[0]:v...

2021-01-26 12:00:31 64

空空如也

空空如也

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

TA关注的人

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