描述

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. 修改mysql远程数据库链接密码(转)

    原文:https://blog.csdn.net/jianjiao7869/article/details/81029171 原来root用户有两个,一个只允许localhost登陆,一个可运行所有用 ...

  2. Angular Material (Components Cdk) 学习笔记 Table

    refer : https://material.angular.io/cdk/table/overview https://material.angular.io/components/table/ ...

  3. Matlab匿名函数,向量化和预分配,函数的函数,P码文件

    匿名函数: 匿名函数是不存储在程序文件中.但与数据类型是 function_handle 的变量相关的函数.匿名函数可以接受输入并返回输出,就像标准函数一样.但是,它们可能只包含一个可执行语句. 例如 ...

  4. Vuex 刷新后数据丢失问题 Typescript

    问题描述:Vuex保存的数据在页面刷新后会全部丢失清除 问题解决方案:使用sessionstorage进行保存,在页面刷新时保存至sessionStorage,页面在加载时再进行填充   (另有vue ...

  5. IE hack大全

    IE hack大全:http://blog.csdn.net/freshlover/article/details/12132801

  6. Ubuntu安装opencv3.4.4教程

    1 去官网下载opencv 在本教程中选用的是opencv3.4.4,下载链接 http://opencv.org/releases.html ,选择sources. 2 解压 unzip openc ...

  7. PLSQL登录的时候Warning提示:Using a filter for all users can lead to poor performance!

    转自: https://blog.csdn.net/athena2015/article/details/81811908

  8. 使用browser-sync预览pandoc markdown???

    由于 pandoc markdown在sublime下面并不能很好的预览,因此可以直接 使用 pandoc build成html,然后借助下面的工具进行实时在浏览器里面预览 http://www.br ...

  9. 爬虫之 BeautifulSoup与Xpath

    知识预览 BeautifulSoup xpath BeautifulSoup 一 简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: '' ...

  10. python之第一对象,函数名的应用,闭包

    一.第一对象 在 Python 中万物皆为对象,函数也不例外,函数作为对象可以赋值给一个变量.可以作为元素添加到集合对象中.可作为参数值传递给其它函数, 还可以当做函数的返回值,这些特性就是第一类对象 ...