更新 0977.有序数组的平方

This commit is contained in:
jinbudaily
2023-07-17 15:21:00 +08:00
parent fdbc7442ac
commit ec101bebb2

View File

@ -21,14 +21,14 @@
* 输入nums = [-7,-3,2,3,11] * 输入nums = [-7,-3,2,3,11]
* 输出:[4,9,9,49,121] * 输出:[4,9,9,49,121]
# 算法公开课 ## 算法公开课
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[双指针法经典题目!LeetCode:977.有序数组的平方](https://www.bilibili.com/video/BV1QB4y1D7ep),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 **[《代码随想录》算法视频公开课](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)。 这个时间复杂度是 O(n + nlogn) 可以说是O(nlogn)的时间复杂度,但为了和下面双指针法算法时间复杂度有鲜明对比,我记为 O(n + nlog n)。
## 双指针法 ### 双指针法
数组其实是有序的, 只不过负数平方之后可能成为最大数了。 数组其实是有序的, 只不过负数平方之后可能成为最大数了。
@ -99,7 +99,8 @@ public:
## 其他语言版本 ## 其他语言版本
Java ### Java
```Java ```Java
class Solution { class Solution {
public int[] sortedSquares(int[] nums) { public int[] sortedSquares(int[] nums) {
@ -141,7 +142,8 @@ class Solution {
} }
``` ```
Python ### Python
```Python ```Python
版本一双指针法 版本一双指针法
class Solution: class Solution:
@ -176,7 +178,8 @@ class Solution:
return sorted(x*x for x in nums) return sorted(x*x for x in nums)
``` ```
Go ### Go
```Go ```Go
func sortedSquares(nums []int) []int { func sortedSquares(nums []int) []int {
n := len(nums) n := len(nums)
@ -196,7 +199,8 @@ func sortedSquares(nums []int) []int {
return ans return ans
} }
``` ```
Rust ### Rust:
```rust ```rust
impl Solution { impl Solution {
pub fn sorted_squares(nums: Vec<i32>) -> Vec<i32> { pub fn sorted_squares(nums: Vec<i32>) -> Vec<i32> {
@ -217,7 +221,8 @@ impl Solution {
} }
} }
``` ```
Javascript ### Javascript
```Javascript ```Javascript
/** /**
* @param {number[]} nums * @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 ```swift
func sortedSquares(_ nums: [Int]) -> [Int] { func sortedSquares(_ nums: [Int]) -> [Int] {
@ -305,7 +310,7 @@ func sortedSquares(_ nums: [Int]) -> [Int] {
} }
``` ```
Ruby: ### Ruby:
```ruby ```ruby
def sorted_squares(nums) def sorted_squares(nums)
@ -323,8 +328,8 @@ def sorted_squares(nums)
end end
``` ```
### C:
C:
```c ```c
int* sortedSquares(int* nums, int numsSize, int* returnSize){ int* sortedSquares(int* nums, int numsSize, int* returnSize){
//返回的数组大小就是原数组大小 //返回的数组大小就是原数组大小
@ -357,7 +362,8 @@ int* sortedSquares(int* nums, int numsSize, int* returnSize){
} }
``` ```
PHP: ### PHP:
```php ```php
class Solution { class Solution {
/** /**
@ -386,7 +392,7 @@ class Solution {
} }
``` ```
Kotlin: ### Kotlin:
双指针法 双指针法
```kotlin ```kotlin
@ -437,7 +443,7 @@ class Solution {
} }
``` ```
Scala: ### Scala:
双指针: 双指针:
```scala ```scala
@ -473,7 +479,8 @@ object Solution {
} }
``` ```
C# ### C#
```csharp ```csharp
public class Solution { public class Solution {
public int[] SortedSquares(int[] nums) { public int[] SortedSquares(int[] nums) {
@ -504,3 +511,4 @@ public class Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a> </a>