剑指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;
    }
}

猜你喜欢

转载自blog.csdn.net/xdzhouxin/article/details/81124003