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

Java LeetCode每日一题

        977. 有序数组的平方

        给你一个按 非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。

        最简单的方法就是将数组 nums 中的数平方后直接排序。

package JavaExercise20240908;import java.util.Arrays;
import java.util.Collections;public class LeetCodeExercise {public static void main(String[] args) {int[] array = {-4,-1,0,3,10};Solution solution = new Solution();int[] result = solution.sortedSquares(array);for (int i : result) {System.out.print(i + " ");}}
}class Solution {public int[] sortedSquares(int[] nums) {for (int i = 0; i < nums.length; i++) {nums[i] = nums[i] * nums[i];}Arrays.sort(nums);return nums;}/*public void sort(int[] nums) {for(int i = 0; i < nums.length - 1; i++) {for (int j = 0; j < nums.length - 1 - i; j++) {if (nums[j] > nums[j + 1]) {int temp = nums[j + 1];nums[j + 1] = nums[j];nums[j] = temp;}}}}*/
}


http://www.mrgr.cn/news/22480.html

相关文章:

  • 社群空间站9.9付费入群系统二开源码 易支付版全套搭建教程
  • Robot Operating System——四元数
  • 《化学教学》
  • 分布式调度方案:Elastic-Job
  • 外排序之文件归并排序实现
  • 哈希表的底层实现(1)---C++版
  • 代码随想录第十一天|二叉树的遍历方式
  • AI智能一键换头像单机版
  • 计算机的错误计算(八十七)
  • windows C++-并行编程-PPL任务并行(二)
  • lower_bound与upper_bound的使用方法
  • C语言程序设计——数组(一)
  • axure判断
  • python、C++、rust速度比较
  • RK3568平台(基础篇)GKI开发方式
  • JVM内存模型
  • C语言:刷题日志(2)
  • 代码随想录Day 38|背包问题完结,题目322.零钱兑换、279.完全平方数、139,单词拆分数
  • 两种常用损失函数:nn.CrossEntropyLoss 与 nn.TripletMarginLoss
  • Ansible简单部署与使用