劍指offer編程題 java實現

最近有空會做一下這些編程題,再此做一個記錄,未必是最優解。

1、在一個二維數組中(每個一維數組的長度相同),每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成一個函數,輸入這樣的一個二維數組和一個整數,判斷數組中是否含有該整數。

/* 此二維數組可看成一個表格,裏面數據是有序的 即左上角最小,右下角最大,因此從右上角開始對比,如果
 * 大於右上角,座標下移,如果小於右上角則座標左移繼續對比,直至找到數據或者不能再移
 * */
public class Solution {
    public static boolean find(int target, int[][] array) {
        /*判斷數組是否爲空*/
        if (array == null || array.length <= 0 || array[0].length <= 0) {
            return false;
        }

        int rows = 0; // 獲取數組行數
        int columns = array[0].length - 1; // 獲取數組列數

        //判斷是否小於最小數,大於最大數
        if (array[0][0] > target || array[array.length - 1][columns] < target) {
            return false;
        }
        //從左上角開始判斷
        while (rows < array.length && columns >= 0) {
            System.out.println("array[" + rows + "][" + columns + "]");
            if (array[rows][columns] == target) {
                return true;
            } else if (array[rows][columns] > target) {
                columns--;
            } else {
                rows++;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        String str = "['1,2,3,4']";
        String newStr = str.replaceAll(",", "','");
        System.out.println(newStr);
    }
}

2、請實現一個函數,將一個字符串中的每個空格替換成“%20”。例如,當字符串爲We Are Happy.則經過替換之後的字符串爲We%20Are%20Happy。

/*
 * 如果是自己寫的替換方法,可以從後往前替換,從前往後替換可能會出現一些問題,由於是一個空格替換成三個字符,
 * 長度不一致會導致字符串長度出現變化,當字符下標後移時不是之前想要的字符
 * 
 * */
public class Solution2 {
    public static String replaceSpace(StringBuffer str) {
        /*暴力替換  從尾到頭遍歷替換*/
        int length = str.length();
        for (int i = length - 1; i >= 0; i--) {
            if (str.charAt(i) == 32) {
                str.replace(i, i + 1, "%20");
            }
        }
        return str.toString();
        /*
         *  第二種方式 直接使用已有方法
         *  return str.toString().replaceAll("\\s", "%20");
         * */

    }

    public static void main(String[] args) {
        StringBuffer str = new StringBuffer("We Are Happy");
        String res = replaceSpace(str);
        System.out.println(res);

    }
}

3、輸入一個鏈表,按鏈表值從尾到頭的順序返回一個ArrayList。

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
import java.util.List;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> list=new ArrayList();
        
        //獲取正序list
        while(listNode!=null){
            list.add(listNode.val);
            listNode=listNode.next;
            
        }
        //將list倒序排序
        int size=list.size();
        Integer tem=null;
        for(int i=0;i<size-1-i;i++){
            tem = list.get(i);
            list.set(i,list.get(size-i-1));
            list.set(size-i-1,tem);
        }
        return list;
    }
}

4、輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。例如輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},則重建二叉樹並返回。

package sword.finger.offer;

/*
 * 前序遍歷中的第一個肯定是根節點,根據根節點在中序遍歷中可以將樹分爲左右兩個子樹中序遍歷以及長度,
 * 根據長度在前序遍歷中除去第一個元素,接下來就是左子樹和右子樹的前序遍歷,
 * 接下來即可根據子樹的前序遍歷和中序遍歷獲取根節點和更小的子樹
 * 以此遞歸
 * 
 * */
public class Solution4 {
    public static TreeNode reConstructBinaryTree(int[] pre, int[] in) {

        int size = pre.length;
        int i = 0;
        //如果遍歷數組爲空
        if (size == 0) {
            return null;
        }
        //根節點
        TreeNode tree = new TreeNode(pre[0]);

        //根據根節點和中序遍歷找左右子節點
        for (; i < size; i++) {
            if (pre[0] == in[i]) {
                //右子樹
                int[] newRightPre = new int[size - i - 1];
                int[] newRightIn = new int[size - i - 1];
                System.arraycopy(pre, i + 1, newRightPre, 0, size - i - 1);
                System.arraycopy(in, i + 1, newRightIn, 0, size - i - 1);
                //左子樹
                int[] newLeftPre = new int[i];
                int[] newLeftIn = new int[i];
                System.arraycopy(pre, 1, newLeftPre, 0, i);
                System.arraycopy(in, 0, newLeftIn, 0, i);
                //遞歸
                tree.right = reConstructBinaryTree(newRightPre, newRightIn);
                tree.left = reConstructBinaryTree(newLeftPre, newLeftIn);
                break;
            }

        }
        return tree;
    }

    public static void main(String[] args) {
        int[] pre = {1, 2, 4, 7, 3, 5, 6, 8};
        int[] in = {4, 7, 2, 1, 5, 3, 8, 6};
        TreeNode tree = reConstructBinaryTree(pre, in);
        System.out.println(tree.val);
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

5、用兩個棧來實現一個隊列,完成隊列的Push和Pop操作。 隊列中的元素爲int類型。

import java.util.Stack;

/*
 * 隊列先進先出,棧後進先出,push爲添加模擬進,pop爲返回並刪除一個數模擬出
 * 因此先將數壓入棧1中,在將數據倒入到棧2中,數據順序就會發生改變,先壓入棧1的數就會在棧2的頂部
 * 當下次在添加數據時就現將棧2中的數據倒回棧1,添加後在倒入棧2完成一次push,進行刪除操作就直接 
 * 將棧2頂部數據pop掉就達到了先進先出的目的
 * */
public class Solution5 {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();

    public void push(int node) {
        while (!stack2.empty()) {
            stack1.push(stack2.pop());
        }
        stack1.push(node);
        while (!stack1.empty()) {
            stack2.push(stack1.pop());
        }
    }

    public int pop() {
        return stack2.pop();
    }
}

未完待續......

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