当前位置:网站首页>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)
边栏推荐
- Label Semantic Aware Pre-training for Few-shot Text Classification
- Handwritten digit recognition: CNN alexnet
- Knowledge map enhancement recommendation based on joint non sampling learning
- Hands on deep learning pytorch version exercise solution - 3.1 linear regression
- LeetCode - 715. Range module (TreeSet)*****
- Install yolov3 (Anaconda)
- Standard library header file
- 八、MySQL之事务控制语言
- Leetcode - the k-th element in 703 data flow (design priority queue)
- Seata分布式事务失效,不生效(事务不回滚)的常见场景
猜你喜欢

High imitation wechat

Softmax 回归(PyTorch)

mysql5.7安装和配置教程(图文超详细版)

I really want to be a girl. The first step of programming is to wear women's clothes
![[C question set] of Ⅵ](/img/49/eb31cd26f7efbc4d57f17dc1321092.jpg)
[C question set] of Ⅵ

Configure opencv in QT Creator

Adaptive Propagation Graph Convolutional Network

Hands on deep learning pytorch version exercise solution - 2.6 probability

权重衰退(PyTorch)

【SQL】一篇带你掌握SQL数据库的查询与修改相关操作
随机推荐
Simple real-time gesture recognition based on OpenCV (including code)
What useful materials have I learned from when installing QT
Raspberry pie 4B installs yolov5 to achieve real-time target detection
多层感知机(PyTorch)
R language classification
Leetcode skimming ---263
Leetcode刷题---217
Leetcode - the k-th element in 703 data flow (design priority queue)
重写波士顿房价预测任务(使用飞桨paddlepaddle)
[LZY learning notes -dive into deep learning] math preparation 2.5-2.7
Leetcode刷题---977
Drop out (pytoch)
7、 Data definition language of MySQL (2)
Ut2016 learning notes
Hands on deep learning pytorch version exercise solution - 2.6 probability
神经网络入门之预备知识(PyTorch)
Configure opencv in QT Creator
Leetcode刷题---1
Leetcode刷题---374
Jetson TX2 刷机