面试必备算法整理

1.排序

在线测试连接
1.1 快排:

class Solution {
    public int[] sortArray(int[] nums) {
        if (nums == null || nums.length < 2) {
            return nums;
        }
        quickSort(nums, 0, nums.length - 1);
        return nums;
    }

    private void quickSort(int[] nums, int l, int r) {
        if (l < r) {
            swap(nums, r, l+(int) (Math.random() * (r - l + 1)));
            int[] p = partition(nums,l,r);
            quickSort(nums,l,p[0]);
            quickSort(nums,p[1]+1,r);
        }
    }

    private int[] partition(int[] nums,int l ,int r) {
        int less = l - 1;
        int more = r;
        while (l < more){
            if(nums[l] < nums[r]){
                swap(nums,l++,++less);
            }else if(nums[l] > nums[r]){
                swap(nums,l,--more);
            }else {
                l++;
            }
        }
        swap(nums,more,r);
        return new int[]{less,more};
    }

    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

1.2: 归并:

class Solution {
    public int[] sortArray(int[] nums) {
        if (nums == null || nums.length < 2) {
            return nums;
        }
        mergeSort(nums, 0, nums.length - 1);
        return nums;
    }

    private void mergeSort(int[] nums, int l, int r) {
        if (l == r) {
            return;
        }
        int mid = l + (r - l) / 2;
        mergeSort(nums, l, mid);
        mergeSort(nums, mid + 1, r);
        merge(nums,l,mid,r);
    }

    private void merge(int[] nums, int l, int mid, int r) {
        int[] help = new int[r - l + 1];
        int i = 0;
        int p1 = l;
        int p2 = mid + 1;
        while (p1 <= mid && p2 <= r) {
            help[i++] = nums[p1] <= nums[p2] ? nums[p1++] : nums[p2++];
        }
        while (p1 <= mid) {
            help[i++] = nums[p1++];
        }
        while (p2 <= r) {
            help[i++] = nums[p2++];
        }
        for (i = 0; i < help.length; i++) {
            nums[l + i] = help[i];
        }
    }
}

1.3 堆排序:

class Solution {
    public int[] sortArray(int[] nums) {
        if (nums == null || nums.length == 0) {
            return nums;
        }
        for (int i = 0; i < nums.length; i++) {
            heapInseet(nums, i);
        }
        int size = nums.length;
        swap(nums, 0, --size);
        while (size > 0){
            hepify(nums,0,size);
            swap(nums,0,--size);
        }
        return nums;
    }

    private void hepify(int[] nums, int i, int size) {
        int left = i * 2 + 1;
        while (left < size){
            int large = left + 1 < size && nums[left] < nums[left + 1]?left + 1 : left;
            large = nums[large] > nums[i]? large : i;
            if(large == i){
                break;
            }
            swap(nums,large,i);
            i = large;
            left = i * 2 + 1;
        }
    }

    private void heapInseet(int[] nums, int i) {
        while (nums[i] > nums[(i - 1) / 2]) {
            swap(nums, i, (i - 1) / 2);
            i = (i - 1) / 2;
        }
    }


    private void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}

1.3 top K问题:
在线测试连接

class Solution {
    public int findKthLargest(int[] nums, int k) {
        int index = nums.length - k;
        return partition(nums, 0, nums.length - 1, index);
    }
    private int partition(int[] nums, int l, int r, int k) {
        swap(nums, r, (int) (l + (r - l + 1) * Math.random()));
        int less = l - 1;
        int more = r;
        int left = l;
        while (l < more) {
            if (nums[l] < nums[r]) {
                swap(nums, ++less, l++);
            } else if (nums[l] > nums[r]) {
                swap(nums, l, --more);
            } else {
                l++;
            }
        }
        swap(nums, r, more);
        if (l > k) {
            return partition(nums, left, l - 1, k);
        } else if (l < k) {
            return partition(nums, l + 1, r, k);
        } else {
            return nums[l];
        }
    }
    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

2.树相关

2.1 树的前序遍历:
在线测试连接
递归:

class Solution {
    List<Integer> list = new ArrayList<>();
    public List<Integer> preorderTraversal(TreeNode root) {
        preorder(root);
        return list;
    }
    private void preorder(TreeNode root) {
        if(root != null){
            list.add(root.val);
            preorderTraversal(root.left);
            preorderTraversal(root.right);
        }
    }
}

非递归:

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root == null){
            return list;
        }
        Stack<TreeNode> st = new Stack<>();
        st.add(root);
        while (!st.isEmpty()){
            TreeNode p = st.pop();
            if(p.right != null){
                st.add(p.right);
            }
            if(p.left != null){
                st.add(p.left);
            }
            list.add(p.val);
        }
        return list;
    }
}

2.2 中序遍历:
在线测试连接
递归:

class Solution {
    List<Integer> list = new ArrayList<>();    
    public List<Integer> inorderTraversal(TreeNode root) {
        inorder(root);
        return list;
    }
    private void inorder(TreeNode root) {
        if(root != null){
            inorder(root.left);
            list.add(root.val);
            inorder(root.right);
        }
    }
}

非递归:

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root != null){
            Stack<TreeNode> st = new Stack<>();
            while (root != null || !st.isEmpty()){
                if(root != null){
                    st.add(root);
                    root = root.left;
                }else {
                    root = st.pop();
                    list.add(root.val);
                    root = root.right;
                }
            }
        }
        return list;
    }
}

2.3 后序遍历:
在线测试连接
递归:

class Solution {
    List<Integer> list = new ArrayList<>();
    public List<Integer> postorderTraversal(TreeNode root) {
        postorder(root);
        return list;
    }
    private void postorder(TreeNode root) {
        if(root != null){
            postorder(root.left);
            postorder(root.right);
            list.add(root.val);
        }
    }
}

非递归:

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root != null){
            Stack<TreeNode> st = new Stack<>();
            Stack<TreeNode> res = new Stack<>();
            st.add(root);
            while (!st.isEmpty()){
                TreeNode temp = st.pop();
                if(temp.left != null){
                    st.add(temp.left);
                }
                if(temp.right != null){
                    st.add(temp.right);
                }
                res.add(temp);
            }
            while (!res.isEmpty()){
                list.add(res.pop().val);
            }
        }
        return list;
    }
}

2.4 前缀树:
在线测试连接

class TrieNode {
    public int path;
    public int end;
    public TrieNode[] next;

    public TrieNode() {
        path = 0;
        end = 0;
        next = new TrieNode[26];
    }
}

class Trie {
    
    private TrieNode root;
    
    public Trie() {
        root = new TrieNode();
    }
    
    public void insert(String word) {
        if (word == null) {
            return;
        }
        char[] ch = word.toCharArray();
        TrieNode node = root;
        for (int i = 0; i < ch.length; i++) {
            int index = ch[i] - 'a';
            if (node.next[index] == null) {
                node.next[index] = new TrieNode();
            }
            node = node.next[index];
            node.path++;
        }
        node.end++;
    }

    public boolean search(String word) {
        if (word == null) {
            return false;
        }
        char[] ch = word.toCharArray();
        TrieNode node = root;
        for (int i = 0; i < ch.length; i++) {
            int index = ch[i] - 'a';
            if (node.next[index] == null) {
                return false;
            }
            node = node.next[index];
        }
        return node.end > 0;
    }

    public boolean startsWith(String prefix) {
        if (prefix == null) {
            return false;
        }
        char[] ch = prefix.toCharArray();
        TrieNode node = root;
        for (int i = 0; i < ch.length; i++) {
            int index = ch[i] - 'a';
            if (node.next[index] == null) {
                return false;
            }
            node = node.next[index];
        }
        return node.path > 0;
    }

    public void delete(String word) {
        if (search(word)) {
            char[] chs = word.toCharArray();
            TrieNode node = root;
            for (int i = 0; i < chs.length; i++) {
                int index = chs[i] - 'a';
                if (--node.next[index].path == 0) {
                    node.next[index] = null;
                    return;
                }
                node = node.next[index];
            }
            node.end--;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值