
C++
文章平均质量分 61
孙小小子
LeetCode 刷题 https://github.com/syt798/LeetCode
展开
-
字符串匹配_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 阅读 · 0 评论 -
基数排序
//基数排序#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 阅读 · 0 评论 -
寻找素数
/**埃拉托斯特尼筛选法:找出小于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 · 334 阅读 · 0 评论 -
约瑟夫问题,双向链表实现
问题描述: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 · 1285 阅读 · 0 评论 -
字符串匹配_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 · 387 阅读 · 0 评论 -
将二叉树拆成链表
/*问题描述:将一棵二叉树按照前序遍历拆解成为一个假链表。所谓的假链表是说,用二叉树的 right 指针,来表示链表中的 next 指针。来源:LintCode作者:syt日期:2017-11-13思路:通过后序遍历,先对左子树进行转换,在对右子树进行转换,在变成链表时,若该节点root的左子树为NULL,则不用操作,若左子树不为空,找到左子树的叶子节点tmp,将该节点ro原创 2017-11-13 20:44:17 · 217 阅读 · 0 评论 -
在二叉树中插入节点
/*问题描述:给定一棵二叉查找树和一个新的树节点,将节点插入到树中。你需要保证该树仍然是一棵二叉查找树。(不使用递归)来源:LintCode作者:syt日期:2017-11-13思路:用一个节点preNode记录最后一个节点,当遍历到为空时,根据插入节点的值与preNode的值比较,大于preNode的值插入到preNode的右子树,否则,为左子树*/#include原创 2017-11-13 21:10:03 · 7480 阅读 · 0 评论 -
关于堆的操作
/*堆操作来源: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 · 349 阅读 · 0 评论 -
二叉树的层次遍历
/*问题描述:给出一棵二叉树,返回其层序遍历来源: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 阅读 · 0 评论 -
二叉树的路径的和
/*问题描述:给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径。一个有效的路径,指的是从根节点到叶节点的路径。来源:LintCode作者:syt日期:2017-11-10*/#include class TreeNode{public: int val; TreeNode *left, *right; TreeNode(){} TreeN原创 2017-11-10 15:51:18 · 279 阅读 · 0 评论 -
二叉树的所有路径
/*问题描述:给一棵二叉树,找出从根节点到叶子节点的所有路径。来源:LintCode作者:syt日期:2017-11-10思路:迭代找出叶子节点并记录中间的寻找过程*/#include class TreeNode{public: int val; TreeNode *left, *right; TreeNode(){} TreeNode(int原创 2017-11-10 15:25:04 · 250 阅读 · 0 评论 -
字符串中旋转单词
/*问题描述:给定一个字符串,单词以空格隔开,对字符串进行转换,例如“I love you”转换成“you love I”,要求空间复杂度为O(1)来源:网易算法课日期:2017-10-24说明:共分为两步,第一步:将整个字符串进行翻转;第二步:对每个单词进行翻转*/#include #include using namespace std;void reverseStrin原创 2017-10-24 20:21:02 · 632 阅读 · 0 评论 -
简化版Vector实现
/*功能:简化版vector说明:参照《Data Structures with C++ Using STL》第二版编写, 没有编写异常处理模块, 实现构造函数,复制构造函数,析构函数,赋值运算重载,下标运算重载, 尾部添加元素push_back, 删除元素pop_back,取尾部元素back() 获取向量大小size(),容量capacity(),判断向量是否为空empty(原创 2017-10-12 19:09:26 · 251 阅读 · 0 评论 -
简化版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 · 227 阅读 · 0 评论 -
字符串旋转
/*问题描述:数组 A={a,b,c,d,e}数组A从位置i旋转,例如i=3,旋转后的A为 A={d,e,a,b,c}来源:网易算法课日期:2017-10-12说明:提供了4个方法:方法1、2是传统方法,通过对元素的逐次移位完成,时间复杂度较高方法3、4时间复杂度都为O(n)*/#include #include using namespace std;原创 2017-10-13 19:31:25 · 195 阅读 · 0 评论 -
Sukudo问题
/*问题描述:sukudo棋盘是一种逻辑游戏,是一种9*9的网格,要求满足一行,每一列,每个3*3的子网格都是由1-9九个数字组成,每个子网格填充的数字都不重复,给定一个9*9的二维数组,给出满足条件的填充算法。来源:网易算法课说明:采用一维启发式搜索算法,对于当前的位置,遍历1-9九个数字,如果找到满足条件的数字就继续走下一步,如果找不到合适的数字则退回上一步,另外选原创 2017-10-13 20:13:07 · 675 阅读 · 0 评论 -
数组的螺旋式打印
/*问题描述:给定一个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 · 281 阅读 · 0 评论 -
数组旋转
/*问题描述:给定一个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 · 225 阅读 · 0 评论 -
平衡二叉树
/*问题描述:平衡二叉树,给定一个二叉树,确定它是高度平衡的。对于这个问题,一棵高度平衡的二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过1。来源:LintCode作者:syt日期:2017-11-10思路:计算每个结点的左子树和右子树的深度,并判断是否是平衡二叉树*/#include class TreeNode{public: int va原创 2017-11-10 10:02:41 · 189 阅读 · 0 评论 -
游程编码
/*问题描述:游程编解码,给定一个字符串"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 · 1495 阅读 · 0 评论 -
数串
/*问题描述:设有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 · 280 阅读 · 0 评论 -
C++中不能重载的运算符
不能重载的运算符:sizeof 运算符 :: 作用域运算符 ?:条件运算符 . 直接成员运算符 *指针运算符只能通过成员函数进行重载= 赋值运算符 []下标运算符 ->成员运算符原创 2018-01-22 20:01:10 · 673 阅读 · 1 评论 -
重建二叉树
题目描述输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{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 · 146 阅读 · 0 评论 -
用两个栈实现队列
题目描述用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。class Solution{public: void push(int node) { stack1.push(node); } int pop() { while(!stack1.empty()) { ...原创 2018-05-02 11:32:31 · 119 阅读 · 0 评论 -
旋转数组中的最小数字
题目描述把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{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 · 160 阅读 · 0 评论 -
数组中的逆序对
题目描述在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007解法一:顺序扫描整个数组,每扫描到一个数字,逐个比较该数字与后面的数字的大小,时间复杂度为 O(n2)int InversePairs(vector<int> data){ ...原创 2018-05-09 15:29:53 · 206 阅读 · 0 评论 -
合并两个排序链表
题目描述输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。非递归:/*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 · 163 阅读 · 0 评论 -
扑克牌顺子
题目描述: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 · 532 阅读 · 0 评论 -
圆圈中最后剩下的数字
题目描述: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 · 393 阅读 · 0 评论 -
按之字形顺序打印二叉树
题目描述请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推思路:利用两个栈,奇数行存在stack1,偶数行存在stack2,若stack1不为空,将stack1中的结点取出,查找该节点的子结点放入stack2中,对于stack2也采用相同处理,注意:取子结点时的顺序/*struct TreeNode {...原创 2018-05-24 10:20:12 · 218 阅读 · 0 评论 -
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 · 425 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 · 925 阅读 · 0 评论 -
C++中不同类型变量对应的字节数
类型32位系统64位系统char11short22int44float44double88long48long long88指针48原创 2018-01-23 09:52:49 · 444 阅读 · 0 评论 -
逆波兰表达式
逆波兰表达式又叫做后缀表达式:如下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 · 458 阅读 · 0 评论 -
括号匹配
利用堆栈实现括号的匹配/*问题描述:括号匹配作者: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 · 184 阅读 · 0 评论 -
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 · 163 阅读 · 0 评论 -
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 · 153 阅读 · 0 评论 -
C++ 赋值和初始化
在某些编译环境下,C++变量会被自动初始化,但在有些环境下变量不会被初始化,变量没有被初始化,可能会导致程序错误或者某些不可预知的行为,最简单的方法是在使用变量或对象之前对其初始化。在类的构造函数中,要区分赋值和初始化的区别:class Test{public: Test(string& name, string& no);private: string my...原创 2018-03-01 19:38:43 · 753 阅读 · 0 评论 -
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 阅读 · 0 评论