![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
剑指offer
KalaerSun
总有许多风景等着你去发现,能做自己曾做不了的,就是最好的生活
展开
-
剑指offer 和为S的两个数字
class Solution {public: vector<int> FindNumbersWithSum(vector<int> array,int sum) { vector<int> result; int i=0; int j=array.size()-1; while(i<...原创 2018-04-09 15:31:28 · 170 阅读 · 1 评论 -
剑指offer 和为S的连续正数序列
class Solution {public: vector<vector<int> > FindContinuousSequence(int sum) { vector<vector<int>> result; int hign=2,low=1; while(hign>low) ...原创 2018-04-09 15:17:01 · 72 阅读 · 0 评论 -
剑指offer 复杂链表的复制
/*struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : label(x), next(NULL), random(NULL) { }};*/class Solution {public: ...原创 2018-03-27 09:44:38 · 82 阅读 · 0 评论 -
剑指offer 数组中只出现一次的数字
class Solution {public: void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) { int Xor=0; for(int i=0;i<data.size();++i) { Xor^=data[i]; ...原创 2018-04-09 13:57:11 · 117 阅读 · 0 评论 -
剑指offer 平衡二叉树
class Solution {public: bool IsBalanced(TreeNode *root,int &depth) { if(root==NULL) { return true; } int left=0; int right=0; i...原创 2018-04-09 12:43:31 · 85 阅读 · 0 评论 -
剑指offer 二叉树这种和为某一值的路径
/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution { void DFS_Fi...原创 2018-03-26 14:56:18 · 121 阅读 · 0 评论 -
剑指offer 二叉搜索树的后序遍历序列
class Solution {public:bool isBSTree(vector<int> sequence,int l,int r) { if(r<=l) { return true; } int i=l; for(;i<r;i++) ...原创 2018-03-26 13:13:13 · 77 阅读 · 0 评论 -
剑指offer 从上往下打印二叉树
/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution {public: vec...原创 2018-03-26 12:52:22 · 77 阅读 · 0 评论 -
剑指offrer 栈的压入,弹出序列
class Solution {public: bool IsPopOrder(vector<int> pushV,vector<int> popV) { if(pushV.empty()||popV.empty()||pushV.size()!=popV.size()) { return false; ...原创 2018-03-26 12:25:58 · 89 阅读 · 0 评论 -
剑指offer 包含min函数的栈
class Solution {public: stack<int> stack1,stack2; void push(int value) { stack1.push(value); if(stack2.empty()) { stack2.push(value); }else i...原创 2018-03-26 12:14:48 · 84 阅读 · 0 评论 -
剑指offer 二叉搜索树与双向链表
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } };*/ class Solution { publi...原创 2018-03-27 10:16:53 · 77 阅读 · 0 评论 -
剑指offer 字符串的排列
class Solution {public: vector<string> Permutation(string str) { //可以用递归来做 &n原创 2018-03-27 10:53:58 · 128 阅读 · 0 评论 -
剑指offer 数组中出现次数超过一半的数字
class Solution {public: int MoreThanHalfNum_Solution(vector<int> numbers) { int n = numbers.size(); &原创 2018-03-27 11:07:22 · 97 阅读 · 0 评论 -
剑指offer 构建乘积数组
class Solution {public: vector<int> multiply(const vector<int>& A) { int length=A.size(); vector<int> B(length,1); if(length!=0) { ...原创 2018-04-12 19:17:43 · 92 阅读 · 0 评论 -
剑指offer 数组中重复的数字
class Solution {public: // Parameters: // numbers: an array of integers // length: the length of array numbers // duplication: (Output) the duplicated nu...原创 2018-04-12 18:39:07 · 113 阅读 · 0 评论 -
剑指offer 把字符串转换成整数
class Solution {public: int StrToInt(string str) { int n=str.size(),s=1; long long res=0; if(!n) return 0; if(str[0]=='-') s=-1; for(int i=(str[0]=='-'||st...原创 2018-04-12 18:00:26 · 164 阅读 · 0 评论 -
剑指offer 不使用加减乘除做加法
class Solution {public: int Add(int num1, int num2) { while(num2!=0) { int temp=num1^num2; num2 = (num1&num2)<<1; num1=temp; ...原创 2018-04-12 17:41:11 · 115 阅读 · 0 评论 -
剑指offer 求1+2+3+4+。。。+n
class Solution {public: int Sum_Solution(int n) { int result=n; result&&(result+=Sum_Solution(n-1)); return result; }};题目要求不能循环判断等关键字 所有借助&& 短路运算符...原创 2018-04-12 17:19:42 · 132 阅读 · 0 评论 -
剑指offer 孩子们的游戏
约瑟夫环的问题class Solution {public: int LastRemaining_Solution(int n, int m) { if(n<1||m<1) return -1; int *array= new int[n];//用数组模拟环 int i=-1,step=0,count=n; ...原创 2018-04-12 17:03:12 · 216 阅读 · 0 评论 -
剑指offer 扑克牌顺子
扑克问题 1 ,大小王是 0 即可看作是任意的数字 先统计0的个数 2,如果有两张一样的牌,一样的数字形成对(不是0)那就不可能是顺子 3,判断相邻两张扑克的差值 如果大于零的个数 则不可能用0补全成为顺子 01456 1与4相差2 但是只有一个0就没法补全class Solution {public: bool IsContinuous( vector<int>...原创 2018-04-12 15:39:50 · 98 阅读 · 0 评论 -
翻转单词序列
class Solution {public: void ReverseWord(string &str,int start,int end) { while(start<end) { swap(str[start++],str[end--]); } } string Reve...原创 2018-04-12 14:57:44 · 153 阅读 · 0 评论 -
左旋转字符串
class Solution {public: string LeftRotateString(string str, int n) { int len = str.length(); if(len==0) return ""; n=n%len; str+=str; return str.substr(n,le...原创 2018-04-12 11:02:22 · 102 阅读 · 0 评论 -
剑指offer 最小的k个数
链接:https://www.nowcoder.com/questionTerminal/6a296eb82cf844ca8539b57c23e6e9bf来源:牛客网class Solution {public: vector<int> GetLeastNumbers_Solution(vector<int> inpu...原创 2018-03-27 11:54:33 · 151 阅读 · 0 评论 -
剑指offer 顺时针打印矩阵
题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10. 题目解释class Solution {public: vector<int> printMa...原创 2018-03-26 11:51:03 · 125 阅读 · 0 评论 -
剑指offer 二叉树的镜像
/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution {public: voi...原创 2018-03-26 11:31:51 · 70 阅读 · 0 评论 -
剑指offer 二叉树的深度
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } };*/ class Solution { publi...原创 2018-04-08 10:50:34 · 80 阅读 · 0 评论 -
剑指offer 数值的整数次方
class Solution {public: double Power(double base, int exponent) { if(exponent == 0) return 1; if(exponent == 1) return base; int x = exponent / 2; if(x > 0) { ...原创 2018-03-01 11:24:47 · 70 阅读 · 0 评论 -
剑指offer 二进制中1的个数
链接:https://www.nowcoder.com/questionTerminal/8ee967e43c2c4ec193b040ea7fbb10b8public class Solution { public int NumberOf1(int n) { int count = 0; while(n!= 0){ count++...原创 2018-03-01 11:03:03 · 97 阅读 · 0 评论 -
剑指offer 矩形覆盖
链接:https://www.nowcoder.com/questionTerminal/72a5a919508a4251859fb2cfb987a0e6public class Solution { public int RectCover(int target) { if(target <= 1){ return 1; }...原创 2018-03-01 00:54:56 · 114 阅读 · 0 评论 -
剑指offer 变态跳台阶
链接:https://www.nowcoder.com/questionTerminal/22243d016f6b47f2a6928b4313c85387来源:牛客网数学归纳法的推理很仔细 可以看一下这个大佬的public class Solution { public int JumpFloorII(int target) { if (target <= 0)...原创 2018-02-28 22:39:25 · 74 阅读 · 0 评论 -
剑指offer 斐波那契数列
class Solution {public: int Fibonacci(int n) { //使用迭代法,用fn1和fn2保存需要不断相加的两个数字 int fn1 = 1; int fn2 = 1; int target=0;//所求的目标值 if (n <= 0) { r...原创 2018-02-28 21:19:30 · 89 阅读 · 0 评论 -
剑指offer 旋转数组的最小数字
class Solution {public: int minNumberInRotateArray(vector<int> rotateArray) { int size = rotateArray.size(); if(size == 0){ return 0; }//if int l...原创 2018-02-28 19:54:32 · 83 阅读 · 0 评论 -
剑指offer 用两个栈实现队列
class Solution{public: void push(int node) { stack1.push(node); } int pop() { int res; while(!stack1.empty()){ stack2.push(stack1.top()); ...原创 2018-02-28 11:08:17 · 77 阅读 · 0 评论 -
剑指offer 重建二叉树
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {publi...原创 2018-02-28 10:56:49 · 83 阅读 · 0 评论 -
剑指offer 从尾到头输出单链表
/*** struct ListNode {* int val;* struct ListNode *next;* ListNode(int x) :* val(x), next(NULL) {* }* };*/class Solution {public: vector<int...原创 2018-02-13 23:12:31 · 85 阅读 · 0 评论 -
剑指offer 空格替换
#include <stdio.h>void replaceSpace(char *str,int length){ int i=0,j=0; while(i<length-1) { if(str[i]==' ') { j++; } i++; } ...原创 2018-02-12 10:26:08 · 158 阅读 · 0 评论 -
剑指offer 调整数组顺序使奇数位于偶数前面
class Solution {public: void reOrderArray(vector<int> &array) { for(int i=0;i<array.size();i++){ if(array[i]%2==1){ int temp=array[i]; ...原创 2018-03-01 15:16:54 · 125 阅读 · 0 评论 -
剑指offer 链表中倒数第k个结点
/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { }};*/class Solution {public: ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) { ...原创 2018-03-01 16:27:41 · 95 阅读 · 0 评论 -
剑指offer 单链表逆序
/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { }};*/class Solution {public: ListNode* ReverseList(ListNode* pHead) { ListNode *prev=N...原创 2018-03-01 18:14:55 · 132 阅读 · 0 评论 -
剑指offer 数字在排序数组中出现的次数
class Solution {public: int GetNumberOfK(vector<int> data ,int k) { int lower = getLower(data,k); int upper = getUpper(data,k); return upper - lower; } int g...原创 2018-04-08 10:43:59 · 81 阅读 · 0 评论