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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39290388/article/details/81876022

剑指offer题25:复制一个随机链表

RondomListNode* RandomLinkCopy(RondomListNode *head)
{
	if (!head)return NULL;//头结点是空直接返回

	RondomListNode *cur = head, *next = head->next;//由①②③变成①①②②③③,但复制出来的结点的随机指针都指向空
	while (cur){
		RondomListNode *tmp = new RondomListNode(cur->val);
		cur->next = tmp; tmp->next = next;
		cur = next; if (next)next = next->next;
	}
	cur = head; next = cur->next;//将随机指针指向正确的位置
	while (cur){
		next->rand = cur->rand ? cur->rand->next : NULL;
		cur = next->next; if (cur)next = cur->next;
	}
	cur = head; next = cur->next;//将新旧链表分离开
	RondomListNode *tmp = next;
	while (cur){
		cur->next = next->next; cur = next->next;
		if (cur){ next->next = cur->next; next = cur->next; }
		else next->next = NULL;
	}
	return tmp;//返回复制出来的新链表的头结点
}

剑指offer题26:输入一棵二叉搜索树,将该二叉树转换成一个排序的双向链表。要求不能创建新的结点,只能调整树中结点的指针

TreeNode *_TreeToLink(TreeNode *root)
{
	if (!root)return NULL;//递归返回条件

	TreeNode *left = _TreeToLink(root->left);//left是左边单链表的最左结点
	TreeNode *right = _TreeToLink(root->right);//right是右边单链表的最左结点

	TreeNode *left_to_right;//左边单链表的最右结点
	left_to_right = left ? left->left : NULL;
	TreeNode *right_to_right;//右边单链表的最右结点
	right_to_right = right ? right->left : NULL;

	root->left = left_to_right;//根的左孩子
	if (left_to_right)left_to_right->right = root;
	root->right = right;//根的右孩子
	if (right)right->left = root;

	if (!left)left = root;
	left->left = right ? right_to_right : root;

	return left;
}
TreeNode *TreeToLink(TreeNode *root)//递归思路
{
	if (!root)return NULL;

	TreeNode *head = _TreeToLink(root);
	head->left = NULL;
	return head;
}

剑指offer题28:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字,ps:如果不存在返回0.
  方法一:排序    方法二:map<K,V>    方法三:阵地攻守思想,先让第一个元素作为第一个士兵来守阵地,count=1;
   遇到相同的元素++count,否则--count;当count=0时,又以新的i值作为守阵地的士兵,直到最后的士兵,那即是所求元素
 方法四:用堆找出一般元素,返回堆顶    

int MoreThanHalfNum(vector<int> arr)//方法三
{
	if (arr.empty())return 0;

	int count = 1, soldier = arr[0];
	for (int i = 1; i < arr.size(); ++i)
	{
		arr[i] == soldier ? ++count : --count;

		if (count == 0)
		{
			soldier = arr[i];
			count = 1;
		}
	}

	count = 0;
	for (int i = 0; i < arr.size(); ++i)
	{
		if (arr[i] == soldier)
			++count;
	}

	return count > arr.size() / 2 ? soldier : 0;
}

剑指offer题29:输入n个整数,找出其中最小的k个数

#include<queue>
vector<int> GetLeastNumbers(vector<int> input, int k)
{
	if (input.size() < k || k <= 0)return vector<int>();//特别注意k的取值

	priority_queue<int> pq;//找最小的前k个数,建大根堆 (孩子 less 双亲)
	for (int i = 0; i < input.size(); ++i)
	{
		if (i < k)
			pq.push(input[i]);
		else if (pq.top() > input[i])
		{
			pq.pop();
			pq.push(input[i]);
		}
	}
	vector<int> arr(k); int index = k - 1;
	while (!pq.empty())
	{
		arr[index--] = pq.top();
		pq.pop();
	}
	return arr;
}

剑指offer题30:输入一个数组,求该数组的最大子数组和是多少。ps,该数组长度最少为1.

int FindGreatestSumOfSubArray(vector<int> arr)
{
	int max = arr[0], val = arr[0];
	for (size_t i = 1; i < arr.size(); ++i)
	{
		val = val < 0 ? 0 : val;//确定val是保留原值还是置零
		val += arr[i];
		if (val > max)max = val;//max代表原数组[0,i]范围内的最大子数组加和
	}
	return max;
}

猜你喜欢

转载自blog.csdn.net/qq_39290388/article/details/81876022
今日推荐