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].

题目:

给一数组,n个元素,将数组向右移动循环移动k个元素。

思路:

注意题目的要求是循环移动,所以k可以是任意非负整数,但数组元素个数为n,因此k=k%n。

一种通用的思路就是:

1、翻转整个数组 :A[0…n-1];如[7,6,5,4,3,2,1]

2、翻转数组前k个:A[0,k-1];如[5,6,7,4,3,2,1]

3、翻转数组后n-k个:A[k,n-1];如[5,6,7,1,2,3,4]

类似的应用:翻转句子中的全部单词,单词内容不变。

代码:

class Solution {
public:
void reverse(int nums[],int i,int j){
while(i<j){
int tmp=nums[i];
nums[i]=nums[j];
nums[j]=tmp;
i++;
j--;
}
} void rotate(int nums[], int n, int k) {
k=k%n;
if(k>= && n> && k<=n){
reverse(nums,,n-);
reverse(nums,,k-);
reverse(nums,k,n-);
}
}
};

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

  1. 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 arr ...

  2. LeetCode算法题-Rotate Array(Java实现)

    这是悦乐书的第184次更新,第186篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第43题(顺位题号是189).给定一个数组,将数组向右旋转k步,其中k为非负数.例如: ...

  3. (LeetCode 78)SubSets

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  4. (LeetCode 72)Edit Distance

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  5. LeetCode OJ:Rotate Array(倒置数组)

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

  6. (LeetCode 153)Find Minimum in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  7. 剑指offer 66. 构建乘积数组(Leetcode 238. Product of Array Except Self)

    剑指offer 66. 构建乘积数组 题目: 给定一个数组A[0, 1, ..., n-1],请构建一个数组B[0, 1, ..., n-1],其中B中的元素B[i] = A[0] * A[1] * ...

  8. 算法学习笔记(LeetCode OJ)

    ================================== LeetCode的一些算法题,都是自己做的,欢迎提出改进~~ LeetCode:http://oj.leetcode.com == ...

  9. (LeetCode 41)First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...

随机推荐

  1. 解决请求参数的中文乱码问题(get、post)

    2018-11-28 在web请求与响应中,会遇到乱码问题,比如填写表单数据时,难免会输入中文,姓名.公司名称等.由于HTML设置了浏览器在传递请求参数时,采用的编码方式是UTF-8,但在解码时采用的 ...

  2. bzoj 1563

    对于很多决策单调性DP问题,我们很难(但不是不可以)证明其决策满足单调性,所以感觉很像时,可以打表看是否满足. 这道题的精度(?范围)很难搞,开始生怕溢出,看了hzwer的代码,才发现用long do ...

  3. Shell 学习笔记之传递参数

    传递参数 设置权限 chmod +x file.sh 传递参数 ./file.sh parameter1 ... 特殊字符 $# 传递到脚本的参数个数 $* 以一个单字符串的形式显示所有向脚本传递的参 ...

  4. Git_创建标签

    在Git中打标签非常简单,首先,切换到需要打标签的分支上: $ git branch * dev master $ git checkout master Switched to branch 'ma ...

  5. 完整的POM文档内容

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. C# 去掉webapi返回json所带的转义字符

    反序列换报错: {"Error converting value \"{\"Result\":true,\"Code\":\"\& ...

  7. MVC打印表格,把表格内容放到部分视图打印

    假设在一个页面上有众多内容,而我们只想把该页面上的表格内容打印出来,window.print()方法会把整个页面的内容打印出来,如何做到只打印表格内容呢? 既然window.print()只会打印整页 ...

  8. [转载] MFC绘制动态曲线,用双缓冲绘图技术防闪烁

    转载的原文地址 先上效果图 随着时间的推移,曲线向右平移,同时X轴的时间坐标跟着更新. 一.如何绘制动态曲线 所谓动画,都是一帧一帧的图像连续呈现在用户面前形成的.所以如果你掌握了如何绘制静态曲线,那 ...

  9. 《软件开发与创新:ThoughtWorks文集:续集》

    <软件开发与创新:ThoughtWorks文集:续集> 基本信息 原书名:The thoughtWorks anthology, volume 2:More essays on softw ...

  10. Robots协议(爬虫协议、机器人协议)

    Robots协议(也称为爬虫协议.机器人协议等)的全称是“网络爬虫排除标准”(Robots Exclusion Protocol),网站通过Robots协议告诉搜索引擎哪些页面可以抓取,哪些页面不能抓 ...