自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(126)
  • 资源 (2)
  • 收藏
  • 关注

原创 创建一个Angular项目

创建一个Angular项目工具:VS Code 或者cmd1.找一个目录,存angular工程文件的目录;2.在终端输入cd 目录路径 ,进入目录;3.输入命令ng new 文件名;4.接下来选择是否需要添加路由,样式;5.输入命令 npm install,等在下载完成;6.然后用VS Code 打开项目;7.找到package.json文件,进入后,查看scripts下的start命令,然后执行该命令,启动项目。8.打开浏览器,输入http://localhost:4200,即可看到运

2021-09-10 23:25:16 620

原创 C++回溯求数组所有子集

#include<iostream>#include<vector>using namespace std;vector<vector<int>> vec;void backtrace(vector<int>& nums,vector<int>& path, int pos){ vec.push_back(path); for(int i=pos;i<nums.size();i++) { .

2020-09-02 21:06:59 1135 1

原创 C++实现字符串匹配strstr()函数

//函数接口:char* mystrstr(char* str1,char* str2)//解释://在字符串str1中查找第一次出现str2的位置//如果找到匹配的字符串,返回第一次匹配字符串的指针//否则,返回NULL#include<isotream>using namespace std;char* mystrstr(char* str1,char* str2){ char* src,*sub; if(str1==NULL || str2==NULL) {

2020-08-19 10:57:15 1014

原创 向循环有序链表插入一个节点的C++实现

//向循环有序链表插入一个值//……struct ListNode{ int val; ListNode* next; ListNode(int x):val(x),next(NULL){}};ListNode* insertNodeToList(ListNode* LNode,ListNode* pNode){ //1.循环链表为空,新插入节点,首尾相连 if(LNode==NULL) { pNode->next=pNode; return PNode; }

2020-08-19 09:06:05 708

原创 C++实现全排列

//把数组分为两部分//一部分为已排列,另一部分为未排列//将未排列部分元素插入已排列部分//回溯,找出所有可能的答案//……void backtrace(vector<vector<int>>& ans,vector<int>& vec,int first, int len){ if(first==len) { ans.push_back(vec); return; } for(int i=first;i<len;i.

2020-08-19 08:29:38 291

原创 向单向有序链表中插入节点C++代码实现

//向单向有序链表中插入一个节点,保证其有序性不变struct ListNode{ int val; ListNode* next; ListNode(int x):val(x),next(NULL){}};ListNode* insertToList(ListNode* head,ListNode* pNode){ if(head==NULL || pNode==NULL) return NULL; ListNode* dumm=new ListNode(); dumm-&gt

2020-08-18 18:50:38 1199

原创 打印100以内的素数(质数)C++实现

质数:只能被1和它本身整除的数。#include<iostream>using namespace std;int main(){ int n; cin>>n; for(int i=2;i<=n;i++) { for(int j=2;j<=i;j++) { if(i%j==0 && i!=j) break; if(i%j==0 && i==j) cout<<i<<endl;

2020-08-18 16:15:36 3707

原创 C++实现LeetCode有序连边转换二叉搜索树

//由题意得,高度平衡的二叉搜索树左右子树高度差不超过1.//因此上,让根节点保证在链表序列的中间位置,此时左右子树得高度差不超过1.//采用递归的方式.struct ListNode{ int val; ListNode* next; ListNode(int x):val(x),next(NULL){}};struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int x):val(x),l.

2020-08-18 08:58:46 108

原创 LeetCode:最长连续序列C++实现

//由于时间复杂度要求,因此不能用常见排序算法去处理//借助set的性质,去重和排序class Solution{public: int longestSequence(vector<int>& vec) { set<int> s; for(int elem:vec) s.insert(elem); //遍历 int longest=0; int cnt=1; for(auto e:s) { if(s.find(e+1)!=.

2020-08-17 20:05:22 207

原创 剑指Offer:变态跳台阶C++实现

//可以从前面的任何一层跳到当前层//因此是前n-1层跳法的总和………………………………int jumpFloor(int n){ vector<int> step(n+1,0); step[0]=step[1]=1; for(int i=2;i<=n;i++) { for(int j=0;j<i;j++) { step[i]+=step[j]; } } return step.back();}...

2020-08-17 15:30:33 101

原创 C++实现strcpy

将str2指向的字符串拷贝到str1中去,并返回指向字符串str1的指针。//首先要保证str1有一段可以容纳str2字符串的内存空间,及str1的空间足够大,最少可以存放str2(包括末尾结束符).//将str2中的字符拷贝到str1中的对应位置,采用一个循环来实现//循环结束标志为str2遇到'\0';#include<iostream>#include<string>using namespace std;char *mystrcpy(char* str1,

2020-08-17 09:21:53 900

原创 每日一题:平衡二叉树

//本题的关键在于“每个节点”的左右两个子树高度差的绝对值为1.//因此,要判断“每个节点”的左右子树.struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int x):val(x),left(NULL),right(NULL){}};class Solution{public: //定义一个求树高度的方法 //递归 int treeHeight(TreeNode* root) { i.

2020-08-17 08:35:10 89

原创 C++实现字符串比较strcmp

如果字符串str1大于str2,则返回1,如果字符串str1小于str2,则返回-1,如果字符串str1等于str2,则返回0。#include<iostream>#include<string>using namespace std;int mystrcmp(const char* str1,const char* str2){ if(*str1==NULL || *str2==NULL) { cout<<"INPUT ERROR!"<&lt

2020-08-16 17:07:12 938

原创 C++实现从尾到头打印链表(剑指Offer)

struct ListNode{ int val; ListNode* next; ListNode(int x):val(x),next(nullptr){}};class Solution{public: vector<int> printListArray(TreeNode* head) { vector<int> printList; while(head) { printList.push_back(head->val);

2020-08-16 15:47:44 95

原创 合唱队队形(最长上升/下降子序列、动态规划)

//最长上升子序列,动态规划//先找出每一位置i的左侧的最长上升子序列的长度center_left[i],每一位置i的最长上升子序列的长度的取值为其左侧比它小的所有位置的最长上升子序列长度中的最大值+1;//再找出每一位置i的右侧的最长下降子序列的长度center_right[i],每一位置i的最长下降子序列的长度的取值为其右侧比他小的所有位置的最长下降子序列长度中的最大值+1;//最后求出每个位置的满足要求的最长序列长度为center_left[i]+center_right[i]-1(i位置多算.

2020-08-16 15:35:27 192

原创 坐标计算

#include<iostream>#include<string>using namespace std;int main(){ string str; while(cin>>str) { pair<int,int> point(0,0); int len=str.size(); int start=0; size_t found=str.find_first_of(';'); while(found!=string:.

2020-08-15 09:12:29 184

原创 C++实现字符串排序

#include<iostream>#include<string>#include<vector>#include<ctype.h>#include<algorithm>using namespace std;bool charSort(char& chr1, char& chr2){ char ch1=tolower(chr1); char ch2=tolower(chr2); return ch1&g.

2020-08-14 19:35:50 6945

原创 用定长数组实现队列C++

#include<iostream>#define SIZE 100;using namespace std;class Queue{private: int arr[SIZE]; int head; int tail;public: Queue() { head=0; tail=0; } bool isFull() { return (tail+1)% SIZE==head; } bool isEmpty() { return head==t

2020-08-13 09:21:18 449

原创 C++设计模式之观察者模式(Observer pattern)

观察者模式:为对象建立一种“通知依赖”关系,当一个对象(目标对象)的状态发生变化时,所有的依赖对象(观察者对象)都得到通知并自动更新。观察者模式,又称发布-订阅模式,其所作的工作就是在解除耦合,弱化依赖关系,采用面向对象技术,让耦合的双方都依赖于抽象,而不是依赖于具体,将这二者封装在独立的对象中以使它们可以独立的改变和复用。观察者模式中的四类角色:1.Subject(目标):抽象的目标类,提供增加和删除观察者对象的的方法,以及一个抽象的Notify方法(通知机制),目标类可以是接口,也可以是抽象类和

2020-08-07 16:04:36 124

原创 C++设计模式之策略模式(Strategy pattern)

策略模式概念:某些对象使用的算法可能多种多样,经常改变(如不同国家的税法征收策略不同),根据需要透明的更改对象的算法,将算法本身与对象解耦,节省对象的开销将各种不同的方法分别封装起来,让他们可以相互替换,策略模式是一种定义一系列算法的方法,Strategy类层次为Context定义了一系列的可重用的算法或行为, 所有的算法以相同的方式进行调用,减少了算法类之间的耦合//策略模式实现#include<iostream> using namespace std;//策略基类cl

2020-08-07 09:53:04 362

原创 C++设计模式之模板模式(template pattern)

模板模式概念:整体结构是稳定的,而子步骤中有很多改变的需求。将子步骤(变化的部分)通过虚函数机制延迟到子类中实现,稳定的部分通过调用变化的部分(晚绑定)来实现。不要调用我,让我来调用你//模板模式实现#include<iostream>using namespace std;class Template{public: Template(){};//构造函数 virtual ~Template(){};//虚析构 //变化的子步骤 virtual void ste

2020-08-07 08:58:03 108

原创 寻找二叉树中的首个共同祖先C++实现

//说明://1.所有的节点值都是唯一的//2.p,q为不同节点且均存在于二叉树中struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int x):val(x),left(NULL),right(NULL){}};class solution{ TreeNode* lowestCommonAncestor(TreeNode* root,int p,int q) { //递归 if(!r.

2020-08-07 08:28:45 204

原创 剑指offer:重建二叉树的C++实现

struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int x):val(x),left(NULL),right(NULL){}};TreeNode* built(vector<int>& preorder,vector<int>& inorder){ if(preorder.size()==0 || inorder.size()==0 || preorder.s.

2020-08-06 09:07:59 113

原创 LFU算法的C++实现

LFU算法:(Least Frequently Used),对每一个数据的访问频次作一记录,数据的访问次数越多,节点访问次数越大。*新插入的数据访问频次为1;*相同频次的数据按时间排序,后插入的在前面;*当需要淘汰数据时,会从尾部开始淘汰,即访问频次从小到大...

2020-08-04 14:49:05 1431

原创 c++实现归并排序merge_sort

//归并排序算法://1.拆分//2.合并//与快排算法的不同点://1.归并排序不是求子问题的解,而在于合并子问题的解//2.归并需要借助额外的内存空间//3.归并时间复杂度最好最坏均是O(NlogN)#include<iostream>#include<vector>using namespace std;void merge_sort(vector<int>& vec,int begin,int end);//拆分void mer

2020-08-04 09:24:12 183

原创 剑指offer:二维数组的查找

#include<iostream>#include<vector>using namespace std;bool searchValue(vector<vector<int>>& matrix,int target){ if(matrix.empty()) return false; int row=matrix.size()-1; int col=matrix[0].size()-1; int h=0; while(h&l.

2020-08-03 14:26:13 73

原创 c++将二叉树展开为单链表(LeetCode每日一题)

//前序遍历struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode():val(0),left(NULL),right(NULL){} TreeNode(int x):val(x),left(NULL),right(NULL){} TreeNode(int x,TreeNode* left,TreeNode* right):val(x),left(left),right(right){}};class s.

2020-08-02 07:59:42 136

原创 c++实现选择排序Select_sort

#include<iostream>#include<vector>#include<algorithm>using namespace std;void select_sort(vector<int>& nums);int main(){ vector<int> ans={0,3,5,2,9,8,6,4,7,1}; select_sort(ans); for(int elem:ans) cout<<el

2020-08-01 20:52:09 188

原创 c++实现QuickSort算法

#include<iostream>#include<vector>#include<algorithm>using namespace std;void QuickSort(vector<int>& nums,int begin,int end){ if(begin>=end) return; int mid=begin+(end-begin)/2;//增加随机性 swap(nums[mid],nums[end]);

2020-07-31 19:22:55 331

原创 c++判断子序列

#include<iostream>#include<string>using namespace std;int main(){ string s="abc"; string t="ahbgdc"; int s_len=s.size(); int t_len=t.size(); if(s_len>t_len) return false; int i=0,j=0,cnt=0; while(i<s_len && j<t.

2020-07-27 08:43:34 935

原创 c++实现LRU(最近未使用)缓存算法

LRU(Least Recently Used Algorithm)LRU算法的高效执行(插入、查找、删除)。插入、删除(链表)查找(哈希表)因此采用哈希表和链表相结合的方式实现LRU算法。#include<iostream>#include<unordered_map>//包含include<list>using namespace std;class cacheLRU{private: struct Node { int key;

2020-07-26 08:53:16 136

原创 LeetCode 86题:分割链表的C++代码

给定一个链表和一个特定的值x,对链表进行分割,使得所有小于x的节点都在大于或等于x的节点之前,且新的链表应保留两个分区中节点的原始相对位置不变。示例:输入: head = 1->4->3->2->5->2, x = 3输出: 1->2->2->4->3->5//双链表//遍历原始链表//将小的节点放在一个链表中,大于等于的节点放在另外一个节点中//将两个链表拼接struct ListNode{ int val; ListNo

2020-07-22 09:59:33 148

原创 c++排序算法之希尔排序

希尔排序的提出原因:希尔排序是对插入排序的优化。假设在插入排序中,有个最小元素位于排序序列最末尾,那么在排序过程中需要将该最小值一步一步地移动到序列的第一个位置,此时复杂度太高O(n^2),为了避免该类情况发生,给其设置了一个增量让其大步移动,优化其效率,此即为希尔排序。希尔排序的基本思想:先取小于n的整数d1作为第一个增量,把全部记录分组。所有距离为d1倍数的记录放在同一组中,先在各组内进行直接插入排序;然后,去第二个增量d2<d1重复上述的分组和排序,直到所取的增量dt=1(dt<dt

2020-07-21 15:01:52 142

原创 leetcode1035题:不相交的线

思路:最长公共子序列、动态规划#include<iostream>#include<vector>#include<algorithm>using namespace std;int unintersect_line(vector<int>& nums1,vector<int>& nums2){ int len1=nums1.size(); int len2=nums2.size(); vector<vec

2020-07-21 09:45:45 216

原创 C++排序算法之插入排序

原理:前部分有序,后部分无序,每次将无须部分的第一个数字插入到有序部分的合适位置,有序部分长度加1,无序部分长度减1。时间复杂度O(n^2),空间复杂度O(1),稳定排序。#include<iostream>#include<vector>#include<algorithm>using namespace std;void insert_sort(vector<int>& nums){ int len=nums.size();

2020-07-20 10:03:09 127

原创 c++排序算法之冒泡排序

C++排序算法之冒泡排序:思想:每次遍历,将相邻元素两两比较,前者小,后者大,直至将最大的元素放置在数组尾部,数组长度减1,即未排序部分的长度。稳定排序,时间复杂度O(n^2),空间复杂度O(1)。#include<iostream>#include<vector>#include<algorithm>using namespace std;void bubble_sort(vector<int>& nums){ int len=

2020-07-19 21:30:23 150

原创 求两个数的最大公约数/最小公倍数

正整数A,B的最小公倍数是指能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数。#include<iostream>using namespace std;int main(){ //最小公倍数=两数之积/最大公约数; int A,B,p; cin>>A>>B; p=A*B; if(A<B) swap(A,B) while(B!=0) { int temp; temp=A%B; A=B; B=temp;

2020-06-27 10:04:31 174

原创 不使用库函数,求一个数字的立方根

不使用库函数,求一个数字的立方根,保留小数点后一位。//二分法#include<iostream>#include<iomanip>using namesapce std;double getCubeRoot(double x){ if(x==-1 || x==0 || x==1) return x; int flag=0; double mid; double low=1.0; double high=x; if(x<0) { flag=-

2020-06-22 09:44:41 519

原创 C++矩阵之岛屿的最大面积

给定一个包含0和1的非空二维数组grid,一个岛屿是由一些相邻的1(代表陆地)构成的组合,这里的相邻要求两个1必须在水平或者竖直方向上相邻,假设grid的四个边缘都被0(代表水)包围着。 找到grid中岛屿的最大面积,如果没有岛屿,则返回面积0。//深度优先搜索int dfs(vector<vector<int>>& grid,int cur_i,int cur_j){ if(cur_i<0 || cur_j<0 || cur_i>grid.siz

2020-06-16 20:11:34 917

原创 字符串数组的最长公共前缀

给定一个字符串数组,求改数组字符串的最长公共前缀,如果不存在最长公共前缀,返回空字符串“”;示例:输入:[“flower”,“flow”,“flight”]输出:“fl”//substr(pos,len); pos起始位置、len长度string LongestCommonPrefix(vector<string>& strs){ if(strs.empty()) return ""; string ans=strs[0]; int len=strs[0].size(

2020-06-15 19:36:24 324

代码随想录知识星球精华-大厂面试八股文v1.1.pdf

代码随想录知识星球精华-大厂面试八股文v1.1.pdf

2022-04-11

二叉树的四种种遍历方式.docx

用C++实现二叉树的前序遍历、中序遍历、后续遍历、层次遍历。分别采用递归的方法和迭代的方法,对入门级数据结构和算法学习大有裨益。不同的方法相互比对,更容易加深理解。

2020-08-18

空空如也

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

TA关注的人

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