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. MySQL学习笔记——复制的实现原理

    1.三个线程 MYSQL复制是从主服务器复制到一个或多个从服务器的异步过程,在主服务器与从服务器之间实现整个复制过程主要由三个线程来实现,其中一个线程I\O在主服务器器端,另两个线程(SQL线程和I\ ...

  2. 一个WebService Demo

    1.建立一个Asp.net Web网站,添加新项Web服务MyMath.asmx.编写如下代码: using System; using System.Collections.Generic; usi ...

  3. [ArcEngine]IFeatureBuffer使用

    public static void LoadOnlyModeInsert(IFeatureClass featureClass, List < IGeometry > geometryL ...

  4. iOS 图片循环滚动(切片效果)

                             #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIAp ...

  5. ios推送

    1. ios 在杀掉app后,只能接受到系统通知,JPUSH自定义消息不能接受到.系统通知经过实验只能接收到50左右个汉字. 2. 实现方案: 推送的时候,JPUSH推送一个消息,App客户端获取到数 ...

  6. python 归档tarfile,zipfile学习

    一.tarfile 用法: tarfile.open(cls, name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)  返回一个Tar ...

  7. Tomcat类加载器机制

    Tomcat为什么需要定制自己的ClassLoader: 1.定制特定的规则:隔离webapp,安全考虑,reload热插拔 2.缓存类 3.事先加载 要说Tomcat的Classloader机制,我 ...

  8. (Protype Pattern)原型模式

    定义: 原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型来创建新的对象 适用性: 当我们系统中有一些类,在使用的时候都有同样需要大量的创建,而这样的创建是复杂的而且是浪费CPU,内存资源的 ...

  9. java collections读书笔记(11) Lists

    继续这个系列,好久没学习了,懒惰呀. Set接口,实际上是collection 类别中最简单的一个接口,因为它并没有比Collection 接口增加任何的内容,相对而言,大家可能更喜欢List接口和它 ...

  10. VS2012窗口及编辑文本框背景颜色变黑

    1.工具->选项 2.环境->常规->深色