题目

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. 巨杉db

    巨杉数据库 and mongo db ,分布式数据库,

  2. CS round--36

    https://csacademy.com/contest/round-36/summary/ C题是一个贪心,最坏情况是,一开始肯定是每一对袜子都抽一个,然后就需要N个袜子了.后面的情况就是相同的了 ...

  3. 为什么数据库ID不能作为URL中的标识符

    最近公司在进行网站的SEO优化,将所有主要页面的URL统一更改为新的格式,其中重要的一项改变是将所有URL的标识符统一为ID,例如过去我们的一个用户的公共页面URL是这样的 https://www.e ...

  4. 【翻译转载】【官方教程】Asp.Net MVC4入门指南(1): 入门介绍

    1. Asp.Net MVC4 入门介绍 · 原文地址:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/ ...

  5. 什么是JavaScript

    来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=30&cid= JavaScript是一种松散类型的客户端脚本语言,在用户浏览器中执 ...

  6. Springboot优点总结

    谈到 Spring Boot,就让我们先来了解它的优点 . 依据官方的文档, Spring Boot 的优点如下: --创建独立的 Spring 应用程序 : --嵌入的 Tomcat . Jetty ...

  7. Java基础语法(Eclipse)

    Java基础语法 今日内容介绍 u Eclipse开发工具 u 超市库存管理系统 第1章 Eclipse开发工具 Eclipse是功能强大Java集成开发工具.它可以极大地提升我们的开发效率.可以自动 ...

  8. Dictionary(支持 XML 序列化),注意C#中原生的Dictionary类是无法进行Xml序列化的

    /// <summary> /// Dictionary(支持 XML 序列化) /// </summary> /// <typeparam name="TKe ...

  9. Python3中requests库学习01(常见请求示例)

    1.请求携带参数的方式1.带数据的post data=字典对象2.带header的post headers=字典对象3.带json的post json=json对象4.带参数的post params= ...

  10. basic ,fundamental ,extreme ,utmost和radical.区别

    basic 普通用词,指明确.具体的基础或起点.fundamental 书面用词,不如basic使用广泛,侧重指作为基础.根本的抽象的事物.radical 着重指事物的根本或其来源.下面是extrem ...