当前位置:网站首页>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
k
A place , amongk
It 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)
边栏推荐
- Preliminary knowledge of Neural Network Introduction (pytorch)
- 深度学习入门之自动求导(Pytorch)
- Leetcode - 705 design hash set (Design)
- A super cool background permission management system
- QT creator uses OpenCV Pro add
- Ind yff first week
- 丢弃法Dropout(Pytorch)
- LeetCode - 715. Range module (TreeSet)*****
- ECMAScript--》 ES6语法规范 ## Day1
- Leetcode刷题---35
猜你喜欢
Class-Variant Margin Normalized Softmax Loss for Deep Face Recognition
Leetcode - 5 longest palindrome substring
Simple real-time gesture recognition based on OpenCV (including code)
多层感知机(PyTorch)
Free online markdown to write a good resume
神经网络入门之预备知识(PyTorch)
Policy gradient Method of Deep Reinforcement learning (Part One)
Secure in mysql8.0 under Windows_ file_ Priv is null solution
EFFICIENT PROBABILISTIC LOGIC REASONING WITH GRAPH NEURAL NETWORKS
Configure opencv in QT Creator
随机推荐
Leetcode skimming ---44
Leetcode skimming ---283
Leetcode刷题---832
Leetcode - the k-th element in 703 data flow (design priority queue)
Leetcode skimming ---263
Advantageous distinctive domain adaptation reading notes (detailed)
Stroke prediction: Bayesian
Powshell's set location: unable to find a solution to the problem of accepting actual parameters
Boston house price forecast (tensorflow2.9 practice)
Training effects of different data sets (yolov5)
侯捷——STL源码剖析 笔记
20220610 other: Task Scheduler
The imitation of jd.com e-commerce project is coming
Weight decay (pytorch)
20220607 others: sum of two integers
Hands on deep learning pytorch version exercise solution-3.3 simple implementation of linear regression
Simple real-time gesture recognition based on OpenCV (including code)
Leetcode - 1172 plate stack (Design - list + small top pile + stack))
Leetcode-106: construct a binary tree according to the sequence of middle and later traversal
2018 y7000 upgrade hard disk + migrate and upgrade black apple