
剑指offer
参考书籍《剑指offer》、牛客网论坛
Gunther17
我很菜
展开
-
单向链表逆序输出
递归------ 消耗内存空间-栈,空间复杂度O(n)// 直接递归实现核心代码片段public void reverse(head){ // 递归终止条件 if(head.next == null){ print(head); return; } // 下一层需要做的事儿 reverse(head.next); // 本层需要做的事儿 print(head);}...原创 2020-02-15 21:46:53 · 769 阅读 · 0 评论 -
数组中的逆序对
1.暴力两层for循环,时间复杂度O(n2)O(n^2)O(n2),空间复杂度111.2.归并排序时间复杂度O(nlogn)O(nlogn)O(nlogn),空间复杂度nnn.分析:交换copy和data是因为:1.在每次的操作中,数值的比较都是采用当前传入函数中第一项,也就是data;比较的结果都存放到copy中;也就意味着此时copy中是经过此次调用的结果。2.从最底层返回时,进入了...原创 2018-12-14 00:20:10 · 146 阅读 · 0 评论 -
顺时针打印矩阵
class Solution {public: vector<int> printMatrix(vector<vector<int> > matrix) { vector<int> res; int row = matrix.size(); in原创 2018-12-13 23:21:22 · 132 阅读 · 0 评论 -
机器人的运动范围
重要事情说三遍:加引用!加引用!加引用!flagclass Solution {public: int MoCoHelp(int i, int j, int threshold, int rows, int cols, vector<vector<int> >&flag) { if (threshold<0 || i < 0 || j&l...原创 2018-12-13 22:05:49 · 157 阅读 · 0 评论 -
矩阵中的路径/79. 单词搜索
回溯法class Solution {public: bool hasPath(char* matrix, int rows, int cols, char* str) { if (!matrix || rows &amp;amp;lt;= 0 || cols &amp;amp;lt;= 0 || !str)return false; int *visited = new int[rows*cols]; me...原创 2019-01-17 22:45:04 · 375 阅读 · 1 评论 -
序列化和反序列化二叉树
对于序列化:使用前序遍历,递归的将二叉树的值转化为字符,并且在每次二叉树的结点不为空时,在转化val所得的字符之后添加一个’ , '作为分割。对于空节点则以 ‘#’ 代替。对于反序列化:按照前序顺序,递归的使用字符串中的字符创建一个二叉树/*c_str() 功能:c_str 是c++ 中 string类 (class) 的 函数,它能把 string类的对象里的字符串 转换成 C 中cha...原创 2018-12-13 15:12:29 · 233 阅读 · 0 评论 -
数据流中的中位数
1.参数缺省的话,优先队列就是大顶堆,队头元素最大。priority_queue&amp;lt;int&amp;gt; Maxheap;2.如果要用到小顶堆,则一般要把模板的三个参数都带进去。STL里面定义了一个仿函数 greater&amp;lt;&amp;gt;,对于基本类型可以用这个仿函数声明小顶堆priority_queue&amp;lt;int, vector&amp;lt;int&原创 2018-12-13 11:17:20 · 119 阅读 · 0 评论 -
二叉搜索树的第k个结点
//本题就是考察中序遍历class Solution {public: int count = 0; TreeNode* KthNode(TreeNode* pRoot, int k) { if (pRoot == nullptr)return nullptr; TreeNode* p = nullptr; p=KthNode(pRoot-&amp;gt;left, k); ...原创 2018-12-12 22:38:45 · 102 阅读 · 0 评论 -
扑克牌顺子
//参考剑指offer,统计连续间隔数和0的数class Solution {public: static bool compare(int a, int b){ return a < b; } bool IsContinuous(vector<int> numbers) { int len = numbers.size(); int numberofZero =...原创 2018-12-12 20:48:10 · 155 阅读 · 0 评论 -
把二叉树打印成多行
分析:和层次遍历相似。需要一个队列。关键需要两个变量:1.一个是当前的需要打印的节点数,2.一个是还剩没有打印的节点数.class Solution {public: vector<vector<int> > Print(TreeNode* pRoot) { deque<TreeNode*> DQ; vector<vector<in...原创 2018-12-12 19:54:39 · 95 阅读 · 0 评论 -
按之字形顺序打印二叉树(两个栈的问题)
解题思路:思路1.大家的实现很多都是将每层的数据存进ArrayList中,偶数层时进行reverse操作,在海量数据时,这样效率太低了。思路2.按之字形顺序打印二叉树需要两个栈。我们在打印某一行结点时,把下一层的子结点保存到相应的栈里。如果当前打印的是奇数层,则先保存左子结点再保存右子结点到一个栈里;如果当前打印的是偶数层,则先保存右子结点再保存左子结点到第二个栈里。具体说:设两个栈,s1...原创 2018-12-12 18:46:57 · 219 阅读 · 0 评论 -
牛客网,剑指offer,第一页笔记
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。class Solution {public: void replaceSpace(char *str, int length) { if (str == nullptr)return; int...原创 2018-11-23 14:48:00 · 135 阅读 · 0 评论 -
剑指offer----删除链表中重复的结点/21. 合并两个有序链表
1.借助hash记录class Solution {public: map&amp;amp;amp;amp;amp;lt;int, int&amp;amp;amp;amp;amp;gt;M; ListNode* deleteDuplication(ListNode* pHead) { if (!pHead)return nullptr; ListNode* pre = pHead; ListNode* p = pHead; whil原创 2018-12-12 11:32:51 · 330 阅读 · 0 评论 -
二叉树的下一个结点
class Solution {public: TreeLinkNode* GetNext(TreeLinkNode* pNode) { if (pNode == nullptr)return nullptr; if (pNode->right)//存在右子树 { TreeLinkNode* p1 = pNode->right; while (p1->...原创 2018-12-12 11:04:11 · 104 阅读 · 0 评论 -
面试题21:调整数组顺序使奇数位于偶数前面 C++
题目:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分。分析:1.直接法2.前后指针交替法 , 类似快速排序#include <iostream> using namespace std;void reorder(int *pData, int length){ if (pD...原创 2018-09-17 14:56:00 · 211 阅读 · 0 评论 -
面试题20:表示数值的字符串 C++
题目: 请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。但 是 "12e" ,"1a3.14" ,"1.2.3", "+-5" 和 "12e+4.3"都不是。分析: 首先看第一个字符是不是正原创 2018-09-15 21:57:00 · 468 阅读 · 0 评论 -
滑动窗口的最大值
1.改进的暴力法。class Solution {public: //只考虑两端增删变化的影响 int MaxinW(const vector<int>& num, int low, int high) { int MaxVal = INT_MIN; for (int j = low; j <= high; j++) MaxVal = max(nu...原创 2018-12-10 16:30:05 · 117 阅读 · 0 评论 -
面试题19/leetcode10:正则表达式匹配 C++
题目:请实现一个函数用来匹配包含 .和* 的正则表达式。模式中的字符.表示任意一个字符,而 *表示它前面的字符可以出现任意次(含0次)。在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但与模式"aa.a"及"ab*a"均不匹配。分析:这道题的核心其实在于分析'*', 对于'.'来说,它和任意字符都匹配,可把其当做普通字符。对于'...原创 2018-09-13 22:23:00 · 1181 阅读 · 0 评论 -
字符流中第一个不重复的字符c++
使用一个hash和字符串来记录过程class Solution{public: map<char, int>M; string s=""; //Insert one char from stringstream void Insert(char ch) { s += ch; M[ch]++; } //return the first appearence o...原创 2018-12-09 22:11:48 · 389 阅读 · 0 评论 -
对称的二叉树
class Solution {public: //pRoot1, *pRoot2两种遍历方式 bool IsSym(TreeNode*pRoot1, TreeNode*pRoot2) { if (!pRoot1&&!pRoot2) return true; if (!pRoot1 || !pRoot2)//考虑遍历叶子的子节点 return fa...原创 2018-12-09 14:29:48 · 113 阅读 · 0 评论 -
构建乘积数组
1.暴力通过,O(n2)O(n^2)O(n2)class Solution {public: //暴力 vector&lt;int&gt; multiply(const vector&lt;int&gt;&amp; A) { int len = A.size(); vector&lt;int&gt;res(1, len); if (len &lt;原创 2018-12-09 10:54:06 · 127 阅读 · 0 评论 -
数组中重复的数字
1.快排,时间:O(nlog(n)),空间:O(1)2.用一个HashMap,时间:O(n),空间:O(n)顺序扫描数组,如果HashMap没有包含当前数字,就把这个数字放入HashMap,否则,就为重复数字。class Solution {public: // Parameters: // numbers: an array of integers...原创 2018-12-09 00:15:35 · 130 阅读 · 0 评论 -
把字符串转换成整数
牛客网ac code: class Solution {public: int StrToInt(string str) { int len = str.size(); if (len == 0)return 0; int sum = 0,flag=1,j=0; if (str[0] == '+' || str[0] == '-') { if (str[0] == ...原创 2018-12-08 22:59:46 · 797 阅读 · 0 评论 -
求1+2+3...+n之和
1.math.powclass Solution {public: int Sum_Solution(int n) { return ((int)pow(n,2)+n)>>1; }};2.递归. &短路class Solution {public: int Sum_Solution(int n) { int sum=n; ...原创 2018-12-08 16:32:54 · 1019 阅读 · 0 评论 -
孩子们的游戏(圆圈中最后剩下的数)/简单约瑟夫环
1.数学递推,递归,时间复杂度O(n)结论:分析:下面用数学公式推导的方法,解决约瑟夫环经典问题。第一步,这n个人我们给每一个人一个编号 0 ,1 ,2 ······ n-2,n-1第二步,当第一轮游戏结束后,这n个人少就变成了n-1个人第三步,将第一轮结束后剩下的人里面第一个报数的记为m,则这n-1个人 的编号为m,m+1,m+2······n-2,n-1,0,1,2,··...原创 2018-12-08 15:07:04 · 1493 阅读 · 0 评论 -
不用加减乘除做加法
分析:相加各位 + 计算进位十进制思想5+7 各位相加:2 进位:102+10 各位相加:12 进位:0(结束)12+0二进制计算过程5+7 各位相加:101^111=010 进位:101&111=101 (<<1=1010)2+10 各位相加:010^1010=1000 进位:010&1010=010 <<1=01008+4 10...原创 2018-12-08 00:12:31 · 109 阅读 · 0 评论 -
翻转字符串/左旋转字符串/翻转单词顺序列
例如:AAAbbbccc左旋为bbbcccAAA//非常规做法,牛客网acceptclass Solution {public: string LeftRotateString(string str, int n) { if (n&gt;str.size()||n&lt;0)return ""; if (n == str.size()||n==0)return str; ...原创 2018-12-07 11:33:46 · 284 阅读 · 0 评论 -
leetcode3/剑指offer48. 无重复字符的最长子串
1.直接哈希法. 复杂度在扫描完所有的s[0...n],s[1...n],s[2...n]....后,最长的极长字符串就是字长不重复子串#include <iostream> #include <string> #include <assert.h>using namespace std;class Solution {...原创 2018-09-20 10:06:00 · 341 阅读 · 0 评论 -
链表中环的入口结点
1.牛客网ac, 时间O(n),空间O(1)class Solution {public: ListNode* EntryNodeOfLoop(ListNode* pHead) { ListNode *p1 = pHead-&amp;amp;gt;next; ListNode *p2 = p1-&amp;amp;gt;next; if (!pHead||!p1 || !p2)return nullptr; ...原创 2018-12-07 00:11:54 · 109 阅读 · 0 评论 -
和为S的两个数字/和为S的连续正数序列
1.牛客网ac其实同leetcode第一题两数之和一样,从两边逼近。class Solution {public: vector&amp;amp;lt;int&amp;amp;gt; FindNumbersWithSum(vector&amp;amp;lt;int&amp;amp;gt; array, int sum) { int left = 0, right = array.size() - 1; int fla原创 2018-12-06 23:24:56 · 202 阅读 · 0 评论 -
数组中只出现一次的数字
1.hash映射,牛客网ac, 时间复杂度O(n),空间复杂度O(n)class Solution {public: /*hash映射*/ void FindNumsAppearOnce(vector<int> data, int* num1, int *num2) { int flag = 0; map<int, int>M; for (int i =...原创 2018-12-06 22:37:15 · 129 阅读 · 0 评论 -
平衡二叉树
1.利用之前的二叉树的深度,但是这样就会导致重复遍历子树牛客网通过code:class Solution {public: int TreeDepth(TreeNode* pRoot) { if (pRoot == nullptr)return 0; return TreeDepth(pRoot-&gt;left) &gt; TreeDepth(pRoot-&gt;right) ...原创 2018-12-06 20:48:32 · 142 阅读 · 0 评论 -
二叉树的深度
1.递归遍历树的扩展, 牛客网ac/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution {public: int TreeD...原创 2018-12-06 19:48:50 · 117 阅读 · 0 评论 -
数字在排序数组中出现的次数/leetcode 34. 在排序数组中查找元素的第一个和最后一个位置
二分查找的扩展,牛客网Acclass Solution {public:/*二分查找的扩展,确定最左边和最右边即可*/ int FindLeft(vector&amp;amp;amp;amp;amp;lt;int&amp;amp;amp;amp;amp;gt; data, int k) { int flag = 0; int begin = 0, end = data.size() - 1; while (begin &amp原创 2018-12-06 19:35:08 · 674 阅读 · 0 评论 -
两个链表的第一个公共结点
1.蛮力法。O(m*n)两层for循环遍历两个链表一个个匹配。2.空间换时间。牛客网签到。分析:先遍历一个链表存放在hash表中,在顺序遍历另一个链表同时查看hash表中是否存在,存在就是公共节点,直接返回。c++ code:class Solution {public: ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode...原创 2018-12-06 17:49:33 · 122 阅读 · 0 评论 -
第一个只出现一次的字符
牛客网ac、签到题注意如果只有一个字符返回位置 0class Solution {public: /*哈希映射*/ int FirstNotRepeatingChar(string str) { map<char, int>Ms; for (int i = 0; i < str.length(); i++) { Ms[str[i]]++;...原创 2018-12-05 23:00:13 · 100 阅读 · 0 评论 -
丑数
方法一:牛客网 no acceptclass Solution {public: /* 首先判断2,3,5是不是一个数的因子,即可以整除。 然后判断这个数是不是2,3,5因子的组合,即一直整除2,3,5最后是不是为1 */ bool UglyNum(int n) { while (n % 2 == 0) n /= 2; while (n % 3 == 0) n...原创 2018-12-05 22:08:25 · 124 阅读 · 0 评论 -
把数组排成最小数c++
洒下原创 2018-12-05 18:56:10 · 1025 阅读 · 0 评论 -
1的数目
暴力O(nlogk)class Solution {public: int CountNum(int i) { int sum=0; while(i) { if(i%10==1) sum++; i/=10; } ...原创 2018-11-30 21:43:13 · 124 阅读 · 0 评论