vault backup: 2022-07-10 09:04:10

This commit is contained in:
juan 2022-07-10 09:04:10 +08:00
parent 7f90edd5c8
commit c17968b7a1
2 changed files with 161 additions and 0 deletions

View file

@ -0,0 +1,63 @@
# Leetcode Rotate-Array
#### 2022-07-10 08:54
> ##### Algorithms:
> #algorithm #array_in_place_operation
> ##### Data structures:
> #DS #array
> ##### Difficulty:
> #coding_problem #difficulty-medium
> ##### Additional tags:
> #leetcode
> ##### Revisions:
> N/A
##### Related topics:
```expander
tag:#array_in_place_operation
```
##### Links:
- [Link to problem](https://leetcode.com/problems/rotate-array/)
___
### Problem
Given an array, rotate the array to the right by `k` steps, where `k` is non-negative.
#### Examples
**Example 1:**
**Input:** nums = [1,2,3,4,5,6,7], k = 3
**Output:** [5,6,7,1,2,3,4]
**Explanation:**
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]
**Example 2:**
**Input:** nums = [-1,-100,3,99], k = 2
**Output:** [3,99,-1,-100]
**Explanation:**
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]
#### Constraints
- `1 <= nums.length <= 105`
- `-231 <= nums[i] <= 231 - 1`
- `0 <= k <= 105`
### Thoughts
#### Method 1: Array In-place operation
#### Method 2: Copy
#### Method 3: CPP's STL
### Solution

View file

@ -0,0 +1,98 @@
# Leetcode Squares-of-a-Sorted-Array
#### 2022-07-10 08:50
> ##### Algorithms:
> #algorithm #two_pointers
> ##### Data structures:
> #DS #array
> ##### Difficulty:
> #coding_problem #difficulty-easy
> ##### Additional tags:
> #leetcode
> ##### Revisions:
> N/A
##### Related topics:
```expander
tag:#two_pointers
```
- [[Leetcode Intersection-of-Two-Arrays-II]]
- [[Leetcode Merge-Sorted-Array]]
- [[Leetcode Merge-Two-Sorted-Lists]]
- [[Two pointers approach]]
##### Links:
- [Link to problem](https://leetcode.com/problems/squares-of-a-sorted-array/)
___
### Problem
Given an integer array `nums` sorted in **non-decreasing** order, return _an array of **the squares of each number** sorted in non-decreasing order_.
Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach?
#### Examples
**Example 1:**
**Input:** nums = [-4,-1,0,3,10]
**Output:** [0,1,9,16,100]
**Explanation:** After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].
**Example 2:**
**Input:** nums = [-7,-3,2,3,11]
**Output:** [4,9,9,49,121]
#### Constraints
- `1 <= nums.length <= 104`
- `-104 <= nums[i] <= 104`
- `nums` is sorted in **non-decreasing** order.
### Thoughts
> [!summary]
> This is a #two_pointers
Using two pointers.
One from left, one from right end.
**fill the array in backwards order.**
### Solution
cpp
```cpp
class Solution {
public:
vector<int> sortedSquares(vector<int> &nums) {
// Double pointers, since array is sorted and access in order.
// O(2n)
int mid;
int size = nums.size();
int left = 0, right = size - 1;
int count = size - 1;
vector<int> answer(size);
for (int i = 0; i < size; i++) {
nums[i] = nums[i] * nums[i];
}
while (left <= right) {
if (nums[left] >= nums[right]) {
answer[count--] = nums[left];
left++;
} else if (nums[right] > nums[left]) {
answer[count--] = nums[right];
right--;
}
}
return answer;
}
};
```