当前位置: 首页 > news >正文

Java中数组常见操作

在力扣算法题中,Java对数组的常见操作及其复杂度:

1. 排序操作

Arrays.sort(nums);                    // 时间复杂度: O(nlogn)
Arrays.sort(nums, 1, 4);             // 时间复杂度: O(klogk), k为排序范围大小// 自定义排序(需要转为Integer[])
Integer[] integerNums = Arrays.stream(nums).boxed().toArray(Integer[]::new);
Arrays.sort(integerNums, (a, b) -> b - a); // O(nlogn)

2. 二分查找

Arrays.binarySearch(nums, 5);        // 时间复杂度: O(logn)
Arrays.binarySearch(nums, 1, 4, 5);  // 时间复杂度: O(logk), k为查找范围大小

3. 数组填充和复制

Arrays.fill(nums, -1);               // 时间复杂度: O(n)
Arrays.fill(nums, 1, 4, 99);         // 时间复杂度: O(k), k为填充范围大小Arrays.copyOf(nums, nums.length);    // 时间复杂度: O(n), 空间复杂度: O(n)
Arrays.copyOfRange(nums, 1, 4);      // 时间复杂度: O(k), 空间复杂度: O(k)

4. 数组比较和转换

Arrays.equals(arr1, arr2);           // 时间复杂度: O(n)
Arrays.toString(arr1);               // 时间复杂度: O(n), 空间复杂度: O(n)
Arrays.deepToString(matrix);         // 时间复杂度: O(n×m), 空间复杂度: O(n×m)

5. Stream API 操作(Java 8+)

// 终端操作(触发计算)
Arrays.stream(nums).sum();           // 时间复杂度: O(n)
Arrays.stream(nums).max().getAsInt(); // 时间复杂度: O(n)
Arrays.stream(nums).min().getAsInt(); // 时间复杂度: O(n)
Arrays.stream(nums).average();       // 时间复杂度: O(n)// 中间操作 + 终端操作
Arrays.stream(nums).filter(x -> x % 2 == 0).toArray(); // 时间复杂度: O(n), 空间复杂度: O(n)
Arrays.stream(nums).map(x -> x * x).toArray();         // 时间复杂度: O(n), 空间复杂度: O(n)
Arrays.stream(nums).sorted().toArray();                // 时间复杂度: O(nlogn), 空间复杂度: O(n)
Arrays.stream(nums).distinct().toArray();              // 时间复杂度: O(n), 空间复杂度: O(n)

6. 其他实用操作

Arrays.setAll(nums, i -> i * i);     // 时间复杂度: O(n)// 数组转List(注意返回的List不可变)
Arrays.asList(1, 2, 3);             // 时间复杂度: O(n), 空间复杂度: O(n)// 可变List转换
List<Integer> list = Arrays.stream(nums).boxed().collect(Collectors.toList()); // O(n), O(n)
int[] array = list.stream().mapToInt(Integer::intValue).toArray();              // O(n), O(n)

7. 多维数组操作

int[][] matrix = new int[m][n];// 多维数组排序(按行排序)
Arrays.sort(matrix, (a, b) -> a[0] - b[0]); // 时间复杂度: O(mlogm × n)// 遍历多维数组
for (int[] row : matrix) {Arrays.sort(row);                // 总时间复杂度: O(m × nlogn)
}

8. 特殊操作复杂度分析

// 链式操作复杂度分析
int result = Arrays.stream(nums).filter(x -> x > 0)              // O(n).map(x -> x * 2)                 // O(n).distinct()                      // O(n).sorted()                        // O(nlogn).sum();                          // O(n)
// 总时间复杂度: O(nlogn)// 嵌套操作
for (int i = 0; i < n; i++) {Arrays.sort(matrix[i]);          // 总时间复杂度: O(m × nlogn)
}// 二分查找组合
Arrays.sort(nums);                   // O(nlogn)
for (int i = 0; i < n; i++) {Arrays.binarySearch(nums, target); // 总时间复杂度: O(nlogn)
}

复杂度总结表

操作 时间复杂度 空间复杂度 说明
Arrays.sort() O(nlogn) O(logn)~O(n) 快速排序的栈空间
Arrays.binarySearch() O(logn) O(1) 二分查找
Arrays.fill() O(n) O(1) 线性遍历
Arrays.copyOf() O(n) O(n) 需要新数组空间
Arrays.stream().sum() O(n) O(1) 累加操作
Arrays.stream().sorted() O(nlogn) O(n) 需要中间存储
数组遍历 O(n) O(1) 基础操作

力扣中的实用建议

// 1. 优先考虑时间复杂度
// 如果 n ≤ 10^3,O(n²) 可接受
// 如果 n ≤ 10^5,O(nlogn) 可接受  
// 如果 n ≤ 10^6,O(n) 可接受// 2. 空间复杂度优化
int[] result = new int[n];           // O(n) 空间
// vs
Arrays.sort(nums);                   // 原地排序,空间复杂度较低// 3. 避免不必要的流操作
// 不推荐(创建多个中间流)
long count = Arrays.stream(nums).filter(x -> x > 0).count();// 推荐(单次遍历)
int count = 0;
for (int num : nums) {if (num > 0) count++;
}
http://www.jsqmd.com/news/48926/

相关文章:

  • 2025年树脂拉链制造企业权威推荐榜单:尼龙拉链/金属拉链/隐形拉链源头厂家精选
  • 小企业OKR实施的组织变革与本土化路径
  • 二、使用Spring AI实现简单聊天功能(实现角色预设、流式和非流式响应)
  • 安装一个为RK3588S优化的、启用了OpenCL (GPU加速) 和 NEON (CPU加速) 的OpenCV 4.10版本。
  • 网页版RStudio跑Harmony总报错?可能是这个原因导致的
  • 递归算法如何分析复杂度?
  • 常用的文件摆渡系统:让数据安全高效跨越网络界限
  • 2025苏州最好的留学机构是哪家公司
  • 2025深圳英国留学中介有哪些机构
  • 2025南昌留学机构哪家好
  • 2025广州哪里有好的留学机构
  • 2025年托盘式不锈钢电缆桥架源头厂家权威推荐榜单:不锈钢电缆桥架/节能型桥架/聚氨酯管箱源头厂家精选
  • 2025北京正规出国留学机构排名
  • FTP传输工具推荐:2025年政企首选的国产文件传输解决方案
  • labview密码破解
  • 2025 拍立得电池怎么选?按场景选对不浪费!四大核心场景 + 十大品牌推荐,适配多机型
  • 2025年江苏saas小程序制作平台权威推荐榜单:江苏电商小程序定制服务/江苏小程序制作公司/江苏电商小程序服务商平台精选
  • 数据库索引重组与重建 - ufo233
  • 【前端小站】CSS 样式美学:从基础语法到界面精筑的实战宝典 - 实践
  • 2025年GEO公司综合实力排行榜:上饶大牛数据服务有限公司领跑行业
  • 本年口碑好的GEO品牌推荐
  • P1024 一三元次方程
  • springboot学习之注解(2)
  • 2025配置管理平台选型:如何破解CMDB建设痛点,从需求匹配到产品选型的实战指南
  • 2025欧洲留学机构十强有哪些
  • 2025南昌哪个留学中介信誉好
  • 2025广州哪家留学机构比较好一点
  • 2025大连靠谱留学机构
  • 社区新体验!一款基于 Golang + Vue 的开源社区系统!
  • 2025北京有多少家留学机构啊