面试题4-算法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27232757/article/details/83474056

本文是为了总结面试的时候经常会遇到的算法面试题,面试之前回顾一下这些算法题有助于调高成功率。所有算法的实现都是基于Java,希望对各位读者有帮助!!!

1.最长公共子串

最长公共子串是为了考量大家的动态规划算法,如果想了解动态规划算法可以去https://blog.csdn.net/baidu_37107022/article/details/73188963查看。

public int LCS (String str1, String str2) {
    int length = str1.length();
    int high = str2.length();
    int result = 0;
    int[][] arr = new int[length+1][high+1];
    for (int i = 1; i < arr.length; i++) {
        for (int j = 1; j < arr[i].length; j++) {
            if (str1.charAt(i-1)==str2.charAt(j-1)) {
                if (arr[i-1][j-1] == 0) {
                    arr[i][j] = 1;
                } else {
                    arr[i][j] = arr[i-1][j-1] + 1;
                }
            }
            if (arr[i][j] > result) {
                result = arr[i][j];
            }
        }
    }
    return result;
}

2.二叉树中两个节点的最近的公共父节点

这个问题分为几种情况:

1)如果遍历节点的左右子树分别存在找查找的两个节点,那么遍历的节点就是最近的公共父节点;

2)如果遍历节点的左子树或者右子树存在目标节点,返回存在的节点;

3)如果要遍历的节点的左子树和右子树都查不到目标节点,返回null;

public Integer findAncestor(TreeNode root, int child1, int child2) {
    if (root == null) {
        return null;
    }
    if (root.value == child1 || root.value == child2) {
        return root.value;
    }
    Integer left = getParent(root.leftNode, child1, child2);
    Integer right = getParent(root.rightNode, child1, child2);
    if (left != null && right != null) {
        return root.value;
    }
    if (left == null && right == null) {
        return null;
    }
    return left==null?right:left;
}

3.根节点到某个节点的路径

public String getPath(TreeNode root, int target, String path) {
    if (root== null) {
        return path;
    }
    path+=root.value;
    if (root.value == target) {
        return path;
    }
    path = getPath(root.leftNode, target, path);
    path = getPath(root.rightNode, target, path);
    if (!path.equals("") && !path.endsWith(target+"")) { //删除的是否一定要加入判断,否则只会输出一个元素
        path = path.substring(0, path.length()-1);
    }
    return path;
}

4.全排列(有重复元素、没有重复元素)

注意:下面只列出了没有重复元素的代码,有重复元素的话,加入判断

private void allPermutation(char[] c, LinkedList<String> listStr, int start){               
    if(start == c.length-1)
        listStr.add(String.valueOf(c));
    else{
        for(int i = start; i <= c.length-1; i++)
        {
            swap(c, i, start);//相当于: 固定第 i 个字符
            allPermutation(c, listStr, start+1);//求出这种情形下的所有排列
            swap(c, start, i);//复位,一定要复位,如果不复位下次循环的交换会乱套
        }
    }
}
  
private void swap(char[] c, int i, int j){ //交换元素
    char tmp = c[i];
    c[i] = c[j];
    c[j] = tmp;
}

5.堆排序

注意:要注意堆的构建,大(小)堆的构建和替换元素之后的重新调整都要会,查找前K个最大的元素,用小顶堆实现的。

public static void heapSort(int[] array) {
    if (array == null || array.length == 1) {
        return;
    }
    int j = 0;
    for (int i = array.length-1; i >= 0; i--) {
        maxHeapify(array, array.length-j);
        swap(array, 0, array.length-1-j);
        j++;
    }
}

public static void maxHeapify(int[] arrays, int size) {
    // 从数组的尾部开始,直到第一个元素(角标为0)
    for (int i = size - 1; i >= 0; i--) {
        buildMaxHeap(arrays, size, i);
    }
}

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

private static void buildMaxHeap(int[] array, int heapSize, int index) {
    int left = 2*index+1;
    int right = 2*index+2;
    int max = index;
    if (left < heapSize && array[left] > array[max]) {
        max = left;
    }
    if (right < heapSize && array[right] > array[max]) {
        max = right;
    }
    if (max != index) {
        swap(array, index, max);
        buildMaxHeap(array, heapSize, max);
    }
}

面试题1-Java基础:https://blog.csdn.net/qq_27232757/article/details/83032601

面试题2-操作系统和计算机网络:https://blog.csdn.net/qq_27232757/article/details/83446164

面试题3-Spring和Mybatis:https://blog.csdn.net/qq_27232757/article/details/83446183

猜你喜欢

转载自blog.csdn.net/qq_27232757/article/details/83474056
今日推荐