面试经典题11
题目描述: 代码: package interview;import java.util.Arrays;/** * @author hxw * @create 2025-05-03-20:02 * @Description: 274. H 指数 中等 */public class two_hundred_and_seventy_four { public static void main(String[] args) { int[] citations = new int[]{1,3,1}; System.out.println(new Solution().hIndex(citations)); } /** * 目的:给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数。计算并返回该研究者的 h 指数。 * 根据维基百科上 h 指数的定义:h 代表“高引用次数” ,一名科研人员的 h 指数 是指他(她)至少发表了 h...
面试经典题10
题目描述: 代码: package interview;/** * @author hxw * @create 2025-05-03-19:51 * @Description: 45. 跳跃游戏 II 中等 */public class forty_five { public static void main(String[] args) { int[] prices = new int[]{2,3,1,1,4}; System.out.println(new Solution().jump(prices)); } /** * 目的:给定一个长度为 n 的 0 索引整数数组 nums。初始位置为 nums[0]。 * 每个元素 nums[i] 表示从索引 i 向后跳转的最大长度。换句话说,如果你在 nums[i] 处,你可以跳转到任意 nums[i + j] 处: * 0 <= j <= nums[i] * i + j < n * 返回到达...
面试经典题9
题目描述: 代码: package interview;/** * @author hxw * @create 2025-05-02-20:29 * @Description: 55. 跳跃游戏 中等 */public class fifty_five { public static void main(String[] args) { int[] prices = new int[]{7, 1, 5, 3, 6, 4}; System.out.println(new Solution().canJump(prices)); } /** * 目的:给你一个非负整数数组 nums ,你最初位于数组的 第一个下标 。数组中的每个元素代表你在该位置可以跳跃的最大长度。 * 判断你是否能够到达最后一个下标,如果可以,返回 true ;否则,返回 false 。 * * 思路:暴力 只判断有0的情况 */ static class...
面试经典题8
题目描述: 代码: package interview;/** * @author hxw * @create 2025-05-02-20:15 * @Description: 122. 买卖股票的最佳时机 II 中等 */public class one_hundred_and_twenty_two { public static void main(String[] args) { int[] prices = new int[]{7, 1, 5, 3, 6, 4}; int result = Solution.maxProfit(prices); System.out.println(result); } /** * 目的:给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。 * 在每一天,你可以决定是否购买或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。 ...
面试经典题7
题目描述: 代码: package interview;import java.util.Arrays;/** * @author hxw * @create 2025-05-02-19:50 * @Description: 121. 买卖股票的最佳时机 简单 */public class one_hundred_and_twenty_one { public static void main(String[] args) { int result = Solution.maxProfit(new int[]{7, 1, 5, 3, 6, 4}); System.out.println(result); } /** * 目的:给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。 * 你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。 *...
面试经典题6
题目描述: 代码: package interview;/** * @author hxw * @create 2025-05-01-21:11 * @Description: 189. 轮转数组 中等 */public class one_hundred_and_eighty_nine { public static void main(String[] args) { int[] nums = new int[]{1, 2, 3, 4, 5, 6, 7}; new Solution().rotate(nums, 3); for (int i = 0; i < nums.length; i++) { System.out.println(nums[i]); } } /** * 目的:给定一个整数数组 nums,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。 *...
面试经典题5
题目描述: 代码: package interview;/** * @author hxw * @create 2025-05-01-21:06 * @Description: 169. 多数元素 简单 */public class One_hundred_and_sixty_nine { public static void main(String[] args) { int[] nums = new int[]{3, 2, 2}; System.out.println(new Solution().majorityElement(nums)); } /** * 目的:给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。 * 思路:多数元素出现的次数大于⌊ n/2 ⌋,因此我们可以使用 Moore 投票算法来解决这个问题。 */ static class Solution...
面试经典题4
题目描述: 代码: package interview;/** * @author hxw * @create 2025-05-01-20:36 * @Description: 80. 删除有序数组中的重复项 II 中等 */public class eighty { public static void main(String[] args) { int[] nums = new int[]{0, 0, 1, 1, 1, 1, 2, 3, 3}; int[] expectedNums = new int[]{0, 0, 1, 1, 1, 1, 2, 3, 3}; int k = Solution.removeDuplicates(nums); assert k == expectedNums.length; for (int i = 0; i < k; i++) { assert nums[i] ==...
面试经典题3
题目描述: 代码: package interview;/** * @author hxw * @create 2025-04-30-21:32 * @Description: 26. 删除有序数组中的重复项 简单 */public class twenty_six { public static void main(String[] args) { int[] nums = new int[]{1, 1, 2}; // 输入数组 int[] expectedNums = new int[]{1, 2}; // 长度正确的期望答案 int k = Solution.removeDuplicates(nums); // 调用 assert k == expectedNums.length; for (int i = 0; i < k; i++) { assert nums[i] ==...
面试经典题2
题目描述: 代码: package interview;import static java.util.Arrays.sort;/** * @author hxw * @create 2025-04-30-21:02 * @Description: 27. 移除元素 简单 */public class twenty_seven { public static void main(String[] args) { int[] nums = new int[]{0, 1, 2, 2, 3, 0, 4, 2}; // 输入数组 int val = 2; // 要移除的值 int[] expectedNums = new int[]{0, 1, 4, 0, 3}; // 长度正确的预期答案。 // 它以不等于 val 的值排序。 int k = Solution.removeElement(nums, val); // 调用你的实现 ...