Skip to content

Create 977. Squares of a Sorted Array.md#26

Open
Kitaken0107 wants to merge 1 commit intomainfrom
Kitaken0107-patch-28
Open

Create 977. Squares of a Sorted Array.md#26
Kitaken0107 wants to merge 1 commit intomainfrom
Kitaken0107-patch-28

Conversation

@Kitaken0107
Copy link
Owner

@liquo-rice
Copy link

sort()を使うのは違う気がします。

Given an integer array nums sorted in non-decreasing order

"sorted in non-decreasing order"という性質を使っていないです。

class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
for i in range(len(nums)):
nums[i] = nums[i]*num[i]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/num/nums/

@@ -0,0 +1,28 @@
977. Squares of a Sorted Array
https://leetcode.com/problems/squares-of-a-sorted-array/
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach?
とありますね。

どうしましょうか。二分探索で正負が変わるところを探して、そこから前と後ろに移動しながらマージソートみたいなものを書くのでどうでしょうか。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants