面试题51:数组中重复的数字

public class Solution {
    public boolean duplicate(int numbers[],int length,int [] duplication) {
        for(int i=0;i<length;i++){
            if(numbers[i] != i){
                int tmp = numbers[numbers[i]];
                if(tmp == numbers[i]){
                    duplication[0] = numbers[i];
                    return true;
                }
                numbers[numbers[i]] = numbers[i];
                numbers[i] = tmp;
            }
        }
        return false;   
    }
}

面试题52:构建乘积数组

public class Solution {
    public int[] multiply(int[] A) {
        int len = A.length;
        int[] B = new int[len];
        int lToR = 1;
        int rToL = 1;
        B[0] = 1;
        for(int i=1;i<=len-1;i++){
            lToR *= A[i-1];
            B[i] = lToR ;
        }
        for(int i=len-2;i>=0;i--){
            rToL *= A[i+1];
            B[i] = B[i]*rToL;
        }
        return B;
    }
}

面试题53:正则表达式匹配

import java.util.Arrays;
public class Solution {
    public boolean match(char[] str, char[] pattern)
    {
        int lenS = str.length;
        int lenP = pattern.length;
        boolean[]  matchArr = new boolean[lenS + 1];
        Arrays.fill(matchArr,false);
        matchArr[lenS] = true;
        for(int i=lenP-1;i>=0;i--){
            if(pattern[i] == '*'){
                for(int j = lenS-1;j>=0;j--){
                    matchArr[j] = matchArr[j] || (matchArr[j+1] && (pattern[i-1] == '.' || pattern[i-1] == str[j])); 
                }
                i--;
            }else{
                for(int j=0;j<lenS;j++){
                    matchArr[j] = matchArr[j+1] && (pattern[i] == '.' || pattern[i] == str[j]);
                }
                matchArr[lenS] = false;
            }
        }
        return matchArr[0];
    }
}

面试题54:表示数值的字符串

public class Solution {
    public boolean isNumeric(char[] str) {
        String string = String.valueOf(str);
        return string.matches("[\\+-]?[0-9]*(\\.[0-9]*)?([eE][\\+-]?[0-9]+)?");
    }
}

面试题55:字符流中第一个不重复的字符

public class Solution {
    private int[] arr =new int[256];
    private int cnt = 1;

    //Insert one char from stringstream
    public void Insert(char ch)
    {
        int index = (int)ch - ((int)'0' - 48);
        System.out.println(index);
        if(arr[index] == 0){
            arr[index] = cnt;
        }else{
            arr[index] = -1;
        }
        cnt++;
    }
  //return the first appearence once char in current stringstream
    public char FirstAppearingOnce()
    {   
        char ch = '#';
        int minIndex = Integer.MAX_VALUE;
        for(int i=0;i<arr.length;i++){
            if(arr[i] != -1 && arr[i] !=0){
                if(minIndex > arr[i]){
                    minIndex = arr[i];
                    ch = (char)(i + '0' - 48 );
                }
            }
        }
        return ch;
    }
}

面试题56:链表中环的入口节点

public class Solution {
    public ListNode EntryNodeOfLoop(ListNode pHead)
    {
        if(pHead == null || pHead.next == null) return null;
        ListNode fast = pHead;
        ListNode slow = pHead;
        while(fast != null && fast.next != null ){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                fast = pHead;
                while(fast != slow){
                    fast = fast.next;
                    slow = slow.next;
                }
                if(fast == slow){
                    return fast;
                }
            }
        }
        return null;
    }
}

面试题57:删除链表中重复的节点

public class Solution {
    public ListNode deleteDuplication(ListNode pHead) {
        if(pHead==null) return null;
        ListNode FakeHead=new ListNode(0);
        FakeHead.next=pHead;
        ListNode pre=FakeHead;
        ListNode cur=pHead;
        while(cur!=null){
            while(cur.next!=null&&cur.val==cur.next.val){
                cur=cur.next;
            }
            if(pre.next==cur){
                pre.next = cur;
                pre=pre.next;
            }
            else{
                pre.next=cur.next;
            }
            cur=cur.next;
        }
        return FakeHead.next;
    }
}

面试题58:二叉树的下一个节点

public class Solution {
    public TreeLinkNode GetNext(TreeLinkNode pNode)
    {
        if(pNode == null) return null;
        TreeLinkNode next = null;
        if(pNode.right != null){
            TreeLinkNode tmp = pNode.right;
            while(tmp.left != null){
                tmp = tmp.left;
            }
            next = tmp;
        }else if(pNode.next != null){
            TreeLinkNode cur = pNode;
            TreeLinkNode parent = pNode.next;
            while(parent != null && cur == parent.right){
                cur = parent;
                parent = parent.next;
            }
            next = parent;
        }
        return next;
    }
}

面试题59:对称的二叉树

public class Solution {
    boolean isSymmetrical(TreeNode pRoot)
    {
        if(pRoot == null) return true;
        boolean res = Judge(pRoot.left,pRoot.right);
        return res;
    }
    public boolean Judge(TreeNode left,TreeNode right){
        if(left == null && right == null) return true;
        if(left == null || right == null) return false;
        return (left.val == right.val) && 
        Judge(left.left,right.right) && 
            Judge(left.right,right.left);
    }
}

面试题60:之字形打印二叉树

public class Solution {
    public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
         travel(pRoot,res,0);
         return res;
    }
    public void travel(TreeNode cur,ArrayList<ArrayList<Integer>> res,int level){
        if(cur==null) return;
        if(res.size()<=level){
            ArrayList<Integer> newLevel = new ArrayList<Integer>();
            res.add(newLevel);
        }
        ArrayList<Integer> col = res.get(level);
        if(level%2==0){
            col.add(cur.val);
        }else{
            col.add(0,cur.val);
        }
        travel(cur.left,res,level+1);
        travel(cur.right,res,level+1);
    }
}
posted on 2016-09-06 15:39  岳阳楼  阅读(154)  评论(0编辑  收藏  举报