![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
每天练点小程序
月已满西楼
某不知名学校毕业学生,某不知名名企软件工程师,擅长C++
python,Mysql
展开
-
模拟实现库函数strstr
模拟实现库函数strstr 代码如下#include<stdio.h>const char* mystrstr(const char*src, const char *needle){ for (int i = 0; src[i] != '\0'; i++) { int temp = i; int j = 0; while (src原创 2017-03-17 18:33:53 · 460 阅读 · 0 评论 -
合并有序链表 计算1加到n不用条件判断
合并两个有序链表,合并以后的链表依旧有序。/* 给定两个有序单链表的头节点 head1 和 head2,请合并两个有序链表,合并后的链表依然有序,并返回合并后链表的头节点。 例如: 0->2->3->7->NULL 1->3->5->7->9->NULL 合并后的链表为:0->1->2->3->3->5->7->7->9->NULL*/非递归法:后续更新递归法void Merge(Li原创 2017-07-14 22:57:52 · 643 阅读 · 0 评论 -
代码片段day13-day15
1. 判断是否是树的子树#include<iostream>#include<cassert>using namespace std;struct BinaryTreeNode{ struct BinaryTreeNode *_pLeft; struct BinaryTreeNode *_pRight; char _data; BinaryTreeNode(i原创 2017-07-15 13:55:46 · 215 阅读 · 0 评论 -
翻转链表查找链表的倒数第k个节点位运算加法
反转单链表void Reverse(ListNode *&pHead){ if (pHead == nullptr||pHead->_pNext==nullptr) return; ListNode *pCur = pHead; ListNode *pNewHead = nullptr; ListNode *pTemp = nullptr;原创 2017-07-16 21:15:17 · 293 阅读 · 0 评论 -
求带环链表入口点 计算时间复杂度
求带环链表入口点 计算时间复杂度ListNode *MeetNode(ListNode *&pHead){ if (pHead == nullptr&&pHead->_pNext==nullptr) return nullptr; ListNode *pSlow = pHead->_pNext; ListNode *pFast; if (pSlow-原创 2017-07-16 23:13:30 · 443 阅读 · 0 评论 -
使用两个栈实现一个队列+使用两个队列实现一个栈
1. 使用两个栈实现一个队列+使用两个队列实现一个栈两个栈实现一个队列#include<iostream>#include<stack>using namespace std;template<class T>class StackQueue{public: void Push(T data) { Spush.push(data); }原创 2017-07-20 13:18:01 · 264 阅读 · 0 评论 -
二叉树中叶子节点的个数第K层的节点个数
叶子节点的个数size_t _GetLeefNode(Node *pRoot){ if (pRoot == NULL) return 0; if (NULL == pRoot->pLeft&&NULL == pRoot->pRight) return 1; return _GetLeefNode(pRoot->pLeft) + _GetLee原创 2017-07-29 22:13:16 · 1068 阅读 · 0 评论 -
判断链表是否有交点若有找出交点
.判断两个链表是否相交,若相交,求交点。(假设链表不带环)ListNode *InstersecNode(ListNode *pHead1, ListNode *pHead2){ bool flag = IsIntersec(pHead1, pHead2); if (!flag) return nullptr; ListNode *pNode1 = pHea原创 2017-07-17 21:51:41 · 272 阅读 · 0 评论 -
day19-day21代码片段
1. 1-n中某数字出现的次数#include<iostream>using namespace std;int getNum(int n,int k){ int count = 0; int x; for (int i = 1; x=n/i; i *= 10) { int high = x / 10; if (k == 0)原创 2017-07-21 23:32:09 · 388 阅读 · 0 评论 -
判断是否是栈的弹出序列&&二进制中1的个数
判断是否是栈的弹出序列bool IsPop(char *PopOrder, char *PushOrder,int len){ if (PopOrder == nullptr || PushOrder == nullptr || len <= 0) return false; bool Possible = false; char *PopStart = P原创 2017-07-23 00:14:08 · 286 阅读 · 0 评论 -
二叉树的层序遍历 N!结尾0的个数
二叉树的层序遍历void _LevelOrder(Node *pRoot) { Node *pCur = pRoot; queue<Node *> q; q.push(pCur); while (!q.empty()) { Node *pTop = q.front();原创 2017-07-24 00:09:44 · 292 阅读 · 0 评论 -
判断输入的矩阵是否是单位矩阵
判断输入的矩阵是否是单位矩阵#include<iostream>using namespace std;#define N 3bool judge(int(*arr)[10]){ for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (i == j原创 2017-03-21 23:15:51 · 2163 阅读 · 0 评论 -
超级标准形式打印杨辉三角
用非常标准的三角形打印杨辉三角,代码如下:#include<stdio.h>#define N 10int main(){ int arr[N][N] = { 0 }; for (int i = 0; i < N; i++) { for (int j = 0; j <N; j++) { if (j == 0 ||原创 2017-03-21 23:14:26 · 752 阅读 · 0 评论 -
多态&指针访问虚函数&不能被继承的类&快速排序&N皇后问题&插入排序&堆排序&merge归并排序&栈上生成对象&两个栈实现一个队列
多态/* 1. 要想实现覆盖(重写)父类必须声明为virtual,子类可以不声明为virtual.-->FunB() 2. 派生类重写基类的虚函数实现多态,要求函数名、参数列表、返回值完全相同。(协变除外) 3. 基类中定义了虚函数,在派生类中该函数始终保持虚函数的特性。 4. 只有类的非静态成员函数才能定义为虚函数,静态成员函数不能定义为虚函数。 5. 如果原创 2017-07-02 22:25:06 · 624 阅读 · 0 评论 -
Median of Two Sorted Arrays 两个有序数组的中位数 C++实现 java实现 leetcode系列(四)
两个有序数组的中位数 C++实现 java实现[LeetCode] 4. Median of Two Sorted Arrays 两个有序数组的中位数 There are twosorted arrays nums1 and nums2 of size m and n respectively.Find themedian of the two sorted arrays. The ove...原创 2019-05-30 22:32:06 · 182 阅读 · 0 评论 -
最长回文子串 C++实现 javas实现 (Longest Palindromic Substrig)leetcode系列(五)
最长回文子串C++实现 java实现Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example 1:Input: "babad"Output: "bab"Note: "aba" is also a val...原创 2019-05-30 22:49:50 · 177 阅读 · 0 评论 -
Z 字形变换C++实现(ZigZag Converesion)leetcode系列(六)
[LeetCode] ZigZag Converesion 之字型转换字符串The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better ...原创 2019-05-30 23:02:49 · 255 阅读 · 0 评论 -
翻转整数C++实现 (Reverse Integer)leetcode系列(七)
翻转整数Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3:Input: 120Output: 21Note:Assume we are deali...原创 2019-05-30 23:08:51 · 403 阅读 · 0 评论 -
代码片段10-12
1. 并查集/*-1 -1 -1 -1 -1 -1 -1 -1{1, 2} {2, 3} {4, 5} {1, 2}*/#include<iostream>using namespace std;class UnionSet{public: UnionSet(int n) :_size(n + 1) { _a = new int原创 2017-07-12 11:53:28 · 229 阅读 · 0 评论 -
最长递增子序列
问题描述 在数字序列A中,按下标递增的顺序选出一个子序列B,如果选出的子序列B是严格递增的,则该子序列B称为原数字序列A的递增子序列。最长递增子序列问题就是找到原数字序列A中最长的递增子序列。例如数字序列{ 2, 1, 5, 3, 6, 4, 8, 9, 7 }的一个最长递增子序列为13489解题思路: 动态规划函数为: L(i) = 1, i = 1或者不存在原创 2017-05-31 17:11:03 · 429 阅读 · 0 评论 -
删除字符串A中和所有和字符串B一样的内容
删除字符串A中和所有和字符串B一样的内容 代码如下: #include原创 2017-03-17 18:37:42 · 1175 阅读 · 0 评论 -
寻找数组中的单身狗(二)狗二的传说
有一个数组,数组中的元素都是成对出现的,其中有两只单身狗藏身其中,你能找出他们么?#includeusing namespace std;void find_singledog(int arr[], int len, int *num1, int *num2){ int temp = 0; int count = 1; for (int i = 0; i < len; i++) {原创 2017-03-16 17:45:14 · 681 阅读 · 0 评论 -
厄米多项式
厄米多项式/*Hermite Polynomials(厄米多项式)是这样定义的1 ,n <= 0Hn(x)= 2x ,n = 12xHn-1(x) - 2(n-1)Hn-2(x) ,n >= 2*/#include <stdio.h> int hermite(int n, int x){原创 2017-03-21 23:17:17 · 3520 阅读 · 0 评论 -
密码学大数乘法
大数是算法语言中的数据类型无法表示的数,其位数超过最大数据类型所能表示的范围,所以,在处理大数问题时首先要考虑的是怎样存储大数,然后是在这种存储方式下其处理的实现方法。一般情况下大数的存储是采用字符数组来存储,即将大数当作一个字符串来存储,而对其处理是按其处理规则在数组中模拟实现。 三 大数乘法。大数乘法,相对之前的加法和减法,难度有所提高,但是本质还是一样的。下面原创 2017-03-15 16:47:22 · 655 阅读 · 0 评论 -
判断输入的文字花括号是否匹配
#includeusing namespace std;int main(){ char c; int count = 0; while ((c = getchar()) != EOF) { if ('{' == c) { count++; } else if ('}' == c&&0!=count) { count--; } else if原创 2017-03-14 21:16:33 · 524 阅读 · 0 评论 -
打印输入的内容并添加行号
#includeusing namespace std;int main(){ char c; int i = 0; bool flag = true; while ((c = getchar()) != EOF) { if (flag) { cout << i; flag = false; } if ('\n' == c) { flag =原创 2017-03-14 21:21:12 · 399 阅读 · 0 评论 -
数字二进制位的奇偶位互换
#includeusing namespace std;int reverse(int n){ return ((n & 0x55555555) > 1);}int main(){ int n; cin >> n; int res=reverse(n); cout << res << endl; system("pause");}原创 2017-03-14 22:35:16 · 728 阅读 · 0 评论 -
计算2+22+222
#includeusing namespace std;int main(){ int sum = 0; int total = 0; int n; cin >> n; for (int i = 0; i < n; i++) { sum = sum * 10 + 2; total += sum; } cout << total << endl; system("pa原创 2017-03-14 22:36:17 · 971 阅读 · 0 评论 -
模拟实现Vector高级版
总结一下实现Vector时遇到的问题:1.delete[]的时候总是报错,应当第一时间想到delete[]和new[]的匹配问题,可能由于自己太过自信,没有往那方面想,以至于浪费了好长时间调试代码2.遇到错误应当现检查逻辑问题,然后再调试,不要浪费时间在寻找错误上,用调试技巧寻找错误会事半功倍。3.关于数组下标问题,数组下标总是比数组大小少一个学到的知识:1.const &是专原创 2017-03-25 09:33:30 · 332 阅读 · 0 评论 -
模拟String类
模拟String类#includeusing namespace std;class String{public: String(char *data="") { if (*data == '\0') { _data = new char[1]; *_data = '\0'; } else { _data = new char[strlen(d原创 2017-03-26 12:30:20 · 413 阅读 · 0 评论 -
模拟实现strcpy
#include#include#includechar *mystrcpy(char *des, const char *src)//加const保证源字符串不被改写{ assert(des != NULL);// assert(src != NULL); if (src == des) { return des; } char *s = des; while ((*d原创 2017-03-16 18:48:25 · 283 阅读 · 0 评论 -
计算一个数字的二进制中1的个数
计算一个数字的二进制中1的个数 法一int main(){ int n; cin >> n; int count = 0; while (n) { if (n & 1 == 1) { count++; } n = (n >> 1); } cout <<原创 2017-03-16 18:52:57 · 344 阅读 · 0 评论 -
模拟实现strncpy与极简改进
模拟实现strncpy与极简改进 代码如下:#include<stdio.h>#include<string.h>#include<assert.h>char *mystrncpy(char *des, const char *src, size_t n){ assert(des != NULL); assert(src != NULL); size_t len =原创 2017-03-16 20:43:26 · 535 阅读 · 0 评论 -
寻找数组中的单身狗(一)狗一入侵
从前有一个这样的数组,数组中的元素在成双作对,他们都快乐的生活在数组的乐园里,时光就这样悠悠的,不紧不慢的流逝着,突然有一天一声怪异的犬吠声打破了原本宁静的数组乐园,数组乐园中多了一只单身狗,数组们面对单身狗的入侵毫无反抗之力,这时他们想到了找英勇无畏的程序猿帮忙,找出这只单身狗,干掉他!!! 给定一个数组如下 int arr[]={1,1,2,2,3,3,4,4,5,6,6,7,7};原创 2017-03-16 21:01:11 · 536 阅读 · 1 评论 -
两种方法位运算实现加法器
按照惯例先来一段代码: 先看feibo1#include<iostream>using namespace std;int feibo1(int n){ int res; int first = 1; int second = 1; if (n < 3) { return 1; } for(int i = 2; i <原创 2017-04-14 00:01:24 · 1362 阅读 · 0 评论 -
之字形打印矩阵
题目: 给定一个矩阵matrix,按照“之”字形的方式打印这个矩阵。例如: 1 2 3 4 5 6 7 8 9 10 11 12 “之”字形打印的结果为:1,2,5,9,6,3,4,7,10,11,8,12 要求额外的空间复杂度为O(1)。解题思路:上坐标(tR,tC)的初始为(0,0),先沿着矩阵的第一行移动(tC++),当原创 2017-05-30 10:36:09 · 507 阅读 · 0 评论 -
需要排序的最短子数组长度
题目: 给定一个无序数组,求出需要排序的最短子数组的长度。 例如:arr={1,5,3,4,2,6,7}返回4,因为只有[5,3,4,2]需要排序。思路: 解决这个问题可以在时间复杂度为O(N)、额外空间复杂度为O(1)完成。 初始化变量noMinIndex=-1,从右向左遍历,便利的过程记录右侧出现过的数的最小值,记为min。假设当前数为arr[i],如果arr[i]>min,原创 2017-05-31 15:30:08 · 419 阅读 · 0 评论 -
[LeetCode] Add Two Numbers 两个数字相加 C++实现 java实现
[LeetCode] Add Two Numbers 两个数字相加 java实现 C++实现You are given twonon-emptylinked lists representing two non-negative integers. The digits are stored inreverse orderand each of their nodes contain ...原创 2019-05-29 23:31:14 · 114 阅读 · 0 评论