剑指offer面试题1-10

面试题3:二维数组中的查找
public class Solution {
    public boolean Find(int [][] array,int target) {
        boolean isFound = false;
        int m = array.length;
        int n = array[0].length;
        if(m>0 && n>0 ){
            int row = 0;
            int col = n-1;
            while(row<m && col>=0){
                if(array[row][col] > target){
                    col = col - 1;
                }else if(array[row][col] <target){
                    row = row + 1;
                }else{
                    isFound = true;
                    break;
                }
            }
        }
        return isFound;       
    }
}
面试题4:替换空格
public class Solution {
    public String replaceSpace(StringBuffer str) {
        StringBuffer res = new StringBuffer("");
        char[] strChar = str.toString().toCharArray();
        int len = strChar.length;
        for(int i=0;i<len;i++){
            if(strChar[i] == ' '){
                res.append("%20");
            }else{
                res.append(strChar[i]);
            }
        }
        return res.toString();
    }
}
面试题5:从尾到头打印链表
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        Stack<Integer> stack = new Stack<Integer>();
        while(listNode != null){
            stack.push(listNode.val);
            listNode = listNode.next;
        }
        ArrayList<Integer> res = new ArrayList<Integer>();
        while(!stack.isEmpty()){
            res.add(stack.peek());
            stack.pop();
        }
        return res;
    }
}
面试题6:重建二叉树
public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        if(pre.length != in.length) return null;
        if(pre.length == 0 || pre==null || in == null){
            return null;
        } 
        return construct(pre,0,pre.length-1,in,0,in.length-1);
    }
    public TreeNode construct(int[] pre,int startPre,int endPre,int[] in,int startIn,int endIn){
        int rootVal = pre[startPre];
        TreeNode root = new TreeNode(rootVal);
        root.left = null;
        root.right= null;
        if(startPre == endPre && startIn == endIn){
            return root;
        }       
        int posIn = startIn;
        while(posIn <= endIn && in[posIn] != rootVal) posIn++;
        int leftLen = posIn - startIn ;
        if(leftLen>0){
            root.left  = construct(pre,startPre+1 , startPre+leftLen,in,startIn,posIn-1);
        }
        if(leftLen<endPre-startPre){
            root.right = construct(pre,startPre+leftLen+1,endPre,in,posIn+1,endIn);
        }
        return root;
    }
}
面试题7:用两个栈实现队列
import java.util.Stack;
public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    
    public void push(int node) {
        while(!stack2.isEmpty()){
            stack1.push(stack2.pop());
        }
        stack1.push(node);
    }
    public int pop() {
        while(!stack1.isEmpty()){
            stack2.push(stack1.pop());
        }
        return stack2.pop();
    }
}
面试题8:旋转数组的最小数字
import java.util.ArrayList;
public class Solution {
    public int minNumberInRotateArray(int [] array) {
        if(array.length == 0) return 0;
        int l = 0;
        int r = array.length-1; 
        while(l<r){           
            int mid = l + (r - l)/2;         
            if(array[mid]>array[r] ){
                l = mid + 1;
            }else if(array[mid]<array[r]){
                r = mid ;
            }else{
                r--;
            }
        }
        return array[l];
    }
}
面试题9:斐波那契数列
public class Solution {
    public int Fibonacci(int n) {
        int twoBeforeF = 1;
        int oneBeforeF = 1;
        int thisF = 0;
        if (n==0) return 0;
        if (n==1) return 1;       
        if (n==2) return 1;
        
        if(n>2){
            for(int i=3;i<=n;i++){
                thisF = twoBeforeF + oneBeforeF;
                twoBeforeF  = oneBeforeF;
                oneBeforeF  = thisF;
            }
        }
        return thisF;
    }
}
面试题9:跳台阶
public class Solution {
    public int JumpFloor(int target) {
        if(target == 0 ) return 0;
        if(target == 1 ) return 1;
        if(target == 2 ) return 2;
        int twoBefore = 1;
        int oneBefore = 2;
        int res = 0;
        for(int i=3;i<=target;i++){
            res = twoBefore + oneBefore;
            twoBefore = oneBefore;
            oneBefore = res;
        }
        return res;
    }
}
面试题9:变态跳台阶
public class Solution {
    public int JumpFloorII(int target) {
        if(target == 0) return 0;
        int[] arr = new int[target+1];
        if(target>0){
            arr[0] = 0;
            for(int i=1;i<=target;i++){
                int tmpSum = 1;
                for(int j=0;j<i;j++){
                    tmpSum += arr[j];
                }
                arr[i] = tmpSum;
            }
        }
        return arr[target];
    }
}
面试题10:二进制中1的个数
public class Solution {
    public int NumberOf1(int n) {
        int count = 0;
       while(n!=0){
            count++;
            n = n & (n-1);
        } 
        return count;
    }
}

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