自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(375)
  • 资源 (4)
  • 收藏
  • 关注

原创 软件方法实现互斥

一.单标志法轮流交替使用。缺点:当有一个进程不再进入临界区,便不能修改公共变量的turn,来标识另外一个进程可以进入临界区。因此另外一个进程将永久不能进入临界区,违背“空则让进”的原则A进程while (turn != A);critical section 临界区turn = Bremainder section 剩余区B进程while (turn != B);critical sectionturn = Aremainder section二.双标志先检查法为了解决在单标

2020-09-12 19:58:08 818

原创 facebook-雪花算法-分布式ID-C++实现

#include <stdint.h>#include <sys/time.h>#include <stdexcept>#include <mutex>#include <thread>#include "lightlog.h"class SnowFlake {private: static const uint64_t m_start_time_stamp = 1480166465631; // 初始时间戳 给一个随机值 st

2022-02-01 18:16:19 1096

原创 C++实现-带有颜色输出的简单日志类

#ifndef _LIGHT_LOG_H#define _LIGHT_LOG_H#include <iostream>#include <ostream>#include <fstream>#include <sstream>#include <string>#include <memory>#include <mutex>#include <cstdlib>namespace llog

2022-02-01 18:13:45 1178 1

原创 C++多态实现

#include <iostream>#include <functional>using namespace std;using EatPtr = function<void()>;using PlayPtr = function<void()>;struct VirtualTable { EatPtr m_eat; PlayPtr m_play;};struct Base { VirtualTable m_table; int

2022-01-30 11:48:54 827

原创 西安交通大学915-2015-编程4

题目描述:解题思路:设置计数器遍历链表代码实现:#include <iostream>#include <algorithm>using namespace std;struct Lnode { int data; Lnode* next; Lnode() {} Lnode(int data, Lnode* next) : data(data), next(next) {}};int caortx(Lnode* HL,

2021-12-16 07:52:40 1486

原创 西安交通大学915-2015-编程3

题目描述:解题思路:候选人类,设置计数器设置候选人数组代码实现:#include <iostream>#include <algorithm>using namespace std;struct Candidate { string name; int cnt;};const int N = 100;Candidate arr[N];int main() { int n, m; cout << "ple

2021-12-16 07:46:17 799

原创 西安交通大学915-2015-编程2

题目描述:解题思路:使用map统计遍历一次字符串代码实现:#include <string>#include <map>#include <iostream>using namespace std;void Count(const string& s) { map<char, int> mp; for (auto c : s) { mp[c]++; } for (auto k:

2021-12-14 22:38:45 884

原创 西安交通大学915-2015-编程1

题目描述:解题思路:矩阵为n阶主对角线 mat[i][i]副对角线 i + j == n - 1代码实现:#include <iostream>using namespace std;const int N = 3;void sum(int mat[N][N], int row, int col) { int dg = 0; int udg = 0; for (int i = 0; i < row; i++) {

2021-12-13 11:02:04 867

原创 西安交通大学915-2016-编程3

题目描述:代码实现:#include <iostream>using namespace std;enum STATUS { NORMAL = 0, // 正常 LATE = 1, // 迟到 LEAVE = 2, // 请假 ABSENT = 3 // 旷课};struct Student { int id; int course[4]; int sum;};const int N = 110;S

2021-12-12 14:06:05 216

原创 西安交通大学915-2016-编程2

题目描述:解题思路:数字转化为字符串 itoa用字符串处理代码实现:#include <iostream>#include <cstring>#include <cstdlib>using namespace std;#define N 128int Count(int m, int n) { char num[N] = {0}; itoa(m, num, 10); int cnt = 0; int i = 0

2021-12-12 13:26:36 218

原创 西安交通大学915-2016-编程1

题目描述:解题思路:遍历一次序列 时间复杂度O(n)代码实现:#include <iostream>#include <cstring>using namespace std;void RemoveDigitInStr(char *s) { int n = strlen(s); int i = 0; int j = 0; while (s[j] != '\0') { if (!isdigit(s[j])) {

2021-12-12 13:18:06 328

原创 西安交通大学915-2017-编程4

题目描述:解题思路:如果m >= n,机器的数量更多,每台机器给分配一个作业即可,不用设计等待时间如果m < n,作业的数量更多,优先选择长时间任务执行,短任务等待时间少代码实现:#include <iostream>#include <algorithm>#include <cstdio>using namespace std;const int N = 100;int target[N]; // 存储完成任务所需要的时间int m

2021-12-11 20:19:22 129

原创 西安交通大学915-2017-编程3

题目描述:设有两个栈s1, s2都采用顺序存储方式,并且共享一个存储区域[0, … , maxsize - 1],为了尽量使用空间,减少溢出的可能,可采用栈顶相向,迎面增长的存储方式,请设计s1, s2的出栈,进栈操作算法。要求:采用空间策略;算法数据结构;写出算法步骤解题思路:分为左栈和右栈,左栈栈顶指针初始为-1,右栈栈顶指针初始为maxsize共享栈满的条件:top1 + 1 == top2左栈空的条件:top1 == -1右栈空的条件:top2 == maxsize代码实现:#

2021-12-11 19:30:52 622

原创 西安交通大学915-2017-编程1

题目描述:解题思路:[1, 2, 3, 4, 5, 6] n = 6 k = 1倒数第一个,是正数的第六个倒数第k个,是正数的第n - k + 1个代码实现:#include <iostream>using namespace std;struct ListNode { ListNode* next; int data;};bool FindReverseKthInLinkedList(ListNode* head, int k) { aut

2021-12-11 18:57:51 154

原创 西安交通大学915-2017-编程2

题目描述:解题思路:二分查找模板注意边界 l <= r注意整型加法溢出,改用减法代码实现:package mainimport "fmt"func BinSearch(a []int, x int) int { l := 0 r := len(a) - 1 for l <= r { m := (r-l)/2 + l if a[m] == x { return m } else if a[m] < x { l = m + 1 } e

2021-12-11 18:47:25 205

原创 西安交通大学915-2018-编程2

题目描述:解题思路:选择一种稳定的排序算法,针对成绩关键字进行排序抽象学生类代码实现:#include <iostream>#include <map>#include <string>#include <algorithm>using namespace std;struct Student { std::string name; int score;};namespace lsc { void sw

2021-12-11 09:26:18 148

原创 西安交通大学915-2018-编程4

题目描述:解题思路:球盒模型dp,状态表示,f[n][m]表示在n个箱子里放入m个小球放置方法的数量状态计算,m < n, 即球数小于箱子数,f[n][m] = f[n][n]m >= n, 即球数大于箱子数,分为两种箱子满,箱子不满 f[n][m] = f[n-m][m] + f[n-1][m]箱子满 f[n-m][m]至少有一个箱子不满f[n-1][m]代码实现:#include <iostream>using namespace std;i

2021-12-11 09:12:37 105

原创 西安交通大学915-2018-编程3

题目描述:解题思路:判断闰年和平年计数代码实现:#include <iostream>using namespace std;bool IsLeapYear(int x) { return ((x % 400 == 0) || (x % 4 == 0 && x % 100 != 0));}int month[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},

2021-12-11 08:36:31 140

原创 西安交通大学915-2018-编程1

题目描述:解题思路:遍历序列,使用map存储代码实现:#include <iostream>#include <map>#include <string>using namespace std;void RemoveSpace(string &s) { string ans = ""; for (auto & c : s) { if (c != ' ') { ans += c;

2021-12-11 01:03:19 97

原创 西安交通大学915-2019-编程3

题目描述:解题思路:暴力枚举,时间复杂度O(n^2)归并排序,merge函数的拓展,一次处理一个区间的逆序对数量,时间复杂度O(nlogn)代码实现:暴力枚举#include <iostream>#include <vector>using namespace std;int main() { vector<int> arr = {1, 3, 2, 5, 4, 7, 6}; int n = arr.size(); in

2021-12-11 00:06:26 479

原创 西安交通大学915-2019-编程2

题目描述:解题思路:包含空格,首先去除空格利用回文的定义,遍历一次代码实现:#include <iostream>#include <stack>#include <vector>#include <string>#include <map>#include <unordered_map>using namespace std;bool RecursionSolution(const string &a

2021-12-10 23:45:38 281

原创 西安交通大学915-2019-编程1

题目描述:解题思路:存储 + 遍历更新代码实现:#include <iostream>using namespace std;struct Pair { int x; int y;};const int N = 100;Pair arr[N];int idx = 0;namespace lsc { void min(Pair& mi, Pair& x) { if (x.x < mi.x || x.y &l

2021-12-10 23:12:12 234

原创 西安交通大学915-2020-编程3

题目描述:代码实现:// 直接爆搜#include <iostream>#include <vector>using namespace std;int cnt = 0;void dfs(int target, vector<int>& value, int u) { if (!target) { cnt++; return; } for (int i = u; i < value.s

2021-12-02 23:24:45 196

原创 西安交通大学915-2020-编程2

题目描述:代码实现:#include <stdio.h>#include <stdlib.h>#include <string.h>void swap(char*s, int i, int j) { char c = s[i]; s[i] = s[j]; s[j] = c;}void reverse(char* s, int i, int j) { while (i < j) { swap(s, i

2021-12-02 23:10:13 244

原创 西安交通大学915-2020-编程1

题目描述:代码实现:#include <iostream>using namespace std;int main() { for (int i = 0; i <= 9999; i++) { int a = i / 1000; int b = i % 1000 / 100; int c = i % 100 / 10; int d = i % 10; int t = c * 1000 +

2021-12-02 22:54:53 181

原创 37-杨辉三角

思路:本质是递推,可以动态规划做对于杨辉三角的任意一行,第一个和最后一个元素都是1中间的元素是由其肩膀上两个元素之和。代码实现:#include <stdio.h>#include <stdlib.h>#define N 10int* f[N];void Generate() { for (int i = 0; i < N; i++) { int* line = malloc(sizeof(int) * (i + 1));

2021-12-02 15:48:41 472

原创 31-三色旗问题(荷兰国旗问题)

思路:使用三个指针维护三个区间代码实现:#include <stdio.h>typedef enum { BLUE = 1, WHITE = 2, RED = 3} Color;Color arr[] = {RED, BLUE, RED, WHITE, WHITE, RED, WHITE, BLUE, BLUE, RED, WHITE};void Swap(Color* arr, int i, int j) { Color t = arr[

2021-12-02 15:30:14 1042

原创 西安交通大学915-2021-编程3

题目描述:代码实现:#include <iostream>#include <cstring>using namespace std;const int N = 110;int f[N]; // 状态表示 : f[i]表示第i天机器人的总数// 状态计算 : 第i天的机器人数量 = 第i-1天的机器人数量 + 第i-3天的机器人数量// 因为第i-3天的机器人数量到今天已经称为一个成熟的机器人,可以产生一个新的机器人// 状态转移方程 f[i] = f[i

2021-12-01 23:14:24 452

原创 西安交通大学915-2021-编程2

题目描述:代码实现:#include <iostream>#include <vector>#include <algorithm>using namespace std;// 获取一个数字的所有因子vector<int> GetDivisors(int x) { vector<int> ans; for (int i = 1; i < x; i++) { if (x % i == 0) {

2021-12-01 23:05:29 112

原创 西安交通大学915-2021-编程1

题目描述:代码:#include <iostream>#include <vector>using namespace std;int FindRowMinValue(vector<vector<int>> &g, int row) { int res = INT_MAX; for (int i = 0; i < g[row].size(); i++) { res = min(res, g[row]

2021-12-01 22:43:06 344

原创 29-约瑟夫环问题

代码#include <stdio.h>/*编号为 1,2,3,…,n 的 n 个人围坐一圈,任选一个正整数 m 作为报数上限值,从第一个人开始按顺时针方向报数,报数到 m 时停止,报数为 m 的人出列。从出列人的顺时针方向的下一个人开始又从 1 重新报数,如此下去,直到所有人都全部出列为止。*/int LastRemain(int n, int m) { if (n == 1) { return 0; } return (LastRe

2021-11-30 06:45:08 116

原创 30-整数逆序输出

思路递归体 先递归,后输出边界条件代码#include <stdio.h>/* * 将一个从键盘输入的整数存放到一个数组中,通过程序的运行按照数组中的逆序输出该整数,利用递归的方法解决问题。 * */void Print(int* a, int n, int u) { if (u == n) { return; } Print(a, n, u + 1); printf("%d", a[u]); if (u ==

2021-11-28 21:34:58 685

原创 28-输出等腰三角形

思路每行前输出空格的数量每行空格后*的数量每行后注意输出换行符代码#include <stdio.h>/*本实例要求从键盘输入任意整数 n,通过程序运行输出对应高度为 n 的等腰三角形 */void PrintTriangle(int n) { // 输出几行 for (int i = 0; i < n; i++) { // 输出每行前的空格 for (int j = 0; j < n - i -1; j++)

2021-11-28 09:02:48 513

原创 27-字符串加密和解密算法

思路根据题意,思路是比较好想的,但是要注意以下两点加密时,要注意偏移之后的值(下标 + 5)不能超能ascii码所能表示的范围。解秘时,要注意偏移之后的值(下标 - 5)不能小于0,所以需要加上N然后取模。代码#include <stdio.h>#include <string.h>/* * 在本实例中要求设计一个加密和解密算法。 * 在对一个指定的字符串加密之后,利用解密函数能够对密文解密,显示明文信息。 * 加密的方式是将字符串中每个字符加上它在字符串中

2021-11-28 08:28:45 1710

原创 DP-LeetCode-152. 乘积最大子数组

思路数组中有负有正有0状态表示f[i] : 表示从[0, i]数组乘积的最大值g[i] : 表示从[0, i]数组乘积的最小值状态计算通过i-1的状态递推i对于f[i]f[i]只考虑自己,则f[i] = nums[i]nums[i] > 0 f[i] = f[i - 1] * nums[i]nums[i] < 0 f[i] = g[i - 1] * nums[i]nums[i] == 0综上f[i]是上面几种情况的最大值对于g[i]g[i]只考虑自己,则g[i]

2021-11-27 09:00:16 104

原创 26-统计单词个数

思路在字符串中一个完整单词的特征?空格前且空格前是字符换行符前且空格前是字符代码#include <stdio.h>#include <string.h>#include <ctype.h>int count(char* s) { int n = strlen(s); int i = 0; int cnt = 0; for (i = 0; i < n; i++) { if (isalpha(s[

2021-11-27 08:01:53 191

原创 B-树,B+树,B*树

文件索引系统中应用why?数据量非常大-》在磁盘中存储-》会给数据创建索引-》给数据进行排序,加速搜索需要解决两个问题?1.减少磁盘的IO2.更快的搜索算法操作系统中,管理内存是按照页page 4K 管理的管理磁盘是按照block 16K现在有n = 1000w个索引需要从磁盘中进行读取和搜索?avl树和m为300的B-树?avl树的高度:log2n = 24层最差的情况一个节点只存储一个索引?最差需要24次磁盘IOB-树高度:log(300)n = 3 层最多花费3次磁盘IO

2021-11-26 23:05:03 241

原创 DP-LeetCode-45. 跳跃游戏 II

class Solution {public: int jump(vector<int>& nums) { // 起跳点的范围[st, ed) int st = 0; // int ed = 1; int ans = 0; while (ed < nums.size()) { int maxPos = 0; // 更新最远的跳跃距离

2021-11-26 19:16:04 181

原创 DP-LeetCode-918. 环形子数组的最大和

思路最大子数组的满足的条件[x x x x (正x x x x x x x x x 正)x x x x ],两边一定是正数,如果至少有一个负数,会是的整个子数组和变小。[x x x x 负(正x x x x x x x x x 正)负 x x x x ],子数组两个边界外的数字一定是负数,如果是整数,一定会被扩充到子数组中,使得子数组和变的更大,是0不影响环形数组一定包括两个边界,所以它的形式是[x x x x 正(负x x x x x x x x x 负)正 x x x x ],其中(负x

2021-11-26 08:37:08 226

原创 DP-LeetCode-3. 最大子序和

思路代码状态表示当前位置为i,last表示[0, i-1]的最大子序和now表示[0, i]的最大子序和,如果想让当前的now更大,那么last需要>=0状态计算now = max(0, last) + nums[i]状态计算class Solution {public: int maxSubArray(vector<int>& nums) { int last = 0; int ans = INT_MIN;

2021-11-25 23:24:18 304

八大排序C++代码实现

冒泡排序 选择排序 插入排序 基数排序 希尔排序 归并排序 堆排序 快速排序的C++的代码实现,可以作为模板使用,是互联网企业面试的必备技能

2020-11-14

常用的数据结构树算法代码总结

本文档总结了常用的数据结构中的树算法代码.包含常见的遍历算法(递归与非递归等),dfs,bfs的实现以及其广泛的应用

2020-01-01

LinkedListProblems.pdf

斯坦福大学整理的18个链表的相关问题,包括各类型的链表的问题。对于面试算法有很大的帮助,唯一的不足是pdf文档为全英文,需要一定的英语阅读能力。

2019-10-29

Effective C++中文第三版高清

C++经典必读书籍之一,整本书的知识点全面细致,每一个mudule都有理有据,常常会触发自己思考一些没有想过的问题。C++的功能多,实现复杂,只是学习语法只会纸上谈兵,而《Effective C++》去理解C++程序的设计原理、应用方法、陷阱可以.加深了对计算机,编程的理解和认识。

2018-08-02

空空如也

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

TA关注的人

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