二维数组查找、替换空格、从头到尾打印链表、重建二叉树(剑指offer1-4)c++版

代码,全部可以跑通,转载请表明出处。

#include<iostream>
#include<vector>
#include<cstring>

void test1();
void test2();
void test3();

using namespace std;

struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
		val(x), next(NULL) {
		
	}	
};
struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
	//JZ1.二维数组的查找
	bool Find(int target, vector<vector<int> > array);
	//JZ2.替换空格
	void replaceSpace(char* &str, int length);
	//JZ3.从尾到头打印链表
	vector<int> JZ3;
	vector<int> printListFromTailToHead(ListNode* head);
	void printListFromTaiToHeadCore(ListNode* head);
	//JZ4.重建二叉树
	TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> vin);
	TreeNode* reConstructBinaryTreeCore(vector<int> pre, int startpre, int endpre, vector<int> vin, int startvin, int endvin);

};

bool Solution::Find(int target, vector<vector<int> > array) {
	//方法1.用hash_map,缺点:没有用到排序的性质
	//方法2.寻找规律。从矩阵的右上角或者左下角开始遍历,以右上角x = array[row][col]为例。
	//若x == target, true;若x > target,则col--;若x < target,则row++
	if (target == NULL || array.empty())	return false;
	int cols = array[0].size();
	int rows = array.size();
	int col = cols - 1, row = 0;
	bool result = false;
	while (row < rows && col >= 0) {
		if (target == array[row][col]) {
			result = true;
			break;
		}
		else if (target < array[row][col])	col--;
		else
		{
			row++;
		}
	}
	return result;
}
void Solution::replaceSpace(char* &str, int length) {
	//先计算空格的长度,再从后往前移动字符并填充空格
	if (!str || length <= 0)	return;
	int i = 0, count = 0;
	int len = strlen(str) + 1;
	while (i < len) {
		if (str[i] == ' ')	count++;
		i++;
	}
	for (i = len - 1; i >= 0; i--) {//char*表示的字符串以/0结尾
		if (str[i] != ' ')	str[i + 2 * count] = str[i];
		else {
			str[i + 2 * count] = '0';
			str[i + 2 * count - 1] = '2';
			str[i + 2 * count - 2] = '%';
			count--;
		}
	}
	return;
}
vector<int> Solution::printListFromTailToHead(ListNode* head) {
	//方法1.栈的思路。简单,不赘述。方法2.递归的方法
	if (!head)	return vector<int>();
	printListFromTaiToHeadCore(head);
	return JZ3;	
}
void Solution::printListFromTaiToHeadCore(ListNode* head) {
	if (!head)	return;
	printListFromTaiToHeadCore(head->next);
	JZ3.push_back(head->val);
	return;
}
TreeNode* Solution::reConstructBinaryTree(vector<int> pre, vector<int> vin) {
	//前序遍历的第一个值x为根节点,在中序遍历中找到x,则x前部分为左子树,后部分为右子树;
	//分别记录左子树的长度和右子树的长度,找出其前序遍历,递归求解。
	int len1 = pre.size(), len2 = vin.size();
	if (len1 <= 0 || len1 != len2)	return nullptr;
	return reConstructBinaryTreeCore(pre, 0, len1 - 1, vin, 0, len1 - 1);
}
TreeNode* Solution::reConstructBinaryTreeCore(vector<int> pre, int startpre, int endpre, vector<int> vin, int startvin, int endvin) {
	if (startpre > endpre || startvin > startvin)	return nullptr;
	TreeNode* pHead = new TreeNode(pre[startpre]);
	int index = startvin;//记录根节点在中序遍历中的位置
	for (index; index < endvin; index++) {
		if (vin[index] == pre[startpre])	break;
	}
	pHead->left = reConstructBinaryTreeCore(pre, startpre + 1, index - startvin + startpre, vin, startvin, index - 1);
	pHead->right = reConstructBinaryTreeCore(pre, index - startvin + startpre + 1, endpre, vin, index + 1, endvin);
	return pHead;
}

void test1(){
	int target = 2;
	vector<vector<int> > test;
	test.push_back({ 1, 2, 3 });
	test.push_back({ 2, 4, 5 });
	Solution s;
	bool result = s.Find(target, test);
	if (result)	cout << 1;
	else
	{
		cout << 0;
	}
	return;
}
void test2() {
	int length = 50;
	char* str = new char[length];
	memset(str, '/0', length);//将str初始化
	char* temp = "abc da";
	strcpy_s(str, strlen(temp) + 1, temp);
	Solution s;
	s.replaceSpace(str, length);
	for (int i = 0; i < strlen(str) + 1; i++) {
		cout << str[i];
	}
	delete[] str;
	return;
}
void test3() {
	ListNode* head = new ListNode(1);
	head->next = new ListNode(2);
	head->next->next = new ListNode(3);
	Solution s;
	vector<int> result = s.printListFromTailToHead(head);
	vector<int>::iterator it;
	for (it = result.begin(); it != result.end(); it++)
		cout << *it << endl;
	return;
}
void test4() {
	vector<int> pre = { 1,2,4,5,3,6 };
	vector<int> vin = { 4,2,5,1,6,3 };
	Solution s;
	TreeNode* result = s.reConstructBinaryTree(pre, vin);
	cout << result->val << endl;
	cout << result->left->val << ' ' << result->right->val;
	return;
}


int main() {
	//test1();
	//test2();
	//test3();
	//test4();
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值