Java Stream流用法
Stream流介绍是一个来自数据源的元素源的元素队列并支持聚合操作
作用范围
数组 Arrays.stream()
单列集合Collection
双列集合Map
**Stream.of()**(直接构造流)
文件行流(Files.lines())
函数生成流(Stream.generate() / Stream.iterate())
中间操作
过滤:filter
映射:map, flatMap
排序:sorted
去重:distinct
截断/跳过:limit, skip
终止操作遍历:forEach
聚合:reduce
收集:collect(Collectors.toList())
统计:count, max, min
匹配:anyMatch, allMatch, noneMatch
查找:findFirst, findAny
使用场景练习练习112345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 ...
刷题计划Java版
Java刷题计划工作原因需要过可信考试,题型为算法题,所以重新捡起秋招准备机考的题目。只不过本次准备的是Java语言,秋招时准备的都是Python,语言的切换在一开始是存在阵痛期的,各种api的不熟悉,以及书写方式的不同,带来一定的不适应,但总要面对这样的过程,因此写下本篇。
数组搜索插入位置本题利用的是二分查找的思路,但是在最后一次查找的时候需要记录位置
1234567891011121314151617181920212223242526272829303132package 数组;/** * @program: SoftExam * @description: * @author: 郭寅之(Clay_Guo) * @create: 2025/7/30 22:23 **/public class 搜索插入位置 { public static void main(String[] args) { int[] nums = {1, 3, 5, 6}; int target = 0; System.o ...
HashMap
123
123
HashMap的遍历方式
HashMap的遍历方式EntrySet遍历KeySet遍历
常见设计模式总结
常见设计模式总结
Spring中和JAVA IO中就涉及到设计模式,具体包括以下几个:
工厂模式单例模式双重锁检查法单例模式
策略模式装饰器模式装饰器模式是指在不改变原有对象的情况下扩展功能。
LeetCode Hot100题解
哈希1、两数之和123456789101112131415class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); int[] res = new int[2]; for (int i = 0; i < nums.length; i++) { if (map.containsKey(target - nums[i])) { res[0] = i; res[1] = map.get(target - nums[i]); return res; } map.put(nums[i], i); } return res; ...
堆
堆堆堆是一种基于树结构的数据结构,具有高效的插入和删除操作。
堆本质是完全二叉树,常用的两种堆分别是:
最大堆:父节点的值小于或等于其子节点的值
最小堆:父节点的值大于或等于其子节点的值
堆通常用于实现优先队列或者堆排序等算法
1234567891011121314import heapq# 创建最小堆heap = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]heapq.heapify(heap)# 插入元素heapq.heappush(heap, 0)# 弹出最小元素min_element = heapq.heappop(heap)print("Min Heap:", heap)print("Min Element:", min_element)
优先队列/堆堆排序
前缀和整理
前缀和
2024年3月9日美团笔试 前缀和 * 1
2024年3月13日 携程笔试 前缀和 * 1
练习题2602、使数组元素全部相等的最少操作次数
本题类似于携程的笔试题,暴力算法的时间复杂度为O(n²),n=10^5,铁超时
12345678910class Solution: def minOperations(self, nums: List[int], queries: List[int]) -> List[int]: res = [] for q in queries: operation = 0 for i in range(len(nums)): operation += abs(nums[i]-q) res.append(operation) return res
正确思路:
前缀和+二分查找
构造前缀和:
12345678910111213class Solution: def minOperation ...
树与二叉树(Java版本)
树与二叉树二叉树的结构123456789101112public class TreeNode{ int val; TreeNode left; TreeNode right; TreeNode(){} TreeNode(int val){this.val = val} TreeNode(int val, TreeNode left, TreeNode right){ this.val=val; this.left = left; this.right = right; }}
二叉树的递归遍历算法先序遍历【递归】1234567891011121314151617class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); preorder(root ...
图论
图论在图论中,主要的遍历方法就是深度优先搜索和广度优先搜索
图的存储方式主要有以下两种:
邻接矩阵(二维数组)
邻接表
深度优先搜索DFS深搜三部曲
1、确定递归函数和参数
2、确定终止条件
3、处理目前搜索节点出发的路径
代码框架12345void dfs(参数) { 处理节点 dfs(图,选择的节点); // 递归 回溯,撤销处理结果}
1234567891011direction = [(1, 0), (-1, 0), (0, 1), (0, -1)]def dfs(grid, visited, x, y): for dx, dy in direction: nextx = x + dx nexty = y + dy if nextx < 0 or nextx >= len(grid) or nexty < 0 or nexty >= len(grid[0]): continue if not visited[nextx][ne ...