劍指offer-(1-5)

1.二維數(shù)組中的查找

在一個(gè)二維數(shù)組中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請(qǐng)完成一個(gè)函數(shù),輸入這樣的一個(gè)二維數(shù)組和一個(gè)整數(shù),判斷數(shù)組中是否含有該整數(shù)。

考點(diǎn):數(shù)組

  • 1.直接使用兩次嵌套遍歷。時(shí)間復(fù)雜度O(n^2)
public class Solution {  
    public boolean Find(int [][] array,int target) {  
        for(int i=0;i<array.length;i++){  
            for(int j=0;j<array[i].length;j++){  
                if(array[i][j]==target){  
                    return true;  
                }         
            }  
        }  
       return false;   
    }  
}
  • 2.改進(jìn):根據(jù)題目,每行每列都是遞增,則找到矩陣右上角數(shù)字,判斷它和target大小,它小則行減1,它大則列減一。時(shí)間復(fù)雜度O(n)
public class Problem_1 {
    public static boolean Find(int[][] array, int target) {
        boolean found = false;
        int rows = array.length;
        int columns = array[0].length;
        if (array != null && rows > 0 && columns > 0 ) {
            int row = 0;
            int column = columns - 1;
            while (row < rows && column >= 0) {
                if (array[row][column] == target) {
                    found = true;
                    break;
                }
                else if (array[row][column] > target) {
                    --column;
                }
                else
                    ++row;
            }
        }
        return found;
    }
}

2.替換空格

請(qǐng)實(shí)現(xiàn)一個(gè)函數(shù),將一個(gè)字符串中的空格替換成“%20”。例如,當(dāng)字符串為We Are Happy.則經(jīng)過替換之后的字符串為We%20Are%20Happy。
考點(diǎn):字符串

public class Solution {
    public String replaceSpace(StringBuffer str) {
         String str1 = str.toString();
        String str2 = str1.replace(" ", "%20");
        return str2;
    }
}

3.從尾到頭打印鏈表

輸入一個(gè)鏈表,從尾到頭打印鏈表每個(gè)節(jié)點(diǎn)的值。

考點(diǎn):鏈表
解題點(diǎn):先進(jìn)后出,使用棧。遞歸的本質(zhì)就是棧。
使用棧

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.Stack;
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
         Stack<Integer> sk = new Stack<Integer>();
        while(listNode!=null)
        {
            sk.push(listNode.val);
            listNode=listNode.next;
        }
         ArrayList<Integer> al = new ArrayList<Integer>();
        while(!sk.isEmpty())
        {
            al.add(sk.pop());
        }
        return al;
    }
}

使用遞歸

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        if (list == null){
            return list;
        }
        ListNode pnode = listNode;
        if(pnode != null){
            if(pnode.next != null){
                list = printListFromTailToHead(pnode.next);
            }
            list.add(pnode.val);
        }
        return list;
    }
}

當(dāng)鏈表很長(zhǎng)時(shí),則函數(shù)調(diào)用層級(jí)很深,導(dǎo)致函數(shù)調(diào)用棧溢出。

4.重建二叉樹

輸入某二叉樹的前序遍歷和中序遍歷的結(jié)果,請(qǐng)重建出該二叉樹。假設(shè)輸入的前序遍歷和中序遍歷的結(jié)果中都不含重復(fù)的數(shù)字。例如輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},則重建二叉樹并返回。

考點(diǎn):二叉樹
先序中序后序遍歷的實(shí)現(xiàn)方法都有遞歸和循環(huán)實(shí)現(xiàn)方法。
求最大(最小值)時(shí)可以使用最大(最小)堆來解決。

  • 1.根據(jù)先序和中序的特點(diǎn),根據(jù)先序第一個(gè)值找到根節(jié)點(diǎn);
  • 2.在中序中找到根節(jié)點(diǎn),則根節(jié)點(diǎn)前面就是左子樹,后面就是右子樹;
  • 3.通過遞歸,構(gòu)建新二叉樹的左子樹和右子樹。
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        TreeNode root = inputTree(pre, 0, pre.length-1, in, 0, in.length-1);
        return root;
    }
    public TreeNode inputTree(int[] pre, int startPre, int endPre, int[] in, int startIn, int endIn){
        if(startPre > endPre || startIn > endIn){
            return null;
        }
        TreeNode root = new TreeNode(pre[startPre]);
        for(int i=startIn;i<=endIn;i++){
            if(in[i]==pre[startPre]){
                root.left=inputTree(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);
                root.right=inputTree(pre,startPre+i-startIn+1,endPre,in,i+1,endIn);
            }
        }
        return root;
    }
}

5.用兩個(gè)棧實(shí)現(xiàn)隊(duì)列

用兩個(gè)棧來實(shí)現(xiàn)一個(gè)隊(duì)列,完成隊(duì)列的Push和Pop操作。 隊(duì)列中的元素為int類型。

考點(diǎn):棧和隊(duì)列

棧:先去后出;隊(duì)列:先進(jìn)先出

import java.util.Stack;

public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    
    public void push(int node) {
        stack1.push(new Integer(node));
    }
    
    public int pop() {
    if(stack2.empty()){
        while(!stack1.empty()){
            stack2.push(stack1.pop());
        }
    }
    if(stack2.empty()){
        System.out.println("null");
    }
    return stack2.pop().intValue();
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容