From ec101bebb24b9af7910c02b979b198c238ae12b1 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Mon, 17 Jul 2023 15:21:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200977.=E6=9C=89=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E5=B9=B3=E6=96=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0977.有序数组的平方.md | 42 +++++++++++++++----------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/problems/0977.有序数组的平方.md b/problems/0977.有序数组的平方.md index de06c419..4bee585b 100644 --- a/problems/0977.有序数组的平方.md +++ b/problems/0977.有序数组的平方.md @@ -21,14 +21,14 @@ * 输入:nums = [-7,-3,2,3,11] * 输出:[4,9,9,49,121] -# 算法公开课 +## 算法公开课 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[双指针法经典题目!LeetCode:977.有序数组的平方](https://www.bilibili.com/video/BV1QB4y1D7ep),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 -## 暴力排序 +### 暴力排序 最直观的想法,莫过于:每个数平方之后,排个序,美滋滋,代码如下: @@ -47,7 +47,7 @@ public: 这个时间复杂度是 O(n + nlogn), 可以说是O(nlogn)的时间复杂度,但为了和下面双指针法算法时间复杂度有鲜明对比,我记为 O(n + nlog n)。 -## 双指针法 +### 双指针法 数组其实是有序的, 只不过负数平方之后可能成为最大数了。 @@ -99,7 +99,8 @@ public: ## 其他语言版本 -Java: +### Java: + ```Java class Solution { public int[] sortedSquares(int[] nums) { @@ -141,7 +142,8 @@ class Solution { } ``` -Python: +### Python: + ```Python (版本一)双指针法 class Solution: @@ -176,7 +178,8 @@ class Solution: return sorted(x*x for x in nums) ``` -Go: +### Go: + ```Go func sortedSquares(nums []int) []int { n := len(nums) @@ -196,7 +199,8 @@ func sortedSquares(nums []int) []int { return ans } ``` -Rust +### Rust: + ```rust impl Solution { pub fn sorted_squares(nums: Vec) -> Vec { @@ -217,7 +221,8 @@ impl Solution { } } ``` -Javascript: +### Javascript: + ```Javascript /** * @param {number[]} nums @@ -242,7 +247,7 @@ var sortedSquares = function(nums) { }; ``` -Typescript: +### Typescript: 双指针法: @@ -277,7 +282,7 @@ function sortedSquares(nums: number[]): number[] { }; ``` -Swift: +### Swift: ```swift func sortedSquares(_ nums: [Int]) -> [Int] { @@ -305,7 +310,7 @@ func sortedSquares(_ nums: [Int]) -> [Int] { } ``` -Ruby: +### Ruby: ```ruby def sorted_squares(nums) @@ -323,8 +328,8 @@ def sorted_squares(nums) end ``` +### C: -C: ```c int* sortedSquares(int* nums, int numsSize, int* returnSize){ //返回的数组大小就是原数组大小 @@ -357,7 +362,8 @@ int* sortedSquares(int* nums, int numsSize, int* returnSize){ } ``` -PHP: +### PHP: + ```php class Solution { /** @@ -386,7 +392,7 @@ class Solution { } ``` -Kotlin: +### Kotlin: 双指针法 ```kotlin @@ -437,7 +443,7 @@ class Solution { } ``` -Scala: +### Scala: 双指针: ```scala @@ -473,7 +479,8 @@ object Solution { } ``` -C#: +### C#: + ```csharp public class Solution { public int[] SortedSquares(int[] nums) { @@ -504,3 +511,4 @@ public class Solution { +