劍指Offer【21-30】Java實現

21、輸入兩個整數序列,第一個序列表示棧的壓入順序,請判斷第二個序列是否可能爲該棧的彈出順序。假設壓入棧的所有數字均不相等。例如序列1,2,3,4,5是某棧的壓入順序,序列4,5,3,2,1是該壓棧序列對應的一個彈出序列,但4,3,5,1,2就不可能是該壓棧序列的彈出序列。(注意:這兩個序列的長度是相等的)

import java.util.ArrayList;
import java.util.Stack;
public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
      Stack<Integer> stack = new Stack<Integer>();
        for(int i=0,j=0;i<pushA.length;i++){
            stack.push(pushA[i]);
            while(stack.size()>0&&stack.peek()==popA[j]){
                stack.pop();
                j++;
            }
        }
        return stack.size()==0;
    }
}

22、從上往下打印出二叉樹的每個節點,同層節點從左至右打印。

import java.util.ArrayList;
import java.util.ArrayDeque;
/**
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<Integer> list = new ArrayList<Integer>();
        if(root == null){
            return list;
        }
        ArrayDeque<TreeNode> queue = new ArrayDeque<TreeNode>();       
        TreeNode node = root;        
        queue.offer(node);
        list.add(node.val);
        while(!queue.isEmpty()){
            node = queue.poll();
            TreeNode left = node.left;
            TreeNode right = node.right;
            if(left!=null){
                queue.offer(left);
                list.add(left.val);
            }
            if(right!=null){
                queue.offer(right);
                list.add(right.val);
            }

        }   
        return list;
    }
}

23、輸入一個整數數組,判斷該數組是不是某二叉搜索樹的後序遍歷的結果。如果是則輸出Yes,否則輸出No。假設輸入的數組的任意兩個數字都互不相同。

import java.util.Arrays;

/**
 * 
 * @author zhx
 * 輸入一個整數數組,判斷該數組是不是某二叉搜索樹的後序遍歷的結果。
 * 如果是則輸出Yes,否則輸出No。
 * 假設輸入的數組的任意兩個數字都互不相同。
 */
public class Solution {
    public boolean VerifySquenceOfBST(int [] sequence) {
        int len = sequence.length;
        if(len == 0){
            return false;
        }
        if(len == 1){
            return true;
        }
        int root = sequence[len-1];
        int rightindex = len-1;
        for(int i=0;i<len-1;i++){
            if(sequence[i]>root){
                rightindex = i;
                break;
            }
        }
        boolean flag1 = true;
        boolean flag2 = true;

        for(int i=rightindex;i<len-1;i++){
            if(sequence[i]<root){
                return false;
            }
        }
        if(rightindex>0)
            flag1 = VerifySquenceOfBST(Arrays.copyOfRange(sequence, 0, rightindex));
        if(len-1>rightindex)
            flag2 = VerifySquenceOfBST(Arrays.copyOfRange(sequence, rightindex, len-1));
        return flag1&&flag2;
    }
    public static void main(String args[]){
        Solution s = new Solution();
        int[] a = {5,4,3,2};
        boolean result = s.VerifySquenceOfBST(a);
        System.out.println(result);
    }
}

24、輸入一顆二叉樹的跟節點和一個整數,打印出二叉樹中結點值的和爲輸入整數的所有路徑。路徑定義爲從樹的根結點開始往下一直到葉結點所經過的結點形成一條路徑。(注意: 在返回值的list中,數組長度大的數組靠前)

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 static void find(ArrayList<ArrayList<Integer>> paths,ArrayList<Integer> list,TreeNode root,int target){
        list.add(root.val); 
        if(root.left==null&&root.right==null){
            if(target == root.val){
                paths.add(list);
            }
            return;
        }
        target = target - root.val;
        ArrayList<Integer> list2=new ArrayList<>();
        list2.addAll(list);
        if(root.left!=null){
            find(paths,list,root.left,target);
        }
        if(root.right!=null){
            find(paths,list2,root.right,target);
        }
    }
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();
        ArrayList<Integer> list = new ArrayList<>();
        if(root == null){
            return result;
        }
        find(result,list,root,target);
        return result;

    }
}

25、輸入一個複雜鏈表(每個節點中有節點值,以及兩個指針,一個指向下一個節點,另一個特殊指針指向任意一個節點),返回結果爲複製後複雜鏈表的head。(注意,輸出結果中請不要返回參數中的節點引用,否則判題程序會直接返回空)

import java.util.HashMap;

/**
 * 輸入一個複雜鏈表(每個節點中有節點值,以及兩個指針,
 * 一個指向下一個節點,另一個特殊指針指向任意一個節點),
 * 返回結果爲複製後複雜鏈表的head。
 * (注意,輸出結果中請不要返回參數中的節點引用,否則判題程序會直接返回空)
 * @author zhx
 *
 */

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;
        }
        HashMap<RandomListNode,RandomListNode> map = new HashMap<>();
        RandomListNode cur = pHead;
        /**
         * 1  1'
         * 2  2'
         * 3  3'
         */
        while(cur!=null){
            map.put(cur, new RandomListNode(cur.label));
            cur = cur.next;
        }
        cur = pHead;
        while(cur!=null){
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(pHead);
    }
}

26、輸入一棵二叉搜索樹,將該二叉搜索樹轉換成一個排序的雙向鏈表。要求不能創建任何新的結點,只能調整樹中結點指針的指向。

27、輸入一個字符串,按字典序打印出該字符串中字符的所有排列。例如輸入字符串abc,則打印出由字符a,b,c所能排列出來的所有字符串abc,acb,bac,bca,cab和cba。

28、數組中有一個數字出現的次數超過數組長度的一半,請找出這個數字。例如輸入一個長度爲9的數組{1,2,3,2,2,2,5,4,2}。由於數字2在數組中出現了5次,超過數組長度的一半,因此輸出2。如果不存在則輸出0。

import java.util.Arrays;
public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
        Arrays.sort(array);
        int k = array[array.length/2];
        int num = 0;
        for(int i=0;i<array.length;i++){
            if(array[i] == k){
                num++;
            }
            if(num>array.length/2){
                return k;
            }
        }
        return 0;
    }
}

29、輸入n個整數,找出其中最小的K個數。例如輸入4,5,1,6,2,7,3,8這8個數字,則最小的4個數字是1,2,3,4,。

import java.util.ArrayList;
import java.util.Arrays;

public class Solution {
    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        ArrayList<Integer> al = new ArrayList<>();
        if(k>input.length){
            return al;
        }
        Arrays.sort(input);
        for(int i=0;i<k;i++){
            al.add(input[i]);
        }
        return al;

    }
}

30、HZ偶爾會拿些專業問題來忽悠那些非計算機專業的同學。今天測試組開完會後,他又發話了:在古老的一維模式識別中,常常需要計算連續子向量的最大和,當向量全爲正數的時候,問題很好解決。但是,如果向量中包含負數,是否應該包含某個負數,並期望旁邊的正數會彌補它呢?例如:{6,-3,-2,7,-15,1,2,2},連續子向量的最大和爲8(從第0個開始,到第3個爲止)。給一個數組,返回它的最大連續子序列的和,你會不會被他忽悠住?(子向量的長度至少是1)

public class Solution {
    public int FindGreatestSumOfSubArray(int[] array) {
        int max = Integer.MIN_VALUE;

        for(int i=0;i<array.length;i++){
            int sum = 0;
            for(int j=i;j<array.length;j++){
                sum = sum + array[j];
                if(max<sum){
                    max = sum;
                }
            }
        }

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