描述

Rotate an array of n elements to the right by k steps.

For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].

Note:

Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.

分析

在一个循环里跟踪k的值即可。每一次循环时删除数组最后元素,并把这个元素插入到数组最前面,直到k为0.

代码如下:

class Solution {
public:
void rotate(vector<int>& nums, int k) {
if(k <= 0)return; //border checking
while(k != 0){
int rot = nums[nums.size() - 1];//store the laset element before erasing it
nums.erase(nums.end() - 1); //erase last element
nums.insert(nums.begin(),rot);//insert last element into beginning of the vector
--k; //update k
}
}
};

leetcode解题报告(20):Rotate Array的更多相关文章

  1. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  2. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  3. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  4. LeetCode 189. 旋转数组(Rotate Array)

    189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...

  5. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

  6. &lt;LeetCode OJ&gt; 189. Rotate Array

    189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...

  7. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

  8. LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku

    1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...

  9. leetcode解题报告(13):K-diff Pairs in an Array

    描述 Given an array of integers and an integer k, you need to find the number of unique k-diff pairs i ...

随机推荐

  1. 在vue中使用ElementUI

    完整引用ElementUI: 安装:在需要使用到的vue项目目录下,使用npm下载安装: npm/cnpm i element-ui -S/--save <!-- 引入样式 --> < ...

  2. AS3动画效果常用公式

    缓动公式: sprite.x += (targetX – sprite.x) * easing;//easing为缓动系数变量 sprite.y += (targetY – sprite.y) * e ...

  3. IDEA解决 lombok 编译报错 cannot resolve method set/get 方法

    首先关于lombok的介绍 https://blog.csdn.net/ThinkWon/article/details/101392808 总之,就是通过添加注解的方式,不需要为类编写getter或 ...

  4. 【转载】如何自己DIY组装一台台式电脑

    针对很多懂计算机的人员来说,有时候都希望自己DIY组装一台台式机,来达到自己的个性化要求以及省钱.其实自己DIY组装一台电脑也很简单,将相应的CPU处理器.主板.内存条.硬盘.固态硬盘.电脑机箱.屏幕 ...

  5. bootstrap环境搭建

    Bootstrap 是stwitter公司的两名前端设计师设计的基于html css javascript的超强的前端框架. Bootstrap 是一移动设备为优先,pc机,平板,手机皆适用的框架. ...

  6. flask打包安装文件

    如果在一台新的电脑需要运行项目的时候,这时候就需要将项目项目所用到的模块都导出来 依赖文件生成pip freeze > requirements.txt 执行该命令会在项目根目录下生成一个 re ...

  7. 一个让Java事半功倍的反射库

    在Java和Android中,我们常常会使用反射来达到一些兼容的目的.Java原生提供的反射很是麻烦,使用起来很是不方便.比如我们想要调UserManager的静态方法get,使用原生的实现如下 tr ...

  8. jquery中的ajax方法(备忘)

    参考:https://www.cnblogs.com/tylerdonet/p/3520862.html w3school:http://www.w3school.com.cn/jquery/ajax ...

  9. 每日一题-——LeetCode(111)二叉树的最小深度

    题目描述: 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 思路一: 把每一层的结点加入到队列,每一层i+1,到下一层时,把上一层在队列中的结点都弹出,按从 ...

  10. JS基础篇【1】

    该文讲解适用于有一定语言开发基础的朋友们,亦可当作久别重逢之回顾! 1.JS简介 JavaScript 是互联网上最流行的脚本语言,这门语言可用于 HTML 和 web,更可广泛用于服务器.PC.笔记 ...