U盘代码总结

整理U盘发现一些以前的资料,记录一下吧


43.递归和非递归俩种方法实现二叉树的前序遍历。
ANSWER
void preorderRecursive(TreeNode * node) {
  if (node == NULL) return;
  visit(node);
  preorderRecursive(node->left);
  preorderRecursive(node->right);
}

For non-recursive traversals, a stack must be adopted to replace the implicit program stack in recursive programs.

void preorderNonrecursive(TreeNode * node) {
  stack<TreeNode *> s;
  s.push(node);
  while (!s.empty()) {
    TreeNode * n = s.pop();
    visit(n);
    if (n->right!=NULL) s.push(n->right);
    if (n->left!=NULL) s.push(n->left);
  }
}

void inorderNonrecursive(TreeNode * node) {
  stack<TreeNode *> s;
  TreeNode * current = node;
  while (!s.empty() || current != NULL) {
    if (current != NULL) {
      s.push(current);
      current = current->left;
    } else {
      current = s.pop();
      visit(current);
      current = current->right;
    }
  }
}

Postorder nonrecursive traversal is the hardest one. However, a simple observation helps that the node first traversed is the node last visited. This recalls the feature of stack. So we could use a stack to store all the nodes then pop them out altogether.
This is a very elegant solution, while takes O(n) space. 
Other very smart methods also work, but this is the one I like the most.

void postorderNonrecursive(TreeNode * node) {
  // visiting occurs only when current has no right child or last visited is his right child
  stack<TreeNode *> sTraverse, sVisit;
  sTraverse.push(node);
  while (!sTraverse.empty()) {
    TreeNode * p = sTraverse.pop();
    sVisit.push(p);
    if (p->left != NULL) sTraverse.push(p->left);
    if (p->right != NULL) sTraverse.push(p->right);
  }
  while (!sVisit.empty()) {
    visit(sVisit.pop);
  }
}


微软亚院之编程判断俩个链表是否相交
给出俩个单向链表的头指针,比如h1,h2,判断这俩个链表是否相交。
为了简化问题,我们假设俩个链表均不带环。
问题扩展:
1.如果链表可能有环列?
2.如果需要求出俩个链表相交的第一个节点列?
ANSWER:
struct Node {
  int data;
  int Node *next;
};
// if there is no cycle.
int isJoinedSimple(Node * h1, Node * h2) {
  while (h1->next != NULL) {
    h1 = h1->next;
  }
  while (h2->next != NULL) {
    h2 = h2-> next;
  }
  return h1 == h2;
}

// if there could exist cycle
int isJoined(Node *h1, Node * h2) {
  Node* cylic1 = testCylic(h1);
  Node* cylic2 = testCylic(h2);
  if (cylic1+cylic2==0) return isJoinedSimple(h1, h2);
  if (cylic1==0 && cylic2!=0 || cylic1!=0 &&cylic2==0) return 0;
  Node *p = cylic1;
  while (1) {
    if (p==cylic2 || p->next == cylic2) return 1;
    p=p->next->next;
    cylic1 = cylic1->next;
    if (p==cylic1) return 0;
  }
}

Node* testCylic(Node * h1) {
  Node * p1 = h1, *p2 = h1;
  while (p2!=NULL && p2->next!=NULL) {
    p1 = p1->next;
    p2 = p2->next->next;
    if (p1 == p2) {
      return p1;
    }
  }
  return NULL;


1:手工试下==http://tieba.baidu.com/p/2826016538
#include<stdio.h>
#include<stdlib.h>
void QuickSort(int *A, int left, int right)
{
	
      if (left >= right) return;
	int x = A[(left + right) >> 1],
 low = left, high = right;
	
while (low<high)
	
{
	
	while (A[low]<x)
	
		low++;
	
	while (A[high]>x)
	
		high--;
		
if (low <= high)
		{
	
		int Temp = A[low];
	
		A[low] = A[high];
		A[high] = Temp;
	
		low++;
		
	high--;
		
}
	
}
	
QuickSort(A, left, high);
	QuickSort(A, low, right);
}

int main()
{
	
int length, i, r[10000];
	scanf("%d", &length);
	for (i = 1; i <= length; i++)
        scanf("%d", &r[i]);
	QuickSort(r, 1, length);
	for (i = 1; i <= length; i++)
	
	printf(" %d", r[i]);
	printf("\n");
	
system("pause");
	
return 0;
}

2:用算法
#include<iostream>
#include<cstdlib>
using namespace std;
int cmp(const void *a, const void *b)
{
	return(*(int*)a > *(int*)b);
}
int main()
{
	int s[100];
	int m;
	while (1)
	{
		cin >> m;
		for (int i = 0; i < m; i++)
			cin >> s[i];
		qsort(s, m, sizeof(s[0]), cmp); 
                qsort(s,m,sizeof(s[0]),cmp);
int cmp(const void *a,const void *b)
{
return *(int*)a-*(int *)b;
}


		for (int j = 0; j < m; j++)
			cout << s[j] << " ";
		cout << "\n";
		memset(s, 0, sizeof(int)*m);
	}
	system("pause");
	return 0;
}


3:用结构体

#include<iostream> 
#include<cstdlib>
using namespace std;
struct node
{
	double data;
	
int no;
}s[100];

int i, n;
int cmp(const void *a, const void *b)
{
	
        struct node *aa = (node *)a;
	struct node *bb = (node *)b;
	if (aa->data != bb->data)
	
	return(((aa->data)>(bb->data)) ? 1 : -1);
	
else
		
return((aa->no) - (bb->no));
}

int main()
{
	
cin >> n;
	
for (i = 0; i<n; i++)
	{
		s[i].no = i + 1;
		cin >> s[i].data;
	}
	
qsort(s, n, sizeof(s[0]), cmp);
	
for (i = 0; i<n; i++)
	
	printf("%d %lf\n", s[i].no, s[i].data);
	printf("\n");
	
system("pause");
	
return 0;
}

2.设计包含min 函数的栈。
定义栈的数据结构,要求添加一个min 函数,能够得到栈的最小元素。
要求函数min、push 以及pop 的时间复杂度都是O(1)。
struct MinStackElement {
  int data;
  int min;
};

struct MinStack {
  MinStackElement * data;
  int size;
  int top;
}

MinStack MinStackInit(int maxSize) {
  MinStack stack;
  stack.size = maxSize;
  stack.data = (MinStackElement*) malloc(sizeof(MinStackElement)*maxSize);
  stack.top = 0;
  return stack;
}
void MinStackFree(MinStack stack) {
  free(stack.data);
}
void MinStackPush(MinStack stack, int d) {
  if (stack.top == stack.size) error(“out of stack space.”);
  MinStackElement* p = stack.data[stack.top];
  p->data = d;
  p->min = (stack.top==0?d : stack.data[top-1]);
  if (p->min > d) p->min = d;
  top ++;
}
int MinStackPop(MinStack stack) {
  if (stack.top == 0) error(“stack is empty!”);
  return stack.data[--stack.top].data;
}
int MinStackMin(MinStack stack) {
  if (stack.top == 0) error(“stack is empty!”);
  return stack.data[stack.top-1].min;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值