剑指offer(1/11)

复杂链表的复制

输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针random指向一个随机节点),请对此链表进行深拷贝,并返回拷贝后的头结点。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)

//下面那段代码思维太混乱了,大家不要参考,如果要用map解决此题,看这段代码就好
import java.util.*;
public class Solution {
    public RandomListNode Clone(RandomListNode pHead)
    {
        HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
        RandomListNode p = pHead;
        //第一次遍历 新建立节点
        while(p != null){
            RandomListNode newNode = new RandomListNode(p.label);
            map.put(p, newNode);
            p = p.next;
        }
        //第二次遍历 赋值映射关系
        p = pHead;
        while(p != null){
            RandomListNode node = map.get(p);
            node.next = (p.next == null)?null: map.get(p.next);
            node.random = (p.random == null)?null: map.get(p.random);
            p = p.next;
        }
        //最后的返回值
        return map.get(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)
    {
        if(pHead=NULL) return pHead;//如果是空,那么的话返回本身也就是返回空
        RandomListNode *cur=pHead;//用结构体建造两个指针一个当前节点,一个临时存下一个
        RandomListNode *nex=NULL;
        //复制元素放入链表中
        while(cur){
            RandomListNode * clone= new RandomListNode(cur->label);
            nex=cur->next;//临时存当前A的下一个即B
            cur->next=clone;//当前A指向复制的A1
            clone->next=nex;//A1的下一个指向B
            cur=nex;//将复制的插入到了链表中,那么A跳到下一个即B
        }
        //重新遍历,复制原链表的随机指针
        cur=pHead;
        while(cur){
            nex=cur->next;
            nex->random = cur->random ? cur->random->next : cur->random;
            /*
            if(cur->random)//不能反回节点引用,报错
                nex->random=cur->random->next;
            else
                nex->random=cur->random;
                
            cur=nex->next;
        }
        //重新遍历拆分链表
        cur= pHead;
        RandomListNode *newhead=cur->next;
        while(cur){
            nex=cur->next;
            cur->next=nex->next;
            cur=nex->next;
            nex->next=cur ? cur->next:cur;
        }
        return newhead;
    }
};
*/
class Solution {
public:
    RandomListNode* Clone(RandomListNode* pHead)
    {
        if(!pHead)
            return nullptr;
        RandomListNode* tmp = pHead;
        while(tmp){
            RandomListNode* nd = new RandomListNode(tmp -> label);
            RandomListNode* nt = tmp -> next;
            tmp -> next = nd;
            nd -> next = nt;
            tmp = nt;
        }
        for(RandomListNode* nh = pHead; nh; nh = nh -> next -> next){
            if(nh -> random)
                nh -> next -> random = nh -> random -> next;
        }
        RandomListNode* dummy = new RandomListNode(0);
        RandomListNode* nx = dummy;
        for(RandomListNode* nd = pHead; nd; ){
            nx -> next = nd -> next;
            nx = nx -> next;
            nd -> next = nx -> next;
            nd = nd -> next;
        }
        return dummy -> next;
    }
};

二叉搜索树与双向链表

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

import java.util.ArrayList;
public class Solution {
    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree == null){
            return null;
        }
        ArrayList<TreeNode> list = new ArrayList<>();
        Inorder(pRootOfTree,list);
        return Convert(list);
    }
    public void Inorder(TreeNode node,ArrayList<TreeNode> list){
        if(node.left != null){
            Inorder(node.left,list);
        }
        list.add(node);
        if(node.right != null){
            Inorder(node.right,list);
        }
    }
    public TreeNode Convert(ArrayList<TreeNode> list){
        for(int i = 0; i < list.size()-1; i++){
            list.get(i).right = list.get(i+1);
            list.get(i+1).left = list.get(i);
        }
        return list.get(0);
    }
}
class Solution {
public:
    TreeNode* Convert(TreeNode* pRootOfTree)
    {
        if(pRootOfTree == nullptr) return nullptr;
        TreeNode* pre = nullptr;
        convertHelper(pRootOfTree, pre);
        TreeNode* res = pRootOfTree;
        while(res ->left)
            res = res ->left;
        return res;
    }
    void convertHelper(TreeNode* cur, TreeNode*& pre)
    {
        if(cur == nullptr) return;
        convertHelper(cur ->left, pre);
        cur ->left = pre;
        if(pre) pre ->right = cur;
        pre = cur;
        convertHelper(cur ->right, pre);
    }
};

全排列

输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则按字典序打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。

import java.util.ArrayList;
import java.util.Collections;
public class Solution {
    public ArrayList<String> Permutation(String str) {
       ArrayList<String> list = new ArrayList<String>();
        if(str == null || str.length() == 0){
            return list;
        }
        char[] s = str.toCharArray();
        fun(s,list,0);
        Collections.sort(list);
        return list;
    }
    public void fun(char[] c,ArrayList<String> list,int i){
        if(i == c.length - 1){
            list.add(new String(c));
        }
        for(int j = i ; j < c.length; j++){
            if(j != i && c[j] == c[i]){
                continue;
            }else{
                swap(c,i,j);
                fun(c,list,i+1);
                swap(c,i,j);
            }
        }
    }
    public void swap(char[] c,int i,int j){
        char temp = c[i];
        c[i] = c[j];
        c[j] = temp;
    }
}
class Solution {
public:
    vector<string> result;
    vector<string> Permutation(string str) {
        if(str.length()==0){
            return result;
        }
        Permutation1(str,0);
        sort(result.begin(),result.end());
        return result;
    }
    void Permutation1(string str,int begin){
        if(begin == str.length()){
            result.push_back(str);
            return ;
        }
            for(int i = begin; str[i]!='\0';i++){
                 
                if(i!=begin&&str[begin]==str[i])
                    continue;
                swap(str[begin],str[i]);
                Permutation1(str,begin+1);
                swap(str[begin],str[i]);
            }
             
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值