劍指offer-21~25

21.棧的壓入、彈出序列
輸入兩個(gè)整數(shù)序列,第一個(gè)序列表示棧的壓入順序,請(qǐng)判斷第二個(gè)序列是否為該棧的彈出順序。假設(shè)壓入棧的所有數(shù)字均不相等。例如序列1,2,3,4,5是某棧的壓入順序,序列4,5,3,2,1是該壓棧序列對(duì)應(yīng)的一個(gè)彈出序列,但4,3,5,1,2就不可能是該壓棧序列的彈出序列。

import java.util.ArrayList;

public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        if(pushA==null || popA==null){
            return false;
        }else if(pushA.length==0 ||popA.length==0){
            return false;
        }
        
        ArrayList<Integer> list =new ArrayList<Integer>();
        
        int push = pushA[0];
        int pop = popA[0];
        int i=0,j=0;
        while(true){
            if(push==pop){
                //優(yōu)先從隊(duì)列中取出一個(gè)
                if(list.size()!=0){
                    push=list.remove(list.size()-1);
                }else{
                    i++;//如果隊(duì)列里面沒有,就從后面拿一個(gè)
                    if(i==pushA.length){
                        i=i-1;
                        break;
                    }
                    push=pushA[i];
                }
                j++;
                if(j==popA.length){
                    break;
                }
                pop=popA[j];
            }else{
                list.add(push);
                i++;
                if(i==pushA.length){
                    break;
                }
                push=pushA[i];
            }
        }
        
        if(j==popA.length-1&&i==pushA.length-1){
            if(list.size()==0){
                return true;
            }else{
                return false;
            }
        }
        return false;
        
    }
}

22.從上往下打印二叉樹
從上往下打印出二叉樹的每個(gè)節(jié)點(diǎn),同層節(jié)點(diǎn)從左至右打印。

import java.util.ArrayList;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        ArrayList<TreeNode> list = new ArrayList<TreeNode>();
        ArrayList<Integer> res = new ArrayList<Integer>();
        if(root!=null){
            list.add(root);
        }
        while(!list.isEmpty()){
            TreeNode tmp = list.remove(0); 
            TreeNode left =tmp.left;
            TreeNode right = tmp.right;
            res.add(tmp.val);
            if(left!=null){
                list.add(left);
            }
            if(right!=null){
                list.add(right);
            }
        }
        return res;
        
    }
}

23.二叉搜索樹的后序遍歷序列
輸入一個(gè)整數(shù)數(shù)組,判斷該數(shù)組是不是某二叉搜索樹的后序遍歷的結(jié)果。如果是則輸出Yes,否則輸出No。假設(shè)輸入的數(shù)組的任意兩個(gè)數(shù)字都互不相同。

public class Solution {
    public boolean VerifySquenceOfBST(int [] sequence) {
        if(sequence==null || sequence.length==0){
            return false;
        }
        return check(sequence,0,sequence.length-1);
    }
    
    public boolean check(int[] array,int start,int end){
        if(start>=end){
            return true;  
        } 
        int size = end -start ;//最后一個(gè)不要跟自己比,去掉最后一個(gè)
        int endVal =array[end];
        //找出第一個(gè)比endVal大的位置
        int mark=-1;
        for(int i =start;i< size;i++){
            if(array[i]>endVal){
                mark=i;
                break;
            }
        }
        
        if(mark==-1){//都比endVal小
            if(check(array,start,end-1)){
                return true;
            }
        }else{ 
            //mark之后的數(shù)都要比val大
        
            for(int i=mark;i<size;i++){
                if(array[i]<endVal){
                    return false;
                }
            }
            if(check(array,start,mark-1) && check(array,mark,end-1)){
            return true;
            }
        }
        return false;
        
    }
}

24.二叉樹中和為某一值的路徑
輸入一顆二叉樹和一個(gè)整數(shù),打印出二叉樹中結(jié)點(diǎn)值的和為輸入整數(shù)的所有路徑。路徑定義為從樹的根結(jié)點(diǎn)開始往下一直到葉結(jié)點(diǎn)所經(jīng)過(guò)的結(jié)點(diǎn)形成一條路徑。

import java.util.ArrayList;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
        ArrayList<ArrayList<Integer>> res =travel(new ArrayList<Integer>(),target,root);
        return res;
        
    }
    
    public ArrayList<ArrayList<Integer>> travel(ArrayList<Integer> list,int value,TreeNode root){
        //為空的情況
        
        ArrayList<ArrayList<Integer>> res=new ArrayList<ArrayList<Integer>>();
        if(root==null){
            return res;
        }
        ArrayList<Integer> newList =new ArrayList<Integer>();
        //將list中的內(nèi)容復(fù)制到newlist中
        for(Integer item:list){
            newList.add(item);
        }
        int val = root.val;
        newList.add(val);
        
        int rest = value-val;
        //葉子節(jié)點(diǎn)
        if(root.left==null && root.right==null){
            if(rest==0){
                res.add(newList);
            }else{
                return res;
            }
        }else{//不是葉子節(jié)點(diǎn)
            ArrayList<ArrayList<Integer>>  left = travel(newList,rest,root.left);
            ArrayList<ArrayList<Integer>>  right = travel(newList,rest,root.right);
            //合并兩項(xiàng),并返回
            if(left!=null){
                for(ArrayList<Integer> item:left){
                    res.add(item);
                }
            }
            if(right!=null){
                for(ArrayList<Integer> item:right){
                    res.add(item);
                }
            }
            
        }
        return res;
    }
}

25.復(fù)雜鏈表的復(fù)制
輸入一個(gè)復(fù)雜鏈表(每個(gè)節(jié)點(diǎn)中有節(jié)點(diǎn)值,以及兩個(gè)指針,一個(gè)指向下一個(gè)節(jié)點(diǎn),另一個(gè)特殊指針指向任意一個(gè)節(jié)點(diǎn))。

import java.util.ArrayList;
/*
public class RandomListNode {
    int label;
    RandomListNode next = null;
    RandomListNode random = null;

    RandomListNode(int label) {
        this.label = label;
    }
}
*/
public class Solution {
    public RandomListNode Clone(RandomListNode pHead)
    {
        //如果為空
        if(pHead==null){
            return null;
        }
        //存儲(chǔ)地址
        ArrayList<String> addresses=new ArrayList<String>();
        addresses.add(pHead.toString());
        
        
        
        //記錄頭指針
        RandomListNode first = pHead;
        //記錄頭指針
        RandomListNode newOne = new RandomListNode(pHead.label);
        
        //存儲(chǔ)指針
        ArrayList<RandomListNode> res = new ArrayList<RandomListNode>();
        res.add(newOne);
        
        RandomListNode entity =newOne;
        //int size=1;
        while(pHead.next!=null){
            pHead=pHead.next;
            //獲取當(dāng)前地址
            String address =pHead.toString();
            //if(addresses.contains(address)){//已經(jīng)存在,回路
            //    entity.next = res.get(addresses.indexOf(address));//獲取
            //    break;
            //}
            //size++;
            entity.next = new RandomListNode(pHead.label);
            entity = entity.next;
            res.add(entity);
            addresses.add(pHead.toString());//默認(rèn)是地址
        }   
        
        pHead = first;
        if(pHead.random!=null){
            newOne.random = res.get(addresses.indexOf(pHead.random.toString()));
        }
        entity=newOne;
        //int i=0;
        while(pHead.next!=null /*&& i<size*/){
            pHead=pHead.next;
            entity=entity.next;
            if(pHead.random!=null){
                entity.random = res.get(addresses.indexOf(pHead.random.toString()));
            }
            //i++;
        }
        return newOne;
        
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 總結(jié) 想清楚再編碼 分析方法:舉例子、畫圖 第1節(jié):畫圖分析方法 對(duì)于二叉樹、二維數(shù)組、鏈表等問題,都可以采用畫圖...
    M_巴拉巴拉閱讀 1,230評(píng)論 0 7
  • 說(shuō)明: 本文中出現(xiàn)的所有算法題皆來(lái)自牛客網(wǎng)-劍指Offer在線編程題,在此只是作為轉(zhuǎn)載和記錄,用于本人學(xué)習(xí)使用,不...
    秋意思寒閱讀 1,168評(píng)論 1 1
  • 劍指 offer 在一個(gè)二維數(shù)組中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請(qǐng)完成...
    faremax閱讀 2,241評(píng)論 0 7
  • 刷題啦刷題啦,劍指offer好像比較有名,所以就在牛客網(wǎng)上刷這個(gè)吧~btw,刷了一些題發(fā)現(xiàn)編程之美的題好典型啊!!...
    Cracks_Yi閱讀 432評(píng)論 0 1
  • 劍指offer 最近在牛客網(wǎng)上刷劍指offer的題目,現(xiàn)將題目和答案(均測(cè)試通過(guò))總結(jié)如下: 二維數(shù)組的查找 替換...
    閆阿佳閱讀 951評(píng)論 0 10