<LeetCode OJ> 189. Rotate Array
189. Rotate Array
Submissions: 278176 Difficulty: Easy
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.
第一种方法:
申请额外vector来处理,24ms
题目要求用三种方法
第一种:申请额外空间O(N),用vector直接处理
找规律:原数组中i位置的数据就是tmpnums中(i+k+len)/ len的数据
class Solution {
public:
void rotate(vector<int>& nums, int k) {
k=k%nums.size(); //k可能大于size
vector<int> tmpnums(nums.size());
for (int i=0;i<nums.size();i++)
tmpnums[(i+k+nums.size())%nums.size()]=nums[i];
nums=tmpnums;
}
};
另外一种方法:
技巧法(逆序),没有申请额外空间,24ms
另外一种:题目意思说能够原地处理
先前面nums.size()-k个数据逆序,接着整个数组总体逆序。最后将前k个数逆序
举例:4,3,2,1,5,6,7-------》7,6,5,1,2,3,4--------》5,6,7,1,2,3,4
class Solution {
public:
void rotate(vector<int>& nums, int k) {
k=k%nums.size();
for (int i=0;i<(nums.size()-k)/2;i++)
{
int tmp1=nums[i];
nums[i]=nums[nums.size()-k-1-i];
nums[nums.size()-k-1-i]=tmp1;
}
for (int i=0;i<nums.size()/2;i++)
{
int tmp1=nums[i];
nums[i]=nums[nums.size()-1-i];
nums[nums.size()-1-i]=tmp1;
}
for (int i=0;i<k/2;i++)
{
int tmp1=nums[i];
nums[i]=nums[k-1-i];
nums[k-1-i]=tmp1;
}
}
};
或者调用库函数来做(与上面的代码全然等价),24ms:
class Solution {
public:
void rotate(vector<int>& nums, int k) {
k=k%nums.size();
vector<int> ::iterator ite=nums.begin();
reverse(ite,ite+nums.size()-k);
reverse(ite,ite+nums.size());
reverse(ite,ite+k);
}
};
第三种方法:
循环左移或者右移(O(N*K)超时)
class Solution {
public:
void MoveRightByOne(vector<int>& nums) {
int temp = nums[ nums.size() - 1];
for (int i = nums.size() - 1; i >=1 ; --i) {
nums[i] = nums[i - 1];
}
nums[0] = temp;
} void MoveLeftByOne(vector<int>& nums) {
int temp = nums[0];
for (int i = 0; i < nums.size()-1 ; ++i) {
nums[i] = nums[i + 1];
}
nums[nums.size() - 1] = temp;
} void rotate(vector<int>& nums ,int k) {
k = k % nums.size();
if (k < nums.size()/2) {
for (int i = 0; i < k; ++i)
MoveRightByOne(nums);
} else {
for (int i = 0; i < nums.size()-k; ++i)
MoveLeftByOne(nums);
}
}
};
注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载。请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50449688
原作者博客:http://blog.csdn.net/ebowtang
<LeetCode OJ> 189. Rotate Array的更多相关文章
- LeetCode OJ 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 ...
- 【LeetCode】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- 【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 array ...
- 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 ...
- 189. Rotate Array - LeetCode
Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- 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 array ...
- Java for 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 array ...
随机推荐
- 服务器和客户端Socket单向通信例子
看了两天socket通信,也参考了网上其他人是怎么写的. 下面我把最简单的socket例程贴出来 server代码: public class Server { private static int ...
- 序列(seq)
序列(seq) 题目描述 给定 N,A,BN,A,B,构造一个长度为 NN 的排列,使得: 排列长度为 N: 最长上升子序列长度为 A: 最长下降子序列长度为 B. 我们有 SPJ,有解任意给出一组, ...
- Java EE 学习:使用 idea2017 搭建 SSM 框架
需要准备的环境: idea 2017.1 jdk1.8 Maven 3.3.9 请提前将idea与Maven.jdk配置好,本次项目用的都是比较新的 步骤: 一.首先使用idea新建一个Maven w ...
- iOS中block 静态全局局部变量block变量,以及对象,详解!
//最近总是犯迷糊,关于block对外部变量的引用,今天有时间就写了一下,加深自己的理解,巩固基础知识 1 #import <Foundation/Foundation.h> ; int ...
- sql语句like的用法 有些正则表达式可以通过like实现
原文发布时间为:2010-10-28 -- 来源于本人的百度文章 [由搬家工具导入] 在SQL结构化查询语言中,LIKE语句有着至关重要的作用。LIKE语句的语法格式是:select * from 表 ...
- TextBox取不到值及其TextBox取不到js赋的值
原文发布时间为:2009-10-22 -- 来源于本人的百度文章 [由搬家工具导入] 原因:使用了一个只读的TextBox控件 曾经遇到过这样的问题:使用了一个只读的TextBox控件,但是在后台代码 ...
- 用来武装Firebug的十四款Firefox插件
原文发布时间为:2010-04-24 -- 来源于本人的百度文章 [由搬家工具导入] 如果你是一名Web设计师,Firebug想必不会陌生,作为一款Firefox插件,它可能是网站前端开发最有用的工具 ...
- javascript获取querystring值【个人觉得这种方法最好最棒最像.NET】
原文发布时间为:2009-05-22 -- 来源于本人的百度文章 [由搬家工具导入] JavaScript获取QueryString值, 当没有QueryString值时输出bool型 null 用j ...
- bzoj 2115 [Wc2011] Xor 路径最大异或和 线性基
题目链接 题意 给定一个 \(n(n\le 50000)\) 个点 \(m(m\le 100000)\) 条边的无向图,每条边上有一个权值.请你求一条从 \(1\)到\(n\)的路径,使得路径上的边的 ...
- linux多线程学习笔记五--线程安全【转】
转自:http://blog.csdn.net/kkxgx/article/details/7506085 版权声明:本文为博主原创文章,未经博主允许不得转载. 一,线程安全基础 一个函数被称为线程安 ...