当前位置:网站首页>189. Rotation array
189. Rotation array
2022-04-22 07:28:00 【yinhua405】
Give you an array , Rotate the elements in the array to the right k A place , among k It is a non negative number .
Example 1:
Input : nums = [1,2,3,4,5,6,7], k = 3 Output :[5,6,7,1,2,3,4]explain : Rotate right 1 Step :[7,1,2,3,4,5,6]Rotate right 2 Step :[6,7,1,2,3,4,5]Rotate right 3 Step :[5,6,7,1,2,3,4]
Example 2:
Input :nums = [-1,-100,3,99], k = 2 Output :[3,99,-1,-100] explain : Rotate right 1 Step : [99,-1,-100,3] Rotate right 2 Step : [3,99,-1,-100]
Tips :
1 <= nums.length <= 105-231 <= nums[i] <= 231 - 10 <= k <= 105
class Solution {
public:
void rotate(vector<int>& nums, int k) {
int size = nums.size();
vector<int>tmp=nums;
tmp.insert(tmp.end(),nums.begin(),nums.end());
nums = vector<int>(tmp.begin()+(size-k),tmp.end()-k);
}
};
版权声明
本文为[yinhua405]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220613324866.html
边栏推荐
猜你喜欢

微软实习生面试的2道算法题目——20220119

最小圆覆盖(计算几何基础)

Leetcode - 6 - (chaîne multiplicatrice, prochain élément plus grand < Ⅰ Ⅱ Ⅲ >, K liste de chaînes inversées)

Two algorithm questions for Microsoft intern interview -- 20220119

抽象类和抽象方法

SQL review, grammar notes, fresh out

H.Happy Number (进制转换/第n个特殊数)(2021牛客暑期多校训练营9 )

Host cannot Ping virtual machine in bridging mode

synchronized锁优化的一些机制(锁升级)

LeetCode - 4 - (接雨水、无重复字符的最长子串、分发糖果、二叉树的<前中后层>序遍历)
随机推荐
Singleton pool, singleton bean, singleton mode
L2-005 set similarity (set judgment)
1242 · 无重叠区间
843 · 数字翻转
H.Happy Number (进制转换/第n个特殊数)(2021牛客暑期多校训练营9 )
SQL复习语法笔记整理,新鲜出炉
L2-004 is this a binary search tree? (first order input & judgment search Binary Tree & second order output)
15. Full arrangement
小题记录——
Introduction
L2-004 这是二叉搜索树吗?(先序输入&判断搜索二叉树&后序输出)
[number theory] congruence (7): fast power, fast power of matrix
Byte Summer Internship - 20220304
详解树状数组模板——理论和代码的实现
In the process of class loading, the allocation area of class variables is different from that of instance variables
Idea does not display the run dashboard view window
76 · 最长上升子序列
This关键字详细概述
顺序表 增删查(找)
Add author photos and author profiles to latex