剑指offer每日六题---------day four

剑指offer题19:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,eg. 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

#include<vector>
vector<int> PrintMaterix(vector<vector<int>> matrix)
{
	vector<int> v;

	int up = 0, down = matrix.size();
	int left = 0, right = matrix[0].size();

	while (up < down && left < right)//顺时针打印
	{
		int i = up, j = left - 1;
		while (++j < right)
			v.push_back(matrix[i][j]);//上横
		--j; if (up + 1 == down){ ++up; continue; }

		while (++i < down)
			v.push_back(matrix[i][j]);//右竖
		--i; if (right - 1 == left){ --right; continue; }

		while (--j >= left)
			v.push_back(matrix[i][j]);//下横
		++j;

		while (--i > up)
			v.push_back(matrix[i][j]);//左竖
		++i;

		up++; right--; down--; left++;
	}
	return v;
}

 

剑指offer题20:定义一个栈,使它实现一个min函数,min函数能再O(1)取出栈中最小的数据

#include<stack>
class Stack
{
public:
	void Push(int x)
	{
		s.push(x);
		if (help.empty())
			help.push(x);
		else
		{
			if (x < help.top())
				help.push(x);
			else
				help.push(help.top());
		}
	}
	void pop()
	{
		s.pop();
		help.pop();
	}
	int Min()
	{
		return help.top();
	}
private:
	stack<int> s;
	stack<int> help;
};

 

剑指offer题21:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序

#include<vector>
#include<stack>
bool IsPopOrder(vector<int> pushV, vector<int> popV)
{
	if (pushV.size() != popV.size())return false;

	stack<int> sta;
	size_t pop_index = 0;
	for (size_t i = 0; i < pushV.size(); ++i)
	{
		if (pushV[i] != popV[pop_index])
			sta.push(pushV[i]);
		else
			++pop_index;
	}
	while (!sta.empty() && sta.top() == popV[pop_index++])
		sta.pop();

	return sta.empty() ? true : false;
}

 

剑指offer题22:层序遍历一棵二叉树

#include<queue>
vector<int> PrintFromTopToBottom(TreeNode* root)
{
	if (!root)return vector<int>();

	vector<int> dst;
	queue<TreeNode*> q;
	q.push(root);
	while (!q.empty())
	{
		TreeNode *front = q.front();
		dst.push_back(front->val);
		q.pop();
		if (front->left)q.push(front->left);
		if (front->right)q.push(front->right);
	}
	return dst;
}

 

剑指offer题23:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。(数组中任意两个数都不相同)
  方法一:递归,对于二叉搜索树的后序遍历结果,去掉该数组中的最后一个数据(也就是根),那么数组分成两部分,
                 前部分是左子树,后部分是右子树
  方法二:非递归,基于递归思想写成非递归,O(n^2)

//方法一:
bool Judge(vector<int>& post, int left, int right)
{
	if (left >= right)return true;

	bool flag = false;
	int li = left, ri = right - 1;
	while (li < right && post[li] < post[right])++li;
	while (ri >= left && post[ri] > post[right])--ri;
	if (li > ri)flag = true;

	return flag
		&& Judge(post, left, ri)
		&& Judge(post, li, right - 1);
}
bool VerifySquenceOfBST(vector<int> post)
{
	if (post.empty())return false;

	return Judge(post, 0, post.size() - 1);
}

方法二:
bool VerifySquenceOfBST2(vector<int> post)
{
	int size = post.size();
	while (--size)
	{
		int count = size, index = size - 1;
		while (index >= 0 && post[index] > post[size]){ --count; --index; }
		while (index >= 0 && post[index] < post[size]){ --count; --index; }

		if (count != 0)return false;
	}
	return true;
}

 

剑指offer题24:输入一棵二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。
             路径定义为数的根结点开始往下一直到叶节点所经过的结点形成一条路径

void _FindPath(TreeNode *root, vector<int>&res,vector<vector<int>> &dst,int sum, int target)
{
	if (!root)return;

	vector<int> res_tmp = res;
	res_tmp.push_back(root->val);
	if ((sum += root->val) == target && !root->left && !root->right)
		dst.push_back(res_tmp);
	if (sum <= target)
		_FindPath(root->left, res_tmp, dst, sum, target);
	if (sum <= target)
		_FindPath(root->right, res_tmp, dst, sum, target);
}
vector<vector<int>> FindPath(TreeNode* root, int target)
{
	vector<int> res;
	vector<vector<int>>dst;
	int sum = 0;
	_FindPath(root, res, dst, sum, target);
	return dst;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值