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. NOIP 模拟 $79\; \rm y$

    题解 \(by\;zj\varphi\) NOIP2013 的原题 最简单的思路就是一个 bfs,可以拿到 \(70pts\) 75pts #include<bits/stdc++.h> ...

  2. SpringCloud微服务实战——搭建企业级开发框架(九):使用Nacos发现、配置和管理微服务

    Nacos是一个更易于构建云原生应用的动态服务发现.配置管理和服务管理平台,Nacos 致力于帮助您发现.配置和管理微服务.Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现.服务配置 ...

  3. Django 开发------django-crontab实现服务端的定时任务

    一.需求: 想实现类似 Linux 中crontab 的功能.定时执行计划任务. 二.配置: a.安装插件: # pip3 install django-crontab b.在settings.py ...

  4. HTML js 页面倒计时后跳转至新页面

    HTML: 1 <body> 2 <p>操作错误!还有<span id="sp">5</span>秒跳转到交换机备份页面...< ...

  5. Dubbo框架协议总结

    Dubbo支持的各种协议及相关特性及应用场景,均可在官网文档中找到. 支持的协议有dubbo.rmi.hession.http.webservice.thrift.memcached.redis.re ...

  6. Java基础语法5-运算符

    运算符 基本运算符(算术.赋值.关系.逻辑等)不再赘述 位运算符 位运算符&.|.^.~.<<.>> &与 |或 ~非 ^异或 <<左移 >& ...

  7. 力扣 - 剑指 Offer 67. 把字符串转换成整数

    题目 剑指 Offer 67. 把字符串转换成整数 思路1 根据题意,要解决这题,首先要判断的条件有: 不包括首位空格 第一位必须为:+.-.数字三者其一,否则不合法 数字必须连续的,如果遇到非数字, ...

  8. Ubuntu 安装 mysql 报错 "update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在"

    解决方法: sudo cp /etc/mysql/my.cnf /etc/mysql/mysql.cnf 偷梁换柱-! 如果想更新mysql的源方法如下: wget http://dev.mysql. ...

  9. 4. 理解Update、Enter、Exit 与 添加、删除元素

    理解Update.Enter.Exit 与 添加.删除元素 在使用data()绑定数据时,例如:现在我们有一个数组[3,6,9,12,15],我们可以将数组每一项与一个<p>绑定,但是,现 ...

  10. Idea tomcat debug按钮灰色无法运行

    打开Project Structure 2.选中src,点击按钮关闭界面,重启idea即可