剑指offer
xiaxzhou
这个作者很懒,什么都没留下…
展开
-
【剑指offer】题65:滑动窗口最大值
使用双向队列保存当前可能成为最大值的数值vector<int> maxInWindows(const vector<int>& num, unsigned int size){ vector<int> max_vec; if (num.size()<size||size<1) return max_vec; deque<int> index; for (a原创 2017-09-03 16:10:32 · 386 阅读 · 0 评论 -
【剑指offer】题49:字符串转整数
注意 int 的正负取值范围不对称, 需要根据符号位分开判定是否正溢出或者负溢出int strtoint_core(string str,int sign){ long long num(0); for (auto iter = str.begin(); iter != str.end();++iter) { if ((*iter)<'0'||(*iter原创 2017-07-06 16:08:36 · 209 阅读 · 0 评论 -
【剑指offer】题48:不能被继承的类
class sealedclass{public: static sealedclass* getinstance() { return new sealedclass(); } static void Delet(sealedclass* instance) { delete instance; }private原创 2017-07-06 14:24:07 · 254 阅读 · 0 评论 -
【剑指offer】题47:不用加减乘除做加法
int Add(int num1, int num2){ int tmp1 = num1&num2; int tmp2 = num1^num2; tmp1 = tmp1 << 1; if ((tmp1&tmp2 )== 0) { return tmp1 | tmp2; } else{ return Add(tm原创 2017-07-06 14:04:31 · 227 阅读 · 0 评论 -
【剑指offer】题46:1+2+...+N
利用构造函数和静态变量class sum{public: static int s; static int k; sum(){ k++; s += k; };};int sum::s(0);int sum::k(0);int Sum_Solution(int n){ vector<sum> tmp(n); return sum::s;}int mai原创 2017-07-06 13:05:02 · 415 阅读 · 0 评论 -
【剑指offer】题45:圆圈中最后剩下的数字
int LastRemaining_Solution(int n, int m){ if (n<=1||m<1) { return -1; } vector<int> vec(n); int index(-1); int count(n-1); while (count) { int k(m);原创 2017-07-06 12:22:10 · 316 阅读 · 0 评论 -
【剑指offer】题44:扑克牌的顺子
bool IsContinuous(vector<int> numbers){ if (numbers.size()<5) { return false; } sort(numbers.begin(), numbers.end()); int min_, max_; auto iter = find_if(numbers.begin(原创 2017-07-05 16:51:22 · 214 阅读 · 0 评论 -
【剑指offer】题43:n个骰子的点数
动态规划 使用dp[k][i]表示k个骰子时,点数和为i的组合数 则转移状态方程: dp[k][i]=∑j=16dp[k−1][i−j]dp[k][i]=\sum_{j=1}^6{dp[k-1][i-j]}void func(int n){ if (n < 1) { return; } vector<int> vec(n*6+1); v原创 2017-07-05 12:02:15 · 277 阅读 · 0 评论 -
【剑指offer】题42:翻转单词顺序VS左旋转字符串
void Reverse_Word(string& str, int left, int right){ while (left<right) { std::swap(str[left++], str[right--]); }}string ReverseSentence(string str){ if (str.size()==0) {原创 2017-07-05 10:48:50 · 223 阅读 · 0 评论 -
【剑指offer】题41:和为s的两个数VS连续正整数之和为S
vector<vector<int> > FindContinuousSequence(int sum){ vector<vector<int>> vec; if (sum<3) { return vec; } int left(1), right(2); int tmp(3); while (left<right)原创 2017-07-04 22:49:51 · 238 阅读 · 0 评论 -
【剑指offer】题40:数组中只出现一次的数字
void FindNumsAppearOnce(vector<int> data, int* num1, int *num2){ if (data.size()<2) { return; } int N(data[0]); for (auto i = 1; i < data.size();++i) { N = N^da原创 2017-07-04 22:10:58 · 220 阅读 · 0 评论 -
【剑指offer】题39:平衡二叉树
bool func(TreeNode* pRoot, int& depth){ if (pRoot == NULL) { return true; } int left(0), right(0); bool leftflag = func(pRoot->left, left); bool rightflag = func(pRoot-原创 2017-07-04 22:01:31 · 201 阅读 · 0 评论 -
【剑指offer】题39:二叉树的深度
int TreeDepth(TreeNode* pRoot){ if (pRoot == NULL) { return 0; } return max(TreeDepth(pRoot->left) + 1, TreeDepth(pRoot->right) + 1);}原创 2017-07-04 21:32:31 · 230 阅读 · 0 评论 -
【剑指offer】题51:数组中重复的数字
bool duplicate(int numbers[], int length, int* duplication) { if (numbers==NULL||length<2||duplication==NULL) { return false; } vector<int> vec(length); for (auto i = 0; i原创 2017-07-06 16:14:44 · 251 阅读 · 0 评论 -
【剑指offer】题52:构建乘积数组
注意反序遍历数组时 auto i = vec.size()-1; i 为无符号整形vector<int> multiply(const vector<int>& A){ vector<int> re(A.size()); if (A.size()<=1) { return re; } vector<int> C(A.size(),1原创 2017-07-06 19:25:29 · 175 阅读 · 0 评论 -
【剑指offer】题64:数据流中的中位数
STL适配器:heap、priority_queue 默认是 vector 实现的最大堆stack,queue默认是 dequeue 实现的试用一个最大堆和一个最小堆,保持两个最大堆中所有数小于最小堆,且保证两个堆元素个数相差不超过1。#include <iostream>#include <vector>#include <algorithm>#include <numeric>#incl原创 2017-09-03 14:55:51 · 299 阅读 · 0 评论 -
【剑指offer】题63:二叉搜索树的第k个节点
TreeNode* KthNode_core(TreeNode* pRoot, int& k){ TreeNode* target(NULL); if (pRoot->left!=NULL) { target = KthNode_core(pRoot->left, k); } if (target == NULL) {原创 2017-09-03 11:33:49 · 285 阅读 · 0 评论 -
【剑指offer】题61:二叉树序列化、反序列化
#include <iostream>#include <string>#include <sstream>using namespace std;struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x),原创 2017-09-03 10:32:58 · 277 阅读 · 0 评论 -
【剑指offer】题61:之字打印二叉树
vector<vector<int>> Print(TreeNode* pRoot){ vector<vector<int>> vec; if (pRoot== NULL) return vec; stack<TreeNode*> my_stack[2]; int cur_deep(0); int next_deep(1); my_stac原创 2017-09-02 22:00:54 · 263 阅读 · 0 评论 -
【剑指offer】题60:分层遍历打印二叉树
void Print(TreeNode* pRoot){ queue<TreeNode*> que; int cur_count(0); int next_count(0); if (pRoot == NULL) { return; } que.push(pRoot); cur_count = 1; while (!原创 2017-09-02 21:35:57 · 268 阅读 · 0 评论 -
【剑指offer】题32:从1到n整数中1出现的次数
动态规划: 使用all[i]all[i]表示所有小于等于i位的数字中1出现的次数如: all[3]表示 0-999 中1出现的次数 all[4]表示 0-9999 中1出现的次数 使用part[i]part[i]表示所有小于等于i位的数字且小于等于整数n的第1-i位组成的数字中1出现的次数如:n = 5354, 则 : part[2]part[2]表示0-54中1出现的次数 pa原创 2017-07-03 17:46:45 · 277 阅读 · 0 评论 -
【剑指offer】题2:实现单件(singleton)模式
实现单件(singleton)模式 单件: 不允许自动生成对象、不允许赋值、拷贝对象:屏蔽构造函数、拷贝构造函数、赋值运算符加锁并两次判断 互斥量:Mutex +区域锁:socpedlock 区域锁即使用RAII模式(Resource Acquisition Is Initialization):把锁封装到一个对象里面。 锁的初始化(mutex原创 2017-06-09 15:58:43 · 418 阅读 · 0 评论 -
【剑指offer】题16:反转链表
if (pHead == NULL || pHead->next == NULL) { return pHead; } ListNode dummy(0); dummy.next = pHead; ListNode * pPre = &dummy; ListNode * pCur = pPre->next; ListNode *原创 2017-06-22 21:41:51 · 224 阅读 · 0 评论 -
【剑指offer】题57:删除链表中的重复的结点
ListNode* deleteDuplication(ListNode* pHead){ if (pHead == NULL || pHead->next == NULL) { return pHead; } ListNode dummy(0); dummy.next = pHead; ListNode * pre(&dummy);原创 2017-07-06 22:29:28 · 311 阅读 · 0 评论 -
【剑指offer】题56:链表环的入口
使用哈希表记录出现过的节点ListNode* EntryNodeOfLoop(ListNode* pHead){ if (pHead==NULL) { return NULL; } set<ListNode*> my_set; //hash_set<ListNode*> my_set; ListNode * curnode(pHead原创 2017-07-06 22:01:46 · 234 阅读 · 0 评论 -
【剑指offer】题55:字符流中第一个不重复的字符
使用哈希表记录次数,使用链表记录顺序 哈希表保存指向链表的指针,用于删除重复出现的字符数组中第一个只出现一次的字符问题也可以使用类似的方法,这样只需遍历一遍字符串即可得到结果class Solution{public: Solution(){ vec.resize(256); }; //Insert one char from stringstream void I原创 2017-07-06 21:43:41 · 410 阅读 · 0 评论 -
【剑指offer】题53:正则表达式匹配
bool match_core(char* str, char * pattern){ if (*str == '\0'&&*pattern == '\0') { return true; } if (*str != '\0' && *pattern == '\0') { return false; } if原创 2017-07-06 21:00:29 · 267 阅读 · 0 评论 -
【剑指offer】题38:字数在数组中出现的次数
二分查找 上界 下界int GetLowBound(vector<int>& data, int left, int right, int k){ while (left+1<right) { int mid = left + ((right - left) >> 1); if (data[mid]>=k) {原创 2017-07-04 20:50:28 · 255 阅读 · 0 评论 -
【剑指offer】题37:链表公共节点
ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2){ if (pHead1==NULL||pHead2==NULL) { return NULL; } int k1(0),k2(0); ListNode * pCur1(pHead1); ListNode原创 2017-07-04 20:36:27 · 205 阅读 · 0 评论 -
【剑指offer】题12:打印1到最大的n位数
使用vector实现大数#include <iostream>#include <string>#include <vector>#include <algorithm>#include <numeric>#include <iomanip>#include <stack>using namespace std;#define debug_class BigNum{public:原创 2017-06-16 17:36:00 · 311 阅读 · 0 评论 -
【剑指offer】题20:顺时针打印矩阵
vector<int> printMatrix(vector<vector<int> > matrix){ vector<int> result; int startx(0), starty(0), endx(matrix.size()-1), endy(matrix[0].size()-1); while (startx <= endx && starty <= endy原创 2017-07-02 09:46:24 · 250 阅读 · 0 评论 -
【剑指offer】题18:树的子结构
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2){ if (pRoot2==NULL) { return true; } if (pRoot1==NULL) { return false; } if (pRoot1->val == pRoot2->val)原创 2017-07-01 21:28:57 · 244 阅读 · 0 评论 -
【剑指offer】题1: 赋值运算符函数
函数返回类型: 令operator=返回reference to *this (Effective C++ 条款10)函数参数: 一般应该是常量引用类型异常安全: 在赋值运算符函数中使用swap 使用copy and swap技术处理自身赋值和赋值失败异常问题(Effective C++ 条款11) copy and swap策略:为打算做修改的对象做一份副本,在副本上做一切必原创 2017-06-08 22:38:41 · 327 阅读 · 0 评论 -
【算法题】之字打印二叉树
使用双栈,一个栈表示当前层,一个栈表示下一层 换层时交换指向栈的指针 还需要判断当前层时偶数层还是奇数层,偶数层左子节点先入栈,奇数次右子节点先入栈#include <iostream>#include <numeric>#include<algorithm>#include <stack>#include <queue>using namespace std;struct Tree原创 2017-06-04 16:53:06 · 478 阅读 · 0 评论 -
【剑指offer】题11:数值的整数次方
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方测试:0、0 0、-1 2.3 、0 2.3 、-2 2.3 、11bool equal(double lhs, double rhs){ if (abs(lhs-rhs)<0.00001) { return true; } els原创 2017-06-16 16:37:51 · 198 阅读 · 0 评论 -
【剑指offer】题10:二进制中1的个数
输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示常规解法:int NumberOf1(int n) { int m = sizeof(n); m *= 8; int k(0); for (auto i = 0; i < m;++i) { if ((n>>i)&1) { k++;原创 2017-06-16 15:24:29 · 202 阅读 · 0 评论 -
【剑指offer】题9:矩阵覆盖
我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?使用F(n)F(n)表示第n列是由一整块小矩阵竖着覆盖的覆盖方法数 使用G(n)G(n)表示第n列是由两块小矩阵横着覆盖的覆盖方法数F(n)=F(n−1)+G(n−1)F(n)=F(n-1)+G(n-1) G(n)=F(n−2)+G(n−2)G(n)=F(n-2)+G原创 2017-06-16 14:34:01 · 242 阅读 · 0 评论 -
【剑指offer】题9:裴波那契数列
测试: -10999 long long Fibonacci(int n) { if(n==0) return 0; if(n==1) return 1; long long f_1 = 1,f_2 = 0; long long f(-1); for(auto i原创 2017-06-16 11:12:37 · 327 阅读 · 0 评论 -
【剑指offer】题8:旋转数组的最小值
考虑到:3 4 5 1 21 0 1 1 11 2 3 4 51null#include <iostream>#include <string>#include <vector>#include <algorithm>#include <numeric>#include <iomanip>#include <stack>using namespace std;#define debug原创 2017-06-15 23:04:09 · 87 阅读 · 0 评论 -
【算法题】判断二叉树平衡性
由平衡二叉树的定义可知,每个节点的左右子树的高度差要小于等于1可以用递归实现,在遍历二叉树各节点事,若节点的左右子树的深度之差不超过1,则是平衡二叉树。int getDepth(Tree*root){ if (root == NULL) return 0; int leftDepth = getDepth(root->left); int rightDep原创 2017-06-04 21:53:45 · 499 阅读 · 0 评论
分享