剑指offer 算法 (分解让复杂问题简单)

题目描述

输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点)。

解析:
分三步:
第一步:cloneList(pHead);复制链表,每个节点后新连接一个复制的自己的节点,random先置空;
第二步:connectRandom(pHead);依次给每个新节点的random连接
第三步:return apartList(pHead);把新建的结点按链表分离出来

/*
struct RandomListNode {
    int label;
    struct RandomListNode *next, *random;
    RandomListNode(int x) :
            label(x), next(NULL), random(NULL) {
    }
};
*/
class Solution {
public:
    RandomListNode* Clone(RandomListNode* pHead)
    {
        cloneList(pHead);
        connectRandom(pHead);
        return apartList(pHead);
    }
    void cloneList(RandomListNode* pHead)
    {
        RandomListNode* node;
        node=pHead;
        while(node!=NULL)
        {
            RandomListNode* newNode;
            newNode=(RandomListNode*)malloc(sizeof(RandomListNode));
            newNode->label=node->label;
            newNode->random=NULL;
            newNode->next=node->next;
            node->next=newNode;
            node=newNode->next;
        }
    }
    void connectRandom(RandomListNode* pHead)
    {
        RandomListNode* node;
        node=pHead;
        while(node!=NULL)
        {
            RandomListNode* newNode;
            newNode=(RandomListNode*)malloc(sizeof(RandomListNode));
            newNode=node->next;
            if(node->random!=NULL)
            {
                newNode->random=node->random->next;
            }
            node=newNode->next;
        }
    }
    RandomListNode* apartList(RandomListNode* pHead)
    {
        RandomListNode* node=pHead;
        RandomListNode* cloneNode=NULL;
        RandomListNode* cloneList=NULL;
        if(node!=NULL)
        {
            cloneNode=node->next;
            cloneList=cloneNode;
            node->next=cloneList->next;
            node=node->next;
        }
		while(node!=NULL)
		{
            cloneNode->next=node->next;
            cloneNode=cloneNode->next;
            node->next=cloneNode->next;
            node=node->next;
        }
        return cloneList;
     }
};

题目描述

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

解析:根据二叉搜索树的特点,中序遍历正好由树的最小值搜到最大值;每当搜到一个点,加入链表;用lastNode保存当前的链表尾。

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    TreeNode* Convert(TreeNode* pRootOfTree)
    {
        TreeNode* lastNode=NULL;//链表尾
        convertList(pRootOfTree,&lastNode);
        //递归完lastHead为链表尾 so向左不断搜索直到链表头
        TreeNode* listHead=lastNode;
        while(listHead!=NULL&&listHead->left!=NULL)
            listHead=listHead->left;
        return listHead;
    }
    void convertList(TreeNode* pRootOfTree,TreeNode** lastNode)
    {
        if(pRootOfTree==NULL)
            return;
        TreeNode* current=pRootOfTree;
        if(current->left)
            convertList(current->left,lastNode);
        current->left=*lastNode;
        if(*lastNode!=NULL)
            (*lastNode)->right=current;
        *lastNode=current;//更新lastNode
        if(current->right)
            convertList(current->right,lastNode);
    }
};

题目描述

输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。]

解析:字符串逐个逐个字母替换,每个字母替换时逐个循环未使用的字符

class Solution {
public:
    vector<string> Permutation(string str) {
        vector<string> v;
        if(str.empty())
            return v;
        Permutation(v,str,0);
        return v;
    }
    void Permutation(vector<string> &v,string str,int cnt)
    {
        int size=str.size();
        if(cnt==str.size()-1)
            v.push_back(str);
        else
        {
            Permutation(v,str,cnt+1);
            for(int i=cnt+1;i<size;i++)
            {
                if(str[i]!=str[cnt])
                {
                    char ch=str[i];
                    str[i]=str[cnt];
                    str[cnt]=ch;
                    Permutation(v,str,cnt+1);
                }
            }
        }
    }
};


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值