
剑指offer
liufanlibashanxi
这个作者很懒,什么都没留下…
展开
-
剑指offer :二叉树的前序,中序,后序遍历
1 递归2 非递归class Solution{public://前序遍历//递归实现 void PreOrder(TreeNode *root) { if (root == NULL) { return; } cout << root_val; PreO...原创 2019-01-03 14:55:21 · 195 阅读 · 0 评论 -
剑指offer 20:包含min函数的栈
包含min函数的栈:1 压入:将value压入数据栈中,如果最小栈为空或者value小于最小栈顶,则把value压入最小栈中; 不然,把最小栈顶压入最小栈中2 弹出,两个都弹3 最小栈中栈顶保存的都是数据栈中的最小值 #include <iostream>#include <stack>#include <assert.h>us...原创 2019-01-01 19:53:11 · 159 阅读 · 0 评论 -
剑指offer 59:按之字顺序打印二叉树
按之字顺序打印二叉树:先按层次遍历,保存到数组中,打印的时候,遇到偶数行,反转打印 #include <iostream>#include <algorithm>#include <vector>using namespace std;struct TreeNode{ int val; struct TreeNode ...原创 2019-01-01 14:06:00 · 184 阅读 · 0 评论 -
剑指offer 58 :对称的二叉树
对称的二叉树 #include <iostream>using namespace std;struct TreeNode{ int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x = 0) :val(x), left(NULL...原创 2019-01-01 11:49:59 · 165 阅读 · 0 评论 -
剑指offer 57:二叉树的下一个结点
思路:中序遍历中有右子树,就是其右子树的下一个结点, 比如结点5的下一个结点是7没右子树,是其父亲的左子树,下一个结点是其父亲,比如结点9的下一个结点是4没右子树,是父亲的右子树,下一个节点是当前节点所在左子树根,比如结点2的下一个结点是3#include <iostream>using namespace std;struct TreeLinkNo...原创 2019-01-01 11:10:02 · 124 阅读 · 0 评论 -
剑指offer 24:二叉树中和为某一值的路径
二叉树中和为某一值的路径#include <iostream>#include <vector>using namespace std;using namespace std;struct TreeNode{ int val; TreeNode *left; TreeNode *right; /*TreeNode(int...原创 2018-12-31 15:49:22 · 113 阅读 · 0 评论 -
剑指offer 22:从上往下打印二叉树
从上往下打印二叉树#include <iostream>#include <vector>using namespace std;using namespace std;struct TreeNode{ int val; TreeNode *left; TreeNode *right; TreeNode(int x) ...原创 2018-12-31 09:53:10 · 105 阅读 · 0 评论 -
剑指offer 18:二叉树的镜像
二叉树的镜像先序遍历#include <iostream>#include <stack>#include <algorithm>using namespace std;struct TreeNode{ int val; TreeNode *left; TreeNode *right; TreeNode(in...原创 2018-12-31 09:11:25 · 111 阅读 · 0 评论 -
剑指offer 17:树的子结构
树的子结构 #include <iostream>using namespace std;//树的子结构struct TreeNode{ int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NUL...原创 2018-12-30 21:29:52 · 125 阅读 · 0 评论 -
剑指offer 4 :重建二叉树
重建二叉树:#include <iostream>#include <vector>//重建二叉树using namespace std;struct TreeNode{ int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), ...原创 2018-12-30 20:47:32 · 134 阅读 · 0 评论 -
剑指offer 12 : 数值的整数次方
1 考虑指数和底数不同的条件#include <iostream>#include <cmath>using namespace std;class solution{public: //12 数值的整数次方 double power(double base, int exponent) { if (exponent =...原创 2018-12-30 14:30:43 · 112 阅读 · 0 评论 -
剑指offer: 56 删除链表中重复的节点
#include <iostream>using namespace std;struct ListNode{ int val; struct ListNode *next; ListNode(int x = 0) : val(x),next(NULL) { }};class Solution{...原创 2018-12-30 11:26:14 · 137 阅读 · 0 评论 -
剑指offer 36:两个链表的第一个公共节点
#include <iostream>struct ListNode{public: int val; struct ListNode *next;};using namespace std;class solution{public: //36 找到两个链表的第一个公共节点 //让长的一个链表先走,实现右对齐 ...原创 2018-12-30 10:14:55 · 108 阅读 · 0 评论 -
剑指offer 15:反转链表
#include <iostream>using namespace std;struct ListNode{public: int val; struct ListNode *next;};class Solution{public: ListNode* ReverseList(ListNode* pHead) { L...原创 2018-12-29 21:42:29 · 92 阅读 · 0 评论 -
剑指offer 14:链表中倒数第K个节点
方法:双指针法 right指针先走K步,然后left,right指针一起走,当right = NULL,left指针到倒数第K个节点#include <iostream>#include <stack>#include <vector>using namespace std;struct ListNode{public: int va...原创 2018-12-29 21:01:15 · 130 阅读 · 0 评论 -
剑指offer 3:从尾到头打印链表
利用栈的先进后出特性#include <iostream>#include <stack>#include <vector>using namespace std;struct ListNode{public: int val; struct ListNode *next;};class Solution{publi...原创 2018-12-29 20:57:06 · 100 阅读 · 0 评论 -
剑指offer 44:翻转单词顺序序列
// 翻转单词顺序序列 // 先翻转所有单词,再逐个单词翻转 string ReverseSentence(string str) { if (str.size() == 0) { return " "; } Reverse(str, 0, str.size() - 1); //翻转所有单词 ...原创 2018-12-25 10:19:05 · 228 阅读 · 0 评论 -
剑指offer 43:左旋转字符串
1 两倍串 截取2 三次翻转#include <iostream>#include <string>using namespace std;class Solution{public: //两倍串截取 string LeftRotationString(string str, int n) { if (str....原创 2018-12-25 09:15:53 · 110 阅读 · 0 评论 -
剑指offer 34:第一个只出现一次的字符
遍历#include <iostream>#include <string>using namespace std;class Solution{public: int FirstNotRepeatingChar(string str) { if (str.length() == 0) { retur...原创 2018-12-25 08:19:03 · 114 阅读 · 0 评论 -
剑指offer 2 替换空格
1 遍历2 找到空格后, count+1,len = length + 2*count, 插入字符#include <iostream>#include <vector>using namespace std;class Solution{public: // %20 替换空格 // 遍历 void ReplaceSpace(...原创 2018-12-24 21:16:18 · 93 阅读 · 0 评论 -
剑指offer 50 找到数组中重数的数字
1 先排序,再寻找#include <iostream>#include <vector>using namespace std;class Solution{public: //找到数组中重复的数字 bool duplicate(vector<int> &numbers, int* duplication)...原创 2018-12-24 19:18:34 · 204 阅读 · 0 评论 -
剑指offer 37 数字在排序数组中出现的次数
1 遍历寻找2 二分查找法: 正常二分法 递归二分法 #include <iostream>#include <vector>using namespace std;class Solution{public: // 数字在排序数组中出现的次数 //遍历 int GetNumberOfK(vector<...原创 2018-12-24 16:32:02 · 93 阅读 · 0 评论 -
剑指offer 28 数组中出现次数超过一半的数字
1 先排序 ,再遍历,计数值大于一半,则为那个数2 阵地攻守 ,计数相同加1,不同减1,计数为0,换接下来一个数,计数为1 //数组中出现次数超过一半的数字 int MoreHalfNumber(vector<int> &numbers) { if (numbers.size() == 0) { return 0;...原创 2018-12-24 11:03:34 · 107 阅读 · 1 评论 -
剑指offer 30 : 连续子数组的最大和
1 两次遍历 寻找最大和2 贪心思想,sum为负数,sum为当前值, sum大于0,为sum加当前值; 如果全为负数,找到最大的一个负数//连续子数组的最大和 int FindGreatSum(vector<int> array) { if (array.size() == 0) { return 0; } ...原创 2018-12-24 10:58:00 · 93 阅读 · 0 评论 -
剑指offer:13 调整数组中的偶数到奇数后面
1 冒泡解法 稳定2 辅助数组 稳定3 两个指针 不稳定#include <iostream>#include <vector>using namespace std;class solution{public: //冒泡解法 void reOerderArray(vector<int> &array){ ...原创 2018-12-23 21:21:22 · 76 阅读 · 0 评论