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]

Related problem: Reverse Words in a String II

思路一:定位具体位置,保存至另一容器,时间O(n),空间O(n)

class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n=nums.size();
if(k>n)k=k%n;
vector<int> tmp;
for(int i=n-k;i<n;++i)
{
tmp.push_back(nums[i]);
}
for(int i=0;i<n-k;++i)
{
tmp.push_back(nums[i]);
}
nums = tmp;
}
};

思路二:全部逆转,再分别逆转两部分,O(n),空间O(1)

18-Rotate Array-Leetcode的更多相关文章

  1. 189. Rotate Array - LeetCode

    Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...

  2. Rotate Array leetcode

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

  3. LeetCode: Reverse Words in a String && Rotate Array

    Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...

  4. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  5. 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

    Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...

  6. LeetCode 189. 旋转数组(Rotate Array)

    189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...

  7. 【LeetCode】Rotate Array

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

  8. LeetCode Rotate Array 翻转数组

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

  9. &lt;LeetCode OJ&gt; 189. Rotate Array

    189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...

  10. Python3解leetcode Rotate Array

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

随机推荐

  1. 使用Mybatis的TypeHandler加解密数据

    使用Mybatis的TypeHandler加解密数据 一.背景 二.解决方案 三.需求 四.实现思路 1.编写一个实体类,凡是此实体类的数据都表示需要加解密的 2.编写一个加解密的`TypeHandl ...

  2. Noip模拟72 2021.10.9

    T1 出了个大阴间题 真就以为他出了个大阴间题就没写,打个暴力就跑了 数据范围显然摆明是状压 设$f[sta][0/1]$表示在已经选择的集合$sta$中,$A$的最大值是$A$还是$A+1$ 然后按 ...

  3. 【SDOI2014】数数(补)

    见 AC自动机(补坑了) [SDOI2014] 数数 简要题意:  我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为子串.例如当S={22,333,0233}时 ...

  4. java中Map及Map.Entry详解

    Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法. keySet()方法返回值是Map中key值的集合:e ...

  5. redis 集群环境搭建

    原理: 1,每个Redis群集的节点都需要打开两个TCP连接,由于这两个连接就需要两个端口,分别是用于为客户端提供服务的常规Redis TCP命令端口(例如6379)以及通过将10000和命令端口相加 ...

  6. 虚拟化与kvm

    cpu指令级别 传统中操作系统运行于R0中称之为特权级别,直接与硬件进行交互. 应用程序运行于r3级别称之为低权限,无法与硬件直接进行交互.也就是说程序是运行于用户态,系统运行于内核态中. 虚拟化要解 ...

  7. yaml基本用法

    简介 YAML 是 "YAML Ain't Markup Language"(YAML 不是一种标记语言)的递归缩写.在开发的这种语言时,YAML 的意思其实是:"Yet ...

  8. restTemple发送请求、上传文件(@LoadBalanced微服务调用及url调用)

    import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Co ...

  9. Springboot+vue前后端分离项目,poi导出excel提供用户下载的解决方案

    因为我们做的是前后端分离项目 无法采用response.write直接将文件流写出 我们采用阿里云oss 进行保存 再返回的结果对象里面保存我们的文件地址 废话不多说,上代码 Springboot 第 ...

  10. 基于hadoop_yarn的资源隔离配置

    目录 yarn的基本概念 scheduler 集群整体的资源定义 fair scheduler简介 配置demo 队列的资源限制 基于具体资源限制 基于权重资源限制 队列运行状态限制 基于用户和分组限 ...