劍指offer java實現合集(4)第16~20題

16.合併兩個排序的鏈表

依次比較兩個鏈表當前節點的值,小的就插入到後年,如果有任何一條鏈表走到了結尾,就直接加載另外一條鏈表剩餘的節點。

注意要提前保存頭結點。

public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        ListNode first = new ListNode(-1);
        ListNode cur = first;
        while(list1!=null&&list2!=null){
            if(list1.val<list2.val){
                first.next = list1;
                first = first.next;
                list1 = list1.next;
            }else{
                first.next = list2;
                first = first.next;
                list2 = list2.next;
            }
        }
        while(list1!=null){
            first.next = list1;
            first = first.next;
            list1 = list1.next;
        }
         while(list2!=null){
            first.next = list2;
            first = first.next;
            list2 = list2.next;
        }
        return cur.next;
    }
}

17.樹的子結構

寫一個遞歸比較的方法,每次找到與B樹根節點的值相同的位置,就調用該方法進行判斷。

public class Solution {
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        if(root2==null){
            return false;
        }
        if(root1==null&&root2!=null){
            return false;
        }
        boolean flag = false;
        if(root1.val==root2.val){
            flag = SameTree(root1,root2);
        }
        if(!flag){
            flag = HasSubtree(root1.left,root2);
        }
        if(!flag){
            flag = HasSubtree(root1.right,root2);
        }
        return flag;
    }
    public boolean SameTree(TreeNode root1,TreeNode root2){
        if(root2==null){
            return true;
        }
        if(root1==null){
            return false;
        }
        if(root1.val ==root2.val){
            return SameTree(root1.left,root2.left)&&SameTree(root1.right,root2.right);
        }else{
            return false;
        }
    }
}

18.二叉樹的鏡像

類似於兩個數交換順序,只不過這次是樹的左右節點。

public class Solution {
    public void Mirror(TreeNode root) {
        if(root==null){
          return ;
        }
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        Mirror(root.left);
        Mirror(root.right);
    }
}

19.順時針打印數組

從最外圈向裏打印,選擇左上角和右下角作爲參考,注意每一個拐點的要求。

import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printMatrix(int [][] matrix) {
        ArrayList<Integer> res = new ArrayList();
        if(matrix==null||matrix.length==0){
            return res;
        }
        int srow = 0;
        int sclu = 0;
        int erow = matrix.length-1;
        int eclu = matrix[0].length-1;
        while(srow<=erow&&sclu<=eclu){
            getres(matrix,res,srow++,sclu++,erow--,eclu--);
        }
        return res;
    }
    public void getres(int [][]num,ArrayList<Integer> res,int srow,int sclu,int erow,int eclu){
        if(srow==erow){
            for(int i =sclu;i<=eclu;i++){
                res.add(num[srow][i]);
            }
        }else if(sclu==eclu){
            for(int i =srow;i<=erow;i++){
                res.add(num[i][sclu]);
            }
        }else{
            int tclu = sclu;
            int trow = srow;
            while(tclu!=eclu){
                res.add(num[srow][tclu]);
                tclu++;
            }
            while(trow!=erow){
                res.add(num[trow][eclu]);
                trow++;
            }
            while(tclu!=sclu){
                res.add(num[erow][tclu]);
                tclu--;
            }
            while(trow!=srow){
                res.add(num[trow][sclu]);
                trow--;
            }
        }
    }
}

20.包含min函數的棧

import java.util.Stack;

public class Solution {

     Stack<Integer> st1 = new Stack();
    Stack<Integer> st2 = new Stack();
    public void push(int node) {
        if(st1.isEmpty()){
            st1.push(node);
            st2.push(node);
        }else{
            st1.push(node);
            if(node<st2.peek()){
                st2.push(node);
            }
        }
        
    }
    
    public void pop() {
        if(st1.peek()==st2.peek()){
            st1.pop();
            st2.pop();
        }else{
            st1.pop();
        }
    }
    
    public int top() {
        return st1.peek();
    }
    
    public int min() {
        return st2.peek();
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章