当前位置:网站首页>Leetcode skimming ---189
Leetcode skimming ---189
2022-07-03 10:35:00 【Long time no see 0327】
subject : Give you an array , Rotate the elements in the array to the right
kA place , amongkIt is a non negative number .
Input :nums = [1,2,3,4,5,6,7], k = 3
Output :[5,6,7,1,2,3,4]
Method 1 : Use extra arrays
class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n = nums.size();
vector<int> newArr(n);
for (int i = 0; i < n; ++i) {
newArr[(i + k) % n] = nums[i];
}
nums.assign(newArr.begin(), newArr.end());
}
};Complexity analysis
Time complexity :O(n)
Spatial complexity :O(n)
Method 2 : Circular substitution
class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n = nums.size();
k = k % n;
int count = gcd(k, n);
for (int start = 0; start < count; ++start) {
int current = start;
int prev = nums[start];
do {
int next = (current + k) % n;
swap(nums[next], prev);
current = next;
} while (start != current);
}
}
};Complexity analysis
Time complexity :O(n)
Spatial complexity :O(1)
Method 3 : An array of reverse
class Solution {
public:
void reverse(vector<int>& nums, int start, int end) {
while (start < end) {
swap(nums[start], nums[end]);
start += 1;
end -= 1;
}
}
void rotate(vector<int>& nums, int k) {
k %= nums.size();
reverse(nums, 0, nums.size() - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, nums.size() - 1);
}
};Complexity analysis
Time complexity :O(n)
Spatial complexity :O(1)
边栏推荐
- 神经网络入门之模型选择(PyTorch)
- Data preprocessing - Data Mining 1
- Hands on deep learning pytorch version exercise solution - 2.4 calculus
- Out of the box high color background system
- Multilayer perceptron (pytorch)
- What did I read in order to understand the to do list
- Advantageous distinctive domain adaptation reading notes (detailed)
- Knowledge map enhancement recommendation based on joint non sampling learning
- Powshell's set location: unable to find a solution to the problem of accepting actual parameters
- LeetCode - 715. Range module (TreeSet)*****
猜你喜欢
![[C question set] of Ⅵ](/img/49/eb31cd26f7efbc4d57f17dc1321092.jpg)
[C question set] of Ⅵ

Rewrite Boston house price forecast task (using paddlepaddlepaddle)
![Step 1: teach you to trace the IP address of [phishing email]](/img/a5/c30bc51da560c4da7fc15f434dd384.png)
Step 1: teach you to trace the IP address of [phishing email]

Boston house price forecast (tensorflow2.9 practice)

Drop out (pytoch)

Ut2015 learning notes

Anaconda installation package reported an error packagesnotfounderror: the following packages are not available from current channels:

Leetcode - 706 design hash mapping (Design)*

八、MySQL之事务控制语言

神经网络入门之预备知识(PyTorch)
随机推荐
Leetcode - 705 design hash set (Design)
深度学习入门之线性回归(PyTorch)
神经网络入门之模型选择(PyTorch)
Hands on deep learning pytorch version exercise solution - 2.4 calculus
Hands on deep learning pytorch version exercise solution - 3.1 linear regression
多层感知机(PyTorch)
神经网络入门之矩阵计算(Pytorch)
Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported
SQL Server Management Studio cannot be opened
QT creator uses OpenCV Pro add
Configure opencv in QT Creator
七、MySQL之数据定义语言(二)
conda9.0+py2.7+tensorflow1.8.0
20220602 Mathematics: Excel table column serial number
Ut2013 learning notes
ECMAScript -- "ES6 syntax specification # Day1
Knowledge map reasoning -- hybrid neural network and distributed representation reasoning
Multilayer perceptron (pytorch)
侯捷——STL源码剖析 笔记
安装yolov3(Anaconda)