- 博客(111)
- 资源 (2)
- 收藏
- 关注
转载 C++容器小结
一、容器种类:1、顺序容器是线性结构,顺序容器中每个元素的位置与元素的值无关,只与元素添加到容器的次序有关。顺序容器有:array(C++11)、vector、list、deque、forward_list(C++11)2、关联容器:关联容器是非线性结构(一般使用树结构),元素的存储按照一定的顺序存储。关联容器有:map、set、multimap、multiset3、无序关联容器:un...
2018-10-12 12:20:42
215
原创 按之字形顺序打印二叉树
题目描述请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推思路:利用两个栈,奇数行存在stack1,偶数行存在stack2,若stack1不为空,将stack1中的结点取出,查找该节点的子结点放入stack2中,对于stack2也采用相同处理,注意:取子结点时的顺序/*struct TreeNode {...
2018-05-24 10:20:12
214
原创 圆圈中最后剩下的数字
题目描述:0,1,...n-1这n个数字排成一个圆圈,从数字0开始每次从这个圆圈里删除第m个数字。求出这个圆圈里剩下的最后一个数字。解题思路:利用链表实现首尾相连的圆圈,逐个从链表中删除结点,直到剩下最后一个结点。struct ListNode { int val; struct ListNode *next; ListNode(int x) :val(x), next(NULL) {}}...
2018-05-16 20:59:37
391
原创 扑克牌顺子
题目描述:LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为...
2018-05-16 20:32:09
530
原创 数组中的逆序对
题目描述在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007解法一:顺序扫描整个数组,每扫描到一个数字,逐个比较该数字与后面的数字的大小,时间复杂度为 O(n2)int InversePairs(vector<int> data){ ...
2018-05-09 15:29:53
202
原创 合并两个排序链表
题目描述输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。非递归:/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { }};*/class Solution {public: ListNode* M...
2018-05-03 10:22:35
161
原创 旋转数组中的最小数字
题目描述把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。1)最简单的解法,遍历数组,寻找最小值,时间复杂度为 O(n)class Solution {public: ...
2018-05-02 12:27:35
157
原创 用两个栈实现队列
题目描述用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。class Solution{public: void push(int node) { stack1.push(node); } int pop() { while(!stack1.empty()) { ...
2018-05-02 11:32:31
118
原创 重建二叉树
题目描述输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。/** * Definition for binary tree * struct TreeNode { * int val; * ...
2018-05-02 11:16:07
143
原创 Roman to Integer
Description:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.思路:题目是把罗马数字字符串转换为整数,罗马数字由 I、V、X、L、C、D、M 7个基本数字构成,罗马数字与整数转换如下图,通过观察可以得出规律,从右向左遍历, 若...
2018-03-26 11:23:27
422
原创 Find Bottom Left Tree Value
Description:Given a binary tree, find the leftmost value in the last row of the tree. Example 1:Input: 2 / \ 1 3Output:1 Example 2: Input: 1 / \ 2 3 / /...
2018-03-21 16:32:46
145
原创 Rotate String
Description:We are given two strings, A and B.A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea'...
2018-03-16 10:15:16
275
原创 Reverse Linked List
Description: Reverse a singly linked list.思路1:使用栈结构实现反转,先遍历一遍list,依次入栈,然后依次出栈,重新建立list/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListN...
2018-03-13 10:43:49
921
原创 Greedy Algorithm--Assign Cookies
Description:Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum s...
2018-03-12 11:06:52
166
原创 Valid Palindrome II
Description:Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.Example 1:Input: "aba"Output: TrueExample 2:Input: "abca"Output: TrueExpla...
2018-03-08 10:48:22
151
原创 Rotate List
Description: Given a list, rotate the list to the right by k places, where k is non-negative.Example: Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.思路:...
2018-03-07 17:34:01
162
原创 括号匹配
利用堆栈实现括号的匹配/*问题描述:括号匹配作者:syt日期:2018-03-07*/#include <iostream>#include <stack>#include <string>using namespace std;class BraceMatch{public: BraceMatch(string s = "") :st...
2018-03-07 11:39:14
183
原创 逆波兰表达式
逆波兰表达式又叫做后缀表达式:如下a+b --> a,b,+a+(b-c) --> a,b,c-,+/*问题描述:逆波兰表达式作者:syt日期:2018-03-07*/#include <iostream>#include <string>#include <stack>using namespace std;class Po...
2018-03-07 10:47:40
455
原创 C++ 赋值和初始化
在某些编译环境下,C++变量会被自动初始化,但在有些环境下变量不会被初始化,变量没有被初始化,可能会导致程序错误或者某些不可预知的行为,最简单的方法是在使用变量或对象之前对其初始化。在类的构造函数中,要区分赋值和初始化的区别:class Test{public: Test(string& name, string& no);private: string my...
2018-03-01 19:38:43
753
原创 C++中不同类型变量对应的字节数
类型32位系统64位系统char11short22int44float44double88long48long long88指针48
2018-01-23 09:52:49
441
原创 C++中不能重载的运算符
不能重载的运算符:sizeof 运算符 :: 作用域运算符 ?:条件运算符 . 直接成员运算符 *指针运算符只能通过成员函数进行重载= 赋值运算符 []下标运算符 ->成员运算符
2018-01-22 20:01:10
669
1
原创 数串
/*问题描述:设有n个正整数,将他们连接成一排,组成一个最大的多位整数。 如:n=3时,3个整数13,312,343,连成的最大整数为34331213。 如:n=4时,4个整数7,13,4,246连接成的最大整数为7424613。来源:牛客作者:syt日期:2017-12-13*/#include #include #include #include using
2017-12-13 20:55:57
278
原创 关于堆的操作
/*堆操作来源:Data Structures with C++ Using STL*/#include #include #include #include using namespace std;//调整堆的顺序template void adjustHeap(vector& v, int first, int last, Compare comp){ int c
2017-11-20 09:45:31
347
原创 在二叉树中插入节点
/*问题描述:给定一棵二叉查找树和一个新的树节点,将节点插入到树中。你需要保证该树仍然是一棵二叉查找树。(不使用递归)来源:LintCode作者:syt日期:2017-11-13思路:用一个节点preNode记录最后一个节点,当遍历到为空时,根据插入节点的值与preNode的值比较,大于preNode的值插入到preNode的右子树,否则,为左子树*/#include
2017-11-13 21:10:03
7475
原创 将二叉树拆成链表
/*问题描述:将一棵二叉树按照前序遍历拆解成为一个假链表。所谓的假链表是说,用二叉树的 right 指针,来表示链表中的 next 指针。来源:LintCode作者:syt日期:2017-11-13思路:通过后序遍历,先对左子树进行转换,在对右子树进行转换,在变成链表时,若该节点root的左子树为NULL,则不用操作,若左子树不为空,找到左子树的叶子节点tmp,将该节点ro
2017-11-13 20:44:17
216
原创 寻找素数
/**埃拉托斯特尼筛选法:找出小于n的所有素数*思路:* 先建立一个集合,包含了2到n范围内的所有整数,从m=2开始,遍历集合元素* 删除所有的2的倍数,m=3,删除集合中所有3的倍数,以此类推,最终筛选出2到n的素数 */void findPrime(set& s, int n){ s.erase(s.begin(), s.end()); if (n <= 1) { re
2017-11-13 09:58:34
333
原创 二叉树的路径的和
/*问题描述:给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径。一个有效的路径,指的是从根节点到叶节点的路径。来源:LintCode作者:syt日期:2017-11-10*/#include class TreeNode{public: int val; TreeNode *left, *right; TreeNode(){} TreeN
2017-11-10 15:51:18
278
原创 二叉树的所有路径
/*问题描述:给一棵二叉树,找出从根节点到叶子节点的所有路径。来源:LintCode作者:syt日期:2017-11-10思路:迭代找出叶子节点并记录中间的寻找过程*/#include class TreeNode{public: int val; TreeNode *left, *right; TreeNode(){} TreeNode(int
2017-11-10 15:25:04
248
原创 平衡二叉树
/*问题描述:平衡二叉树,给定一个二叉树,确定它是高度平衡的。对于这个问题,一棵高度平衡的二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过1。来源:LintCode作者:syt日期:2017-11-10思路:计算每个结点的左子树和右子树的深度,并判断是否是平衡二叉树*/#include class TreeNode{public: int va
2017-11-10 10:02:41
189
原创 字符串匹配_2
/*问题描述:字符串匹配,KMP算法来源:网易算法课日期:2017-11-6*/#include #include #include using namespace std;class KMP{public: KMP(string P) { int length = P.length() + 1; this->P = P; for (int i =
2017-11-06 20:22:33
167
原创 二叉树的层次遍历
/*问题描述:给出一棵二叉树,返回其层序遍历来源:LintCode作者:syt日期:2017-11-6*/#include #include #include using namespace std;class TreeNode{public: int val; TreeNode *left, *right; TreeNode(){} TreeNode
2017-11-06 16:59:35
207
原创 约瑟夫问题,双向链表实现
问题描述:N个人围城一圈,从第一个人开始报数,第M个被杀掉,最后剩下一个人,其余人都排除/*链表*/#include #include using namespace std;//双向链表template class dnode{public: T nodeValue; dnode *prev; dnode *next; dnode() { next =
2017-11-03 11:36:13
1282
原创 字符串匹配_1
/*问题描述:字符串匹配,暴力枚举法和Rabin-Karp来源:网易算法课日期:2017-10-26*/#include #include using namespace std;//暴力枚举法//target 目标字符串 str 待查找字符串 int match_1(string target, string str){ int i, j; for (i = 0; i
2017-10-26 20:54:08
382
原创 基数排序
//基数排序#include #include #include using namespace std;class RadixSort{public: void radixSort(vector& v, int d); //基数排序 //v 要排序的向量,d 排序数字的位数 void distribute(const vector& v, int power); /
2017-10-26 10:05:20
153
原创 字符串中旋转单词
/*问题描述:给定一个字符串,单词以空格隔开,对字符串进行转换,例如“I love you”转换成“you love I”,要求空间复杂度为O(1)来源:网易算法课日期:2017-10-24说明:共分为两步,第一步:将整个字符串进行翻转;第二步:对每个单词进行翻转*/#include #include using namespace std;void reverseStrin
2017-10-24 20:21:02
631
原创 游程编码
/*问题描述:游程编解码,给定一个字符串"aaasssdd",编码为"3a3c2d", 解码为给定一个字符串"3d2s",解码为"dddss"来源:网易算法课日期:2017-10-24*/#include #include using namespace std;class RLE{public: string encode(string s) { string str
2017-10-24 20:20:15
1491
原创 数组旋转
/*问题描述:给定一个2维数组,将数组中的元素旋转90度,空间复杂度为O(1)来源:网易算法课日期:2017-10-23*/#include using namespace std;class ArrayRotation{public: ArrayRotation(int n) { size = n; arr = new int *[n]; for (in
2017-10-23 20:18:04
220
原创 数组的螺旋式打印
/*问题描述:给定一个2维数组,将数组中的元素以螺旋状顺序打印出来来源:网易算法课日期:2017-10-23*/#include using namespace std;class ArraySpiral{public: ArraySpiral(int n) { arr = new int *[n]; for (int i = 0; i < n; i++)
2017-10-23 20:17:27
280
原创 简化版stack
/*功能:简化版stack说明:参照《Data Structures with C++ Using STL》第二版编写, 没有编写异常处理模块,利用vector类实现ministack 尾部添加元素push, 删除元素pop,取栈顶元素top() 获取栈元素的数量size(),判断栈是否为空empty()*/#include using namespace std;templ
2017-10-23 09:28:35
226
原创 Windows10/7 + Visual Studio 2013 + PCL1.8.0
最近在用PCL做点云的显示,分别在Windows10和Windows7环境下配置了PCL,配置过程中也查了参考了不少配置博主的配置过程,自己再重新整理一遍。1. 下载安装PCLPCL安装包链接: https://pan.baidu.com/s/1jIb1x0U 密码: 6kaa,根据系统选择安装包,我选择的PCL-1.8.0-AllInOne-msvc2013-win64版本
2017-10-15 13:35:02
641
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人