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. 【贪心】【字典树】Gym - 101466A - Gaby And Addition

    题意:定义一种无进位加法运算,给你n个正整数,问你取出两个数,使得他们加起来和最大/最小是多少. 无进位加法运算,其实是一种位运算,跟最大xor那个套路类似,很容易写出对于每个数字,其对应的最优数字是 ...

  2. wikioi 1017 乘积最大

    dp[i][j]=max(dp[i][j],dp[t][k-1]*mapn[t+1][i]); dp[i][j]代表从0-i之间有j个乘号,mapn[i][j]表示第i位到第j位的数究竟是多少 #in ...

  3. 工作记录(1)- js问题

    也是好久不写博客了,确实懒了:想想应该把node.js的东西写完整比较好,在抽时间吧: 这几天在做阿里巴巴的一个页面展示,里面设计到了一些js的问题,中途也遇到了一些幼稚的问题, 算是简单记录一下,以 ...

  4. InvalidateRect(转)

    ///===================该段是自己总结的一个小结================================= InvalidateRect()函数的作用是设置一个无效区域,并 ...

  5. Easy WordPress Updates: Store FTP Info in wp-config.php

    Saw an interesting blog post on Twitter today about storing WordPress FTP information in wp-config.p ...

  6. adb root : adbd cannot run as root in production builds

    在有些android手机上使用adb root希望获取root权限时出现如下提示信息:adbd cannot run as root in production builds.此时提升root权限的方 ...

  7. intellij idea 部署项目的时候 图中application context 写不写有什么关系?有什么作用?

    这个就是你部署之后访问的路径,比如你写一个/test,那反问就是127.0.0.1:8080/test,没有写的话就是127.0.0.1:8080

  8. PostgreSQL SystemTap on Linux 转

    PostgreSQL 支持动态跟踪, 可以通过dtrace或者systemtap工具统计相关探针的信息. 安装systemtap yum install systemtap kernel-debugi ...

  9. java基础学习总结——GUI编程(一)

    一.AWT介绍

  10. 基于springboot的spring JSR validation 后台参数验证

    springboot集成JSR参数校验: 1. 导入maven <dependency> <groupId>org.springframework.boot</group ...