劍指Offer題目整理

二分查找

LinkedList

反轉鏈表java

public class Solution {
    public ListNode ReverseList(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        
        return prev;
    }
}
複製代碼

合併兩個有序鏈表node

public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if (list1 == null && list2 == null) {
            return null;
        }
        ListNode curr = new ListNode(0);
        ListNode dummy = curr;
        while (list1 != null && list2 != null) {
            if (list1.val < list2.val) {
                curr.next = list1;
                list1 = list1.next;
            }
            else {
                curr.next = list2;
                list2 = list2.next;
            }
            curr = curr.next;
        }
        if (list1 == null) {
            curr.next = list2;
        }
        if (list2 == null) {
            curr.next = list1;
        }
        return dummy.next;
    }
}
複製代碼

String處理

Stack和Queue

兩個棧實現隊列數組

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(node);
    }
    
    public int pop() {
        if (!stack2.isEmpty()) {
            return stack2.pop();
        }
        while (!stack1.isEmpty()) {
            stack2.push(stack1.pop());
        }
        if (!stack2.isEmpty()) {
            return stack2.pop();
        }
        return -1;
    }
}
複製代碼

二叉樹

前序和中序重建二叉樹函數

public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        if (pre == null || in == null || pre.length == 0 || in.length == 0) {
            return null;
        }
        int n = pre.length;
        TreeNode root = helper(pre, in, 0, n - 1, 0, n - 1);
        
        return root;
    }
    
    private TreeNode helper(int[] pre, int[] in, int preStart, int preEnd, int inStart, int inEnd) {
        if (preStart > preEnd || inStart > inEnd) {
            return null;
        }
        TreeNode root = new TreeNode(pre[preStart]);
        int val = root.val;
        int index = 0;
        for (int i = inStart; i <= inEnd; i++) {
            if (in[i] == val) {
                index = i;
            }
        }
        root.left = helper(pre, in, preStart + 1, preEnd, inStart, index - 1);
        root.right = helper(pre, in, preStart + (index - inStart) + 1, preEnd, index + 1, inEnd);
        
        return root;
    }
}
複製代碼

DP

斐波那契數列spa

  • 問題: 如今要求輸入一個整數n,請你輸出斐波那契數列的第n項(從0開始,第0項爲0)。 n<=39
public class Solution {
    public int Fibonacci(int n) {
        if (n <= 1) {
            return n;
        }
        int fib = 0;
        int lastfib = 1;
        int secfib = 0;
        int count = 1;
        while (count < n) {
            fib = lastfib + secfib;
            secfib = lastfib;
            lastfib = fib;
            count++;
        }
        return fib;
    }
    
}
複製代碼

跳臺階3d

public class Solution {
    public int JumpFloor(int target) {
        if (target == 1) {
            return 1;
        }
        if (target == 2) {
            return 2;
        }
        int oneStepBefore = 2;
        int twoStepsBefore = 1;
        int total = 0;
        for (int i = 3; i <= target; i++) {
            total = oneStepBefore + twoStepsBefore;
            twoStepsBefore = oneStepBefore;
            oneStepBefore = total;
        }
        
        return total;
    }
}
複製代碼

矩形覆蓋指針

  • 問題: 咱們能夠用2×1的小矩形橫着或者豎着去覆蓋更大的矩形。請問用n個2×1的小矩形無重疊地覆蓋一個2×n的大矩形,總共有多少種方法?

public class Solution {
    public int RectCover(int target) {
        if (target < 1) {
            return 0;
        } else if (target == 1 || target == 2) {
            return target;
        } else {
            return RectCover(target-1) + RectCover(target-2);
        }
    }
}
複製代碼

位運算

二進制中1的個數code

  • 問題: 輸入一個整數,輸出該數二進制表示中1的個數。其中負數用補碼錶示。
public class Solution {
    public int NumberOf1(int n) {
        int count = 0;
        for (int i = 0; i < 32; i++) {
            count += (n >> i) ^ 1; // 這裏是 用的'與'操做 必須是1 & 1 ==> 1
        }
        return count;
    }
}
複製代碼

數值的整數次方cdn

  • 問題: 給定一個double類型的浮點數base和int類型的整數exponent。求base的exponent次方。
public class Solution {
    public double Power(double base, int n) {
        double res = 1,curr = base;
        int exponent;
        if(n > 0){
            exponent = n;
        }else if(n < 0){
            if(base == 0)
                throw new RuntimeException("分母不能爲0"); 
            exponent = -n;
            curr = 1 / curr;
        }else{// n==0
            return 1;// 0的0次方
        }
        while(exponent != 0){
            if((exponent & 1) == 1)
                res *= curr;
            curr* = curr;// 翻倍
            exponent >>= 1;// 右移一位 等價於 exponent /= 2
        }
        return res;       
    }
}
複製代碼

雙指針

同向雙指針模板 ? 調整數組順序使奇數位於偶數前面blog

  • 問題:輸入一個整數數組,實現一個函數來調整該數組中數字的順序,使得全部的奇數位於數組的前半部分,全部的偶數位於數組的後半部分,並保證奇數和奇數,偶數和偶數之間的相對位置不變。
  • 思路: 只要遇到偶數在奇數前面的時候須要swap,一個指針指向最靠前的偶數,一個指針指向這個偶數後面的第一個奇數,進行交換
複製代碼
相關文章
相關標籤/搜索