剑指offer面试题21-30

面试题21:包含min函数的栈
import java.util.Stack;

public class Solution {
    private Stack<Integer> stack = new Stack<Integer>();
    private Stack<Integer> minStack = new Stack<Integer>();
    public void push(int node) {
        if(minStack.isEmpty() || node <= minStack.peek()){
            minStack.push(node);
        }
        stack.push(node);
    }
    
    public void pop() {
        if(stack.peek().equals(minStack.peek())){
            minStack.pop();
        }
        stack.pop();
    }
    
    public int top() {
        return stack.peek();
    }
    
    public int min() {
        if(!minStack.isEmpty()){
            return minStack.peek();
        }else{
            return stack.peek();
        }
    }
}
面试题22:栈的压入、弹出序列
import java.util.ArrayList;
import java.util.Stack;  

public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        boolean flag = false;
        int n = pushA.length;
        if(n > 0){
            int i=0,j=0;
            Stack<Integer> s = new Stack<Integer>();
            while(j < n){
                while(s.isEmpty() || s.peek()!=popA[j]){
                    if(i == n) break;
                    s.push(pushA[i]);
                    i++;
                }
                if(s.peek()!=popA[j]){
                    break;
                }
                s.pop();
                j++;
            }
            if(s.isEmpty() && j==n){
                flag = true;
            }
        }
        return flag;
    }
}
面试题23:从上往下打印二叉树
public class Solution {
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        ArrayList<Integer> res = new ArrayList<Integer>();
        Queue<TreeNode> que = new LinkedList<TreeNode>();
        if(root == null) return res;
        que.offer(root);
        while(!que.isEmpty()){
            int levelNum = que.size();
            for(int i=0;i<levelNum;i++){
                if(que.peek().left != null) que.offer(que.peek().left);
                if(que.peek().right!= null) que.offer(que.peek().right);
                res.add(que.poll().val);
            }
        }
        return res;
    }
}
面试题24:
import java.util.*;
public class Solution {
    public boolean VerifySquenceOfBST(int [] sequence) {
        if(sequence == null || sequence.length == 0) return false;
        int len = sequence.length;
        int rootVal = sequence[len-1];
        int i=0;
        for(;i<len-1;i++){
            if(rootVal < sequence[i]){
                break;
            }
        }
        int j=i;
        for(;j<len-1;j++){
            if(rootVal > sequence[j]){
                return false;
            }
        }
        boolean judgeLeft = true;
        if(i>0){judgeLeft = VerifySquenceOfBST(getSeq(sequence,0,i-1));}
        boolean judgeRight = true;
        if(i<len-1){judgeRight = VerifySquenceOfBST(getSeq(sequence,i,len-2));}
        return (judgeLeft && judgeRight);
    }
    public int[] getSeq(int[] seq,int start,int end){
        int[] ret = new int[end-start+1];
        for(int i=start;i<=end;i++){
            ret[i-start] = seq[i];
        }
        return ret;
    }
}
面试题25:二叉树中和为某一值的路径
public class Solution {
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> tmp = new ArrayList<Integer>();
        findHelp(res,tmp,root,0,target);
        return res;
    }
    public void findHelp(ArrayList<ArrayList<Integer>> res,ArrayList<Integer> tmp,TreeNode root , int curSum,int target){
        if(root == null) return;
        curSum += root.val;       
        ArrayList<Integer> tmpCur = new ArrayList<Integer>(tmp);
        tmpCur.add(root.val);
        if(root.left == null && root.right==null && curSum == target) {res.add(tmpCur);return;}
        else if(curSum > target) {return;}
        else if(curSum < target){
            findHelp(res,tmpCur,root.left,curSum,target);
            findHelp(res,tmpCur,root.right,curSum,target);
        }
        return;
    }
}
面试题26:复杂链表的复制
public class Solution {
    public RandomListNode Clone(RandomListNode pHead)
    {
        if(pHead == null) return null;
        RandomListNode p = pHead;
        while(p != null){
            RandomListNode pClone  = new RandomListNode(p.label);
            pClone.next = p.next;
            pClone.random = null;
            p.next = pClone;
            p = p.next.next;
        }
        p = pHead;
        while(p != null){
            if(p.random != null){
                p.next.random = p.random.next;
            }
            p = p.next.next;
        }

        RandomListNode pCloneHead = new RandomListNode(0);
        RandomListNode pCloneNode = new RandomListNode(0);
        p = pHead;
        if(p != null){
            pCloneHead = pCloneNode =  p.next;
            p.next = pCloneNode.next;
            p = p.next;
        }
        while(p != null){
            pCloneNode.next = p.next;
            pCloneNode = pCloneNode.next;
            p.next = pCloneNode.next;
            p = p.next;
        }
        return pCloneHead;
    }
}
面试题27:二叉搜索树与双向链表
public class Solution {
    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree == null) return pRootOfTree;
          pRootOfTree = convertHelp(pRootOfTree);
          while(pRootOfTree.left!=null) pRootOfTree = pRootOfTree.left;
          return pRootOfTree;
    }
    public TreeNode convertHelp(TreeNode root){
        if(root == null) return root;
        if(root.left != null){
            TreeNode left = convertHelp(root.left);
            while(left.right!=null) left = left.right;
            left.right = root;
            root.left  = left;
        }
        if(root.right != null){
            TreeNode right = convertHelp(root.right);
            while(right.left!=null) right = right.left;
            root.right = right;
            right.left = root;
        }
        return root;
    }
}
面试题28:字符串的排列
import java.util.ArrayList;
import java.util.Collections; 
public class Solution {
    public ArrayList<String> Permutation(String str) {
        ArrayList<String> res = new ArrayList<String>();
        if(str == null || str.length()>9 || str.length()<=0){
            return res;
        }
        str= str.trim();
        char[] arr = str.toCharArray();
        permuHelp(arr,0,res);
        Collections.sort(res);
        return res;
    }
    public void permuHelp(char[] arr,int start,ArrayList<String> res){
        if(arr.length - 1 == start){
            res.add(new String(arr));
            return;
        }
        for(int i=start;i<arr.length;i++){
            if(i != start && arr[i]==arr[start]) continue;
            char tmp = arr[start];
            arr[start] = arr[i];
            arr[i]     = tmp;

            permuHelp(arr,start+1,res);

            tmp = arr[start];
            arr[start] = arr[i];
            arr[i]     = tmp;
        }
    }
}
面试题29:数组中出现次数超过一半的数字
public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
        if(array.length == 0) return 0;
        int res = array[0];
        int cnt = 1;
        for(int i=1;i<array.length;i++){
            if(res == array[i]){
                cnt++;
            }else{
                cnt--;
            }
            
            if(cnt == 0){
                res = array[i];
                cnt = 1;
            } 
        }
        cnt = 0;
        for(int i=0;i<array.length;i++){
            if(array[i] == res){
                cnt++;
            }
        }
        if(cnt>array.length/2) return res;
        else return 0;     
    }
}
面试题30:最小的K个数
import java.util.PriorityQueue;  
import java.util.Comparator;  
import java.util.ArrayList;
import java.util.Queue;
public class Solution {
    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        ArrayList<Integer> res = new ArrayList<Integer>();
        if(input.length==0 || k>input.length || k==0) return res;
        Comparator<Integer> com =  new Comparator<Integer>(){
            public int compare(Integer a , Integer b){
                if(a>b) {return 1;}
                else if(a<b) {return -1;}
                else {return 0;}                  
            }
        };
        Queue<Integer> pq =  new PriorityQueue<Integer>(input.length,com);  
        for(int i=0;i<input.length;i++){
            pq.add(input[i]);
        }        
        for(int i=0;i<k;i++){
            res.add(pq.poll());
        }
        return res;
    }
}

 

你可能感兴趣的:(剑指offer面试题21-30)