题目

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.

[show hint]

Hint:

Could you do it in-place with O(1) extra space?

Related problem: Reverse Words in a String II

Credits:

Special thanks to @Freezen for adding this problem and creating all test cases.

分析

数组旋转问题。

给定一个序列和一个整数,要求将序列元素右旋k位。题目不难,但是需要注意的一点是k值可能大于序列长度,所以,旋转之前先求k对序列长度size的余数即可。

该题目一个简单的解决方法是三次反转:

  1. 将序列中(0 , size-k-1)元素反转;
  2. 将序列中(size-k , size-1)元素反转;
  3. 将全序列(0,size-1)反转;

    即可完成要求!

AC代码

class Solution {
public:
void rotate(vector<int>& nums, int k) {
if (nums.empty())
return; int size = nums.size(); //确定最终右旋元素个数 k %= size; vector<int>::iterator beg = nums.begin();
//旋转0~(size-k) (size-k , size);
reverse(beg, beg + size - k);
reverse(beg + size - k, nums.end());
//最后一次全部反转,即可完成
reverse(beg, nums.end());
}
};

GitHub测试程序源码

LeetCode(189) Rotate Array的更多相关文章

  1. LeetCode(61) Rotate List

    题目 Given a list, rotate the list to the right by k places, where k is non-negative. For example: Giv ...

  2. (LeetCode 189)Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...

  3. LeetCode(48)Rotate Image

    题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...

  4. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  5. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  6. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  7. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  8. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  9. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

随机推荐

  1. spoolight on oracle 配置

    spoolight seting 1ORACLE_HOME=D:\oracle\product\11.2.0\client_1set SQLPATH=D:\oracle\product\11.2.0\ ...

  2. (转)Mysql数据库之Binlog日志使用总结CentOS 7.x设置自定义开机启动,添加自定义系统服务

    Centos 系统服务脚本目录: /usr/lib/systemd/ 有系统(system)和用户(user)之分, 如需要开机没有登陆情况下就能运行的程序,存在系统服务(system)里,即: li ...

  3. 「干货分享」模块化编程和maven配置实践一则

    ​ 封面 说到模块化编程,对我个人而言首先起因于团队协作的需要,也就是组织架构结构特点来决定,而不是跟风求得自我认同,看看我们团队的组织结构: ​ 其中: 基础平台部职责: 1.AI实验室:语音,图像 ...

  4. var type = $('#<%=DropDownListRateType.ClientID %>').val();DropDownListRateType.ClientID是什么意思

    <%=DropDownListRateType.ClientID %>这个是C#绑定服务器控件在客户端ID, 比如你的DropDownListRateType你定义一个id,如果你用了模板 ...

  5. c# 视频播放

    发表于: 2003-10-15 20:39:21 搞定了,嘿嘿!首先非常感谢zoujiaming在邮件中给我指了条路:用C#调用API搞定!!!使用的是mciSendString API函数主要参考了 ...

  6. Linux的优缺点,Linux与windows的区别

    Linux 一. 什么是linux? Linux是一套免费使用和自由传播的类Unix操作系统,是一个多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和网络协议. ...

  7. android中常见的设计模式有哪些?

    建造者模式 建造者模式最明显的标志就是Build类,而在Android中最常用的就是Dialog的构建,Notification的构建也是标准的建造者模式. 建造者模式很好理解,如果一个类的构造需要很 ...

  8. Web端 session cookies Application viewstate

    URL传值/QueryString1.不占用服务器内存2.保密性差,传递值的长度有限 因为  上篇文章 保密性差,长度有限   传值有限只能传string类型的值 这篇文章学的知识是 session ...

  9. 10款免费的MySQL数据库图形化管理工具

    绝大多数的关系数据库都明显不同于MS Access,它们都有两个截然不同的部分:后端作为数据仓库,前端作为用于数据组件通信的用户界面.这种设计非常巧妙,它并行处理两层编程模型,将数据 层从用户界面中分 ...

  10. Netbackup常用命令--bpdbjobs

    1.大纲 bpdbjobs – 与 NetBackup 作业数据库进行交互 bpdbjobs [-report] [-M master_servers] [-ignore_parent_jobs] [ ...