中高级Java面试题解析,剑指BATJ,提前祝大家程序员节快乐

在线体验各类最新模型,更有模型 免费Token 额度领取!
立即体验
简介: 本站小福利 点我获取阿里云优惠券为什么大多数程序员相进BAT工作?在中国互联网技术发展过程中,BAT带给我们程序员太多的回忆,20年发展过程中,他们各自形成自己的的体系和战略规划,掌握着中国互联网信息技术,很多新技术都是BAT创新,然后提供技术支持给我们普通的开发者,这就是程序员进入BAT工作最有力的说服力。

本站小福利 点我获取阿里云优惠券

为什么大多数程序员相进BAT工作?

在中国互联网技术发展过程中,BAT带给我们程序员太多的回忆,20年发展过程中,他们各自形成自己的的体系和战略规划,掌握着中国互联网信息技术,很多新技术都是BAT创新,然后提供技术支持给我们普通的开发者,这就是程序员进入BAT工作最有力的说服力。

第一题:二叉搜索树与双向链表
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。

解题思路
由于 BST 的特性,采用中序遍历正好符合排序
要考虑 root 节点要与 左节点的最大值连接,与右节点的最小值连接
增加一个已排序链表的指针,指向最后一个已排序节点
public TreeNode Convert(TreeNode pRootOfTree) {

if (pRootOfTree == null) {
    return null;
}
TreeNode[] nodeList = {new TreeNode(-1)

}
;
ConvertToLink(pRootOfTree, nodeList);
TreeNode cursor = pRootOfTree;
while (cursor.left != null) {

cursor = cursor.left;

}
cursor.right.left = null;
return cursor.right;
}
private void ConvertToLink(TreeNode root, TreeNode[] nodeList) {
if (root == null) {

return;

}
ConvertToLink(root.left, nodeList);
root.left = nodeList[0];
nodeList[0].right = root;
nodeList[0] = root;
ConvertToLink(root.right, nodeList);
}
第二题:合并两个排序的链表
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

解题思路
双指针指向两个链表
循环选取最小值,加入结果集
public ListNode Merge(ListNode list1, ListNode list2) {

ListNode head = new ListNode(-1);
ListNode cursor = head;
while (list1 != null || list2 != null) {
    if (list1 == null) {
        while (list2 != null) {
            cursor.next = list2;
            cursor = cursor.next;
            list2 = list2.next;
        }
        continue;
    }
    if (list2 == null) {
        while (list1 != null) {
            cursor.next = list1;
            cursor = cursor.next;
            list1 = list1.next;
        }
        continue;
    }
    if (list1.val < list2.val) {
        cursor.next = list1;
        cursor = cursor.next;
        list1 = list1.next;
    } else {
        cursor.next = list2;
        cursor = cursor.next;
        list2 = list2.next;
    }
}
return head.next;

}
第三题:树的子结构
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

解题思路
遍历查找相等根节点
通过递归查找当前根节点下是否包含子树 root2
public Boolean HasSubtree(TreeNode root1, TreeNode root2) {

if (root2 == null) {
    return false;
}
LinkedList<TreeNode> pipeline = new LinkedList<>();
pipeline.addLast(root1);
while (!pipeline.isEmpty()) {
    TreeNode node = pipeline.pop();
    if (node == null) {
        continue;
    }
    pipeline.addLast(node.left);
    pipeline.addLast(node.right);
    if (node.val == root2.val && isSub(node, root2)) {
        return true;
    }
}
return false;

}
private Boolean isSub(TreeNode root1, TreeNode root2) {

if (root1 == null && root2 == null) {
    return true;
}
if (root1 == null) {
    return false;
}
if (root2 == null) {
    return true;
}
if (root1.val == root2.val) {
    return isSub(root1.left, root2.left) && isSub(root1.right, root2.right);
} else {
    return false;
}

}
第四题:二叉树的镜像
操作给定的二叉树,将其变换为源二叉树的镜像。

输入描述:

二叉树的镜像定义:源二叉树

        8
       /  \
      6   10
     / \  / \
    5  7 9 11
    镜像二叉树
        8
       /  \
      10   6
     / \  / \
    11 9 7  5

解题思路
从上到下进行左右节点交换
public void Mirror(TreeNode root) {

if (root == null) return;
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
Mirror(root.left);
Mirror(root.right);

}
第五题:顺时针打印矩阵
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

解题思路
通过4个指针,表示可打印区域,并对区域进行收缩
非 n*n 的矩阵,对于剩余非 4 边遍历的元素,要考虑边界
public ArrayList printMatrix(int[][] matrix) {

ArrayList<Integer> res = new ArrayList<>();
if (matrix.length == 0) {
    return res;
}
if (matrix.length == 1) {
    for (int i : matrix[0]) {
        res.add(i);
    }
    return res;
}
int top = 0, bottom = matrix.length - 1, left = 0, right = matrix[0].length - 1;
for (; left <= right && top <= bottom; ) {
    if (top == bottom) {
        for (int i = left; i <= right; i++) {
            res.add(matrix[top][i]);
        }
        break;
    }
    if (left == right) {
        for (int i = top; i <= bottom; i++) {
            res.add(matrix[i][left]);
        }
        break;
    }
    for (int p = left; p <= right; p++) {
        res.add(matrix[top][p]);
    }
    top++;
    for (int p = top; p <= bottom; p++) {
        res.add(matrix[p][right]);
    }
    right--;
    for (int p = right; p >= left; p--) {
        res.add(matrix[bottom][p]);
    }
    bottom--;
    for (int p = bottom; p >= top; p--) {
        res.add(matrix[p][left]);
    }
    left++;
}
return res;

}
第六题:包含min函数的栈
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的 min 函数(时间复杂度应为O(1))。

解题思路
通过增加最小栈来记录当前最小节点
private LinkedList stack = new LinkedList<>();
private LinkedList min = new LinkedList<>();
public void push(int node) {

stack.addLast(node);
if (min.isEmpty()) {
    min.addLast(node);
    return;
}
if (node < min.peekLast()) {
    min.addLast(node);
} else {
    min.addLast(min.peekLast());
}

}
public void pop() {

if (stack.isEmpty()) {
    return;
}
stack.removeLast();
min.removeLast();

}
public int top() {

if (stack.peekLast() == null) {
    return 0;
}
return stack.peekLast();

}
public int min() {

if (min.peekLast() == null) {
    return 0;
}
return min.peekLast();

}
第七题:栈的压入、弹出序列
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)

解题思路
通过 Stack 进行模拟 push,当 pop 的节点等于 Stack 的 top 节点时,pop Stack
最后如果 Stack 剩余数据,则判定为 false
public Boolean IsPopOrder(int[] pushA, int[] popA) {

if (pushA.length != popA.length) {
    return false;
}
if (pushA.length == 0) {
    return false;
}
LinkedList<Integer> stack = new LinkedList<>();
int j = 0;
for (int value : pushA) {
    stack.addLast(value);
    while (stack.peekLast() != null && popA[j] == stack.getLast()) {
        j++;
        stack.removeLast();
    }
}
return stack.isEmpty();

}
第八题:从上往下打印二叉树
从上往下打印出二叉树的每个节点,同层节点从左至右打印。

解题思路
层次遍历,通过队列进行辅助遍历
public ArrayList PrintFromTopToBottom(TreeNode root) {

ArrayList<Integer> res = new ArrayList<>();
LinkedList<TreeNode> nodeQueue = new LinkedList<>();
if (root == null) {
    return res;
}
nodeQueue.addLast(root);
while (!nodeQueue.isEmpty()) {
    TreeNode node = nodeQueue.pollFirst();
    if (node == null) {
        continue;
    }
    nodeQueue.addLast(node.left);
    nodeQueue.addLast(node.right);
    res.add(node.val);
}
return res;

}
第九题:二叉搜索树的后序遍历序列
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出 Yes ,否则输出 No 。假设输入的数组的任意两个数字都互不相同。

解题思路
后序遍历中,最后一个节点为 root 节点
由于 BST 的左子树都小于 root,右子树都大于 root,那么可以判定该节点是否为 BST
依次类推,通过递归方式,再判定左右子树
public Boolean VerifySquenceOfBST(int[] sequence) {

if (sequence.length == 0) {
    return false;
}
if (sequence.length == 1) {
    return true;
}
return isBST(sequence, 0, sequence.length - 1);

}
private Boolean isBST(int[] sequence, int start, int end) {

if (start < 0 || end < 0 || start >= end) {
    return true;
}
int rootV = sequence[end];
int rightIndex = -1, rightV = Integer.MIN_VALUE;
for (int i = start; i < end; i++) {
    if (rightV == Integer.MIN_VALUE && sequence[i] > rootV) {
        rightV = sequence[i];
        rightIndex = i;
        continue;
    }
    if (rightV != Integer.MIN_VALUE && sequence[i] < rootV) {
        return false;
    }
}
return isBST(sequence, start, rightIndex - 1) && isBST(sequence, rightIndex, end - 1);

}
第十题:二叉树中和为某一值的路径
输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的 list 中,数组长度大的数组靠前)

解题思路
将走过的路径记录下来,当走过路径总和 = target 并且当前节点是叶子节点时,该路径符合要求
通过递归遍历所有可能的路径
public ArrayList> FindPath(TreeNode root, int target) {

ArrayList<ArrayList<Integer>> res = new ArrayList<>();
FindPath(res, new LinkedList<>(), root, 0, target);
res.sort(Comparator.comparingint(list -> -list.size()));
return res;

}
private void FindPath(ArrayList> res,

              LinkedList<Integer> path,
              TreeNode node,
              int pathSum,
              int target) {
if (node == null) {
    return;
}
if (pathSum > target) {
    return;
}
if (pathSum + node.val == target && node.right == null && node.left == null) {
    ArrayList<Integer> resPath = new ArrayList<>(path);
    resPath.add(node.val);
    res.add(resPath);
    return;
}
path.addLast(node.val);
if (node.left != null) {
    FindPath(res, path, node.left, pathSum + node.val, target);
}
if (node.right != null) {
    FindPath(res, path, node.right, pathSum + node.val, target);
}
path.removeLast();

}
第十一题:复杂链表的复制
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的 head 。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)

解题思路
复制每个节点,如:复制节点 A 得到 A1 ,将 A1 插入节点 A 后面
遍历链表,并将 A1->random = A->random->next;
将链表拆分成原链表和复制后的链表
public RandomListNode Clone(RandomListNode pHead) {

if (pHead == null) {
    return null;
}
RandomListNode cursor = pHead;
while (cursor != null) {
    RandomListNode copyNode = new RandomListNode(cursor.label);
    RandomListNode nextNode = cursor.next;
    cursor.next = copyNode;
    copyNode.next = nextNode;
    cursor = nextNode;
}
cursor = pHead;
while (cursor != null) {
    RandomListNode copyNode = cursor.next;
    if (cursor.random == null) {
        cursor = copyNode.next;
        continue;
    }
    copyNode.random = cursor.random.next;
    cursor = copyNode.next;
}
RandomListNode copyHead = pHead.next;
cursor = pHead;
while (cursor.next != null) {
    RandomListNode copyNode = cursor.next;
    cursor.next = copyNode.next;
    cursor = copyNode;
}
return copyHead;

}
第十二题:字符串的排列
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。

输入描述:输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。

解题思路
将字符串划分为两个部分,第一个字符以及后面的其他字符
将第一个字符和后面所有字符进行交换
对于 abc 这个字符串,计算出的排列顺序为:

abc
acb
bac
bca
cba
cab
代码:

public ArrayList Permutation(String str) {

Set<String> res = new HashSet<>();
if (str == null || str.length() == 0) {
    return new ArrayList<>();
}
Permutation(res, str.toCharArray(), 0);
ArrayList<String> list = new ArrayList<>(res);
list.sort(String::compareTo);
return list;

}
private void Permutation(Set res, char[] chars, int start) {

if (start == chars.length) {
    res.add(new String(chars));
    return;
}
for (int i = start; i < chars.length; i++) {
    swap(chars, start, i);
    Permutation(res, chars, start + 1);
    swap(chars, start, i);
}

}
private void swap(char[] chars, int i, int j) {

char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;

}
第十三题:数组中出现次数超过一半的数字
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出 2 。如果不存在则输出 0。

解题思路
由于数组的特性,在排序数组中,超过半数的数字一定包含中位数
通过 partition 方法,借用快排的思想,随机选取一个 key,将数组中小于 key 的移动到 key 的左侧,数组中大于 key 的移动到 key 的右侧
最终找到中位数的下标,还需要检查中位数是否超过半数
public int MoreThanHalfNum_Solution(int[] array) {

int start = 0, end = array.length - 1;
int mid = array.length / 2;
int index = partition(array, start, end);
if (index == mid) {
    return array[index];
}
while (index != mid && start <= end) {
    if (index > mid) {
        end = index - 1;
        index = partition(array, start, end);
    } else {
        start = index + 1;
        index = partition(array, start, end);
    }
}
if (checkIsHalf(array, index)) return array[index];
return 0;

}
private Boolean checkIsHalf(int[] array, int index) {

if (index < 0) {
    return false;
}
int count = 0;
for (int i : array) {
    if (array[index] == i) {
        count++;
    }
}
return count > array.length / 2;

}
private int partition(int[] array, int start, int end) {

if (start >= array.length || start < 0
        || end >= array.length || end < 0) {
    return -1;
}
int key = array[start];
int left = start, right = end;
while (left < right) {
    while (left < right && array[right] >= key) {
        right--;
    }
    if (left < right) {
        array[left] = array[right];
        left++;
    }
    while (left < right && array[left] <= key) {
        left++;
    }
    if (left < right) {
        array[right] = array[left];
        right--;
    }
}
array[left] = key;
return left;

}
第十四题:最小的K个数
输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。

解题思路

  1. Partition
    该算法基于 Partition

public ArrayList GetLeastNumbers_Solution_Partition(int[] input, int k) {

ArrayList<Integer> res = new ArrayList<>();
if (k > input.length || k < 1) {
    return res;
}
int start = 0, end = input.length - 1;
int index = partition(input, start, end);
while (index != k - 1) {
    if (index > k - 1) {
        end = index - 1;
        index = partition(input, start, end);
    } else {
        start = index + 1;
        index = partition(input, start, end);
    }
}
for (int i = 0; i < input.length && i < k; i++) {
    res.add(input[i]);
}
return res;

}
private int partition(int[] nums, int start, int end) {

int left = start, right = end;
int key = nums[left];
while (left < right) {
    while (left < right && nums[right] > key) {
        right--;
    }
    if (left < right) {
        nums[left] = nums[right];
        left++;
    }
    while (left < right && nums[left] <= key) {
        left++;
    }
    if (left < right) {
        nums[right] = nums[left];
        right++;
    }
}
nums[left] = key;
return left;

}

  1. 小根堆算法
    该算法基于小根堆,适合海量数据,时间复杂度为:n*logk

public ArrayList GetLeastNumbers_Solution(int[] input, int k) {

ArrayList<Integer> res = new ArrayList<>();
if (k > input.length||k==0) {
    return res;
}
for (int i = input.length - 1; i >= 0; i--) {
    minHeap(input, 0, i);
    swap(input, 0, i);
    res.add(input[i]);
    if (res.size() == k) break;
}
return res;

}
private void minHeap(int[] heap, int start, int end) {

if (start == end) {
    return;
}
int childLeft = start * 2 + 1;
int childRight = childLeft + 1;
if (childLeft <= end) {
    minHeap(heap, childLeft, end);
    if (heap[childLeft] < heap[start]) {
        swap(heap, start, childLeft);
    }
}
if (childRight <= end) {
    minHeap(heap, childRight, end);
    if (heap[childRight] < heap[start]) {
        swap(heap, start, childRight);
    }
}

}
private void swap(int[] nums, int a, int b) {

int t = nums[a];
nums[a] = nums[b];
nums[b] = t;

}
第十五题:连续子数组的最大和
例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。给一个数组,返回它的最大连续子序列的和,你会不会被他忽悠住?(子向量的长度至少是1)

解题思路
通过动态规划计算最大和,

$$ f(i) $$

定义为以第

$$ i $$

个数字结尾的子数组的最大和,那么

$$ max(f(i)) $$

就有以下公式:

$$ max(f(i))=begin{cases} num[i] & i=0 or f(i)<0\ num[i]+f(i) & ine0 and f(i)>0 end{ cases } $$

public int FindGreatestSumOfSubArray(int[] array) {

if (array == null || array.length == 0) {
    return 0;
}
int max = array[0];
int sum = 0;
for (int a : array) {
    if (sum + a > a) {
        sum += a;
    } else {
        sum = a;
    }
    if (sum > max) {
        max = sum;
    }
}
return max;

}

我的官网
在这里插入图片描述
我的官网http://guan2ye.com

我的CSDN地址http://blog.csdn.net/chenjianandiyi

我的简书地址http://www.jianshu.com/u/9b5d1921ce34

我的githubhttps://github.com/javanan

我的码云地址https://gitee.com/jamen/

阿里云优惠券https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=vf2b5zld

相关文章
|
9月前
|
存储 缓存 NoSQL
Redis常见面试题全解析
Redis面试高频考点全解析:从过期删除、内存淘汰策略,到缓存雪崩、击穿、穿透及BigKey问题,深入原理与实战解决方案,助你轻松应对技术挑战,提升系统性能与稳定性。(238字)
|
11月前
|
存储 安全 测试技术
Python面试题精选及解析
本文详解Python面试中的六大道经典问题,涵盖列表与元组区别、深浅拷贝、`__new__`与`__init__`、GIL影响、协程原理及可变与不可变类型,助你提升逻辑思维与问题解决能力,全面备战Python技术面试。
656 1
|
Web App开发 缓存 前端开发
浏览器常见面试题目及详细答案解析
本文围绕浏览器常见面试题及答案展开,深入解析浏览器组成、内核、渲染机制与缓存等核心知识点。内容涵盖浏览器的主要组成部分(如用户界面、呈现引擎、JavaScript解释器等)、主流浏览器内核及其特点、从输入URL到页面呈现的全过程,以及CSS加载对渲染的影响等。结合实际应用场景,帮助读者全面掌握浏览器工作原理,为前端开发和面试提供扎实的知识储备。
478 4
|
9月前
|
监控 Java 关系型数据库
面试性能测试总被刷?学员真实遇到的高频问题全解析!
面试常被性能测试题难住?其实考的不是工具,而是分析思维。从脚本编写到瓶颈定位,企业更看重系统理解与实战能力。本文拆解高频面试题,揭示背后考察逻辑,并通过真实项目训练,帮你构建性能测试完整知识体系,实现从“会操作”到“能解决问题”的跨越。
|
存储 安全 Java
2025 最新史上最全 Java 面试题独家整理带详细答案及解析
本文从Java基础、面向对象、多线程与并发等方面详细解析常见面试题及答案,并结合实际应用帮助理解。内容涵盖基本数据类型、自动装箱拆箱、String类区别,面向对象三大特性(封装、继承、多态),线程创建与安全问题解决方法,以及集合框架如ArrayList与LinkedList的对比和HashMap工作原理。适合准备面试或深入学习Java的开发者参考。附代码获取链接:[点此下载](https://pan.quark.cn/s/14fcf913bae6)。
6457 50
|
前端开发 JavaScript 开发者
2025 最新 100 道 CSS 面试题及答案解析续篇
本文整理了100道CSS面试题及其答案,涵盖CSS基础与进阶知识。内容包括CSS引入方式、盒模型、选择器优先级等核心知识点,并通过按钮、卡片、导航栏等组件封装实例,讲解单一职责原则、样式隔离、响应式设计等最佳实践。适合前端开发者巩固基础、备战面试或提升组件化开发能力。资源地址:[点击下载](https://pan.quark.cn/s/50438c9ee7c0)。
287 5
2025 最新 100 道 CSS 面试题及答案解析续篇
|
NoSQL Java 微服务
2025 年最新 Java 面试从基础到微服务实战指南全解析
《Java面试实战指南:高并发与微服务架构解析》 本文针对Java开发者提供2025版面试技术要点,涵盖高并发电商系统设计、微服务架构实现及性能优化方案。核心内容包括:1)基于Spring Cloud和云原生技术的系统架构设计;2)JWT认证、Seata分布式事务等核心模块代码实现;3)数据库查询优化与高并发处理方案,响应时间从500ms优化至80ms;4)微服务调用可靠性保障方案。文章通过实战案例展现Java最新技术栈(Java 17/Spring Boot 3.2)的应用.
1042 9
|
缓存 NoSQL Java
Java Redis 面试题集锦 常见高频面试题目及解析
本文总结了Redis在Java中的核心面试题,包括数据类型操作、单线程高性能原理、键过期策略及分布式锁实现等关键内容。通过Jedis代码示例展示了String、List等数据类型的操作方法,讲解了惰性删除和定期删除相结合的过期策略,并提供了Spring Boot配置Redis过期时间的方案。文章还探讨了缓存穿透、雪崩等问题解决方案,以及基于Redis的分布式锁实现,帮助开发者全面掌握Redis在Java应用中的实践要点。
702 6
|
设计模式 安全 Java
Java 基础知识面试题全解析之技术方案与应用实例详解
本内容结合Java 8+新特性与实际场景,涵盖函数式编程、Stream API、模块化、并发工具等技术。通过Lambda表达式、Stream集合操作、Optional空值处理、CompletableFuture异步编程等完整示例代码,助你掌握现代Java应用开发。附面试题解析与技术方案,提升实战能力。代码示例涵盖计算器、员工信息统计、用户查询、模块化系统设计等,助你轻松应对技术挑战。
363 9
|
缓存 Java 索引
2025 年 Java 面试必备最常见 200 + 面试题及答案解析
这是一份针对Java面试的综合技术方案与应用实例文档,涵盖Java基础(JDK/JRE、字符串、IO流)、容器(ArrayList/LinkedList、HashMap)、多线程(线程创建、同步、线程池)、数据库(MySQL索引、Redis缓存穿透)及Spring框架(IOC容器、热部署)等核心模块。通过详细解析常见面试题,帮助读者深入理解并掌握Java核心技术,为应对面试提供全面指导。此外,还涉及反射、设计模式、JVM调优等进阶内容,助力开发者全面提升技能。代码示例可从提供的链接下载。
1538 6

推荐镜像

更多
  • DNS