From 7765c3b22cb7ffde59a454d3fd3b6409a792d247 Mon Sep 17 00:00:00 2001 From: "huangge1199@hotmail.com" Date: Thu, 8 Jul 2021 22:59:03 +0800 Subject: [PATCH] =?UTF-8?q?977:=E6=9C=89=E5=BA=8F=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E7=9A=84=E5=B9=B3=E6=96=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/SquaresOfASortedArray.java | 66 +++++++++++++++++++ .../editor/cn/SquaresOfASortedArray.md | 40 +++++++++++ 2 files changed, 106 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/SquaresOfASortedArray.java create mode 100644 src/main/java/leetcode/editor/cn/SquaresOfASortedArray.md diff --git a/src/main/java/leetcode/editor/cn/SquaresOfASortedArray.java b/src/main/java/leetcode/editor/cn/SquaresOfASortedArray.java new file mode 100644 index 0000000..870b02d --- /dev/null +++ b/src/main/java/leetcode/editor/cn/SquaresOfASortedArray.java @@ -0,0 +1,66 @@ +//给你一个按 非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。 +// +// +// +// +// +// +// 示例 1: +// +// +//输入:nums = [-4,-1,0,3,10] +//输出:[0,1,9,16,100] +//解释:平方后,数组变为 [16,1,0,9,100] +//排序后,数组变为 [0,1,9,16,100] +// +// 示例 2: +// +// +//输入:nums = [-7,-3,2,3,11] +//输出:[4,9,9,49,121] +// +// +// +// +// 提示: +// +// +// 1 <= nums.length <= 104 +// -104 <= nums[i] <= 104 +// nums 已按 非递减顺序 排序 +// +// +// +// +// 进阶: +// +// +// 请你设计时间复杂度为 O(n) 的算法解决本问题 +// +// Related Topics 数组 双指针 排序 +// 👍 247 👎 0 + +package leetcode.editor.cn; + +import java.util.Arrays; + +//977:有序数组的平方 +class SquaresOfASortedArray{ + public static void main(String[] args) { + //测试代码 + Solution solution = new SquaresOfASortedArray().new Solution(); + } + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int[] sortedSquares(int[] nums) { + for (int i = 0; i < nums.length; i++) { + nums[i]*=nums[i]; + } + Arrays.sort(nums); + return nums; + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/SquaresOfASortedArray.md b/src/main/java/leetcode/editor/cn/SquaresOfASortedArray.md new file mode 100644 index 0000000..70b0983 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/SquaresOfASortedArray.md @@ -0,0 +1,40 @@ +

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

+ + + +

 

+ +

示例 1:

+ +
+输入:nums = [-4,-1,0,3,10]
+输出:[0,1,9,16,100]
+解释:平方后,数组变为 [16,1,0,9,100]
+排序后,数组变为 [0,1,9,16,100]
+ +

示例 2:

+ +
+输入:nums = [-7,-3,2,3,11]
+输出:[4,9,9,49,121]
+
+ +

 

+ +

提示:

+ + + +

 

+ +

进阶:

+ + +
Related Topics
  • 数组
  • 双指针
  • 排序
  • \n
  • 👍 247
  • 👎 0
  • \ No newline at end of file