当前位置:网站首页>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)
边栏推荐
- 侯捷——STL源码剖析 笔记
- 2018 Lenovo y7000 black apple external display scheme
- Inverse code of string (Jilin University postgraduate entrance examination question)
- Leetcode刷题---202
- Leetcode刷题---278
- 安装yolov3(Anaconda)
- A complete answer sheet recognition system
- 20220610 other: Task Scheduler
- [LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation
- Leetcode刷题---704
猜你喜欢

侯捷——STL源码剖析 笔记

安装yolov3(Anaconda)

ECMAScript--》 ES6语法规范 ## Day1

Adaptive Propagation Graph Convolutional Network

深度学习入门之线性代数(PyTorch)

Out of the box high color background system

Install yolov3 (Anaconda)

Powshell's set location: unable to find a solution to the problem of accepting actual parameters

Yolov5 creates and trains its own data set to realize mask wearing detection

深度学习入门之自动求导(Pytorch)
随机推荐
ThreadLocal原理及使用场景
Softmax regression (pytorch)
Ut2012 learning notes
Linear regression of introduction to deep learning (pytorch)
深度学习入门之线性代数(PyTorch)
Hands on deep learning pytorch version exercise solution-3.3 simple implementation of linear regression
High imitation Netease cloud music
Leetcode - 1172 plate stack (Design - list + small top pile + stack))
I really want to be a girl. The first step of programming is to wear women's clothes
Model evaluation and selection
20220608 other: evaluation of inverse Polish expression
一个30岁的测试员无比挣扎的故事,连躺平都是奢望
20220604 Mathematics: square root of X
Simple real-time gesture recognition based on OpenCV (including code)
Ind kwf first week
MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法
Inverse code of string (Jilin University postgraduate entrance examination question)
Raspberry pie 4B deploys lnmp+tor and builds a website on dark web
Softmax 回归(PyTorch)
Jetson TX2 brush machine