class Solution {
public:
void rotate(vector<int>& nums, int k) {

int n=nums.size();
int i=0;

//-------------------

//解法一  会超时

//--------------------
k=k%n;
while(i<k){
  int temp=nums[n-1];
  for(int j=n-1;j>0;j--){
    nums[j]=nums[j-1];
  }
  nums[0]=temp;
  i++;
  }

//-------------------

//解法二

//--------------------

vector<int> num1;  新开一个数组,存放
k=k%n;
if(k==0) return;
while(i<k)
{
  num1.push_back(nums[n-k+i]);
  i++;
}
i=0;
while(i<n-k)
{
  num1.push_back(nums[i]);
  i++;
}
i=0;
while(i<n)
{
  nums[i]=num1[i];
  i++;
}

}

//-------------------

//解法三   三次旋转;很巧妙

//--------------------

};

rotate array 旋转数组的更多相关文章

  1. [LeetCode] Rotate Array 旋转数组

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

  2. [LeetCode] 189. Rotate Array 旋转数组

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

  3. Rotate Array 旋转数组 JS 版本解法

    Given an array, rotate the array to the right by k steps, where k is non-negative. 给定一个数组,并且给定一个非负数的 ...

  4. 189 Rotate Array 旋转数组

    将包含 n 个元素的数组向右旋转 k 步.例如,如果  n = 7 ,  k = 3,给定数组  [1,2,3,4,5,6,7]  ,向右旋转后的结果为 [5,6,7,1,2,3,4].注意:尽可能找 ...

  5. LeetCode Rotate Array 翻转数组

    题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. class ...

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

  7. rotate image(旋转数组)

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

  8. 33. Search in Rotated Sorted Array旋转数组二分法查询

    一句话思路:反正只是寻找一个最小区间,断开也能二分.根据m第一次的落点,来分情况讨论. 一刷报错: 结构上有根本性错误:应该是while里面包括if,不然会把代码重复写两遍,不好. //situati ...

  9. 153 Find Minimum in Rotated Sorted Array 旋转数组的最小值

    假设一个按照升序排列的有序数组从某未知的位置旋转.(比如 0 1 2 4 5 6 7 可能变成 4 5 6 7 0 1 2).找到其中最小的元素.你可以假设数组中不存在重复的元素.详见:https:/ ...

随机推荐

  1. 设计模式~简单工厂模式(Factory)

    简单工厂模式Simple Factory根据提供给它的数据,返回一个类的实例.通常它返回的类都有一个公共的父类(或者接口对象). 简单工厂的作用是实例化对象,而不需要客户了解这个对象属于哪个具体的子类 ...

  2. Oracle Hang Manager

    名词术语1.Cross Boundary Hang 交叉边界hang.在12.1.0.1中,hang manager可以检测database和asm之间的hang.2.Deadlock or Clos ...

  3. 使用dbms_system追踪其它session

    dbms_system是内部包,建议在官方指导下使用该包. SQL> desc dbms_system PROCEDURE ADD_PARAMETER_VALUE Argument Name T ...

  4. Baseline管理

    1.创建基线 SQL> exec dbms_workload_repository.create_baseline(start_snap_id=>,end_snap_id=>,bas ...

  5. PAT 解题报告 1047. Student List for Course (25)

    1047. Student List for Course (25) Zhejiang University has 40000 students and provides 2500 courses. ...

  6. devexpress13学习系列(一)PDFViewer(2)

    DevExpress.XtraPdfViewer Namespace 该命名空间下,保留着pdfviewer组件需要的类,主要有:   Class Description   PdfCurrentPa ...

  7. PostgreSQL Replication之第十二章 与Postgres-XC一起工作(2)

    12.2安装 Postgres-XC 可以从 http://postgres-xc.sourceforge.net/下载Postgres-XC.对于本书,我们使用1.0.3版本的Postgres-XC ...

  8. G面经prepare: Maximum Subsequence in Another String's Order

    求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...

  9. log4net面面观之工作原理

    转自:逗逼的博客:http://itrust.cnblogs.com/archive/2005/01/25/97225.html 要知道Log4net究竟是咋干活的,咱们可以从下面这个脉络简图入手.你 ...

  10. SQL 2012 连接失败