<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 ...
随机推荐
- MySQL的InnoDB的细粒度行锁,是它最吸引人的特性之一。
MySQL的InnoDB的细粒度行锁,是它最吸引人的特性之一. 但是,如<InnoDB,5项最佳实践>所述,如果查询没有命中索引,也将退化为表锁. InnoDB的细粒度锁,是实现在索引记录 ...
- POJ 1941 The Sierpinski Fractal ——模拟
只需要开一个数组,记录一下这个图形. 通过一番计算,发现最大的面积大约是2k*2k的 然后递归下去染三角形. 需要计算出左上角的坐标. 然后输出的时候需要记录一下每一行最远延伸的地方,防止行末空格过多 ...
- wordpress install 主题
手动安装,你可到访WordPress官方网站的主题部分,找到你喜欢的主题后,可压缩下载到电脑并将其解压缩: 上传.zip 包到服务器,解压到 wordpress/wp-content/themes c ...
- redux使用需要注意的地方
1. react和redux没有直接联系,当react需要结合redux使用的时候,需要引入 react-redux ,该插件提供了connet等方法使得react可以注入redux属性. 2. re ...
- hdoj 1175 连连看
连连看 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- linux之函数
17.1 基本的脚本函数 函数:是一个脚本代码块,可以为其命名并在代码中任何位置重用. 17.1.1 创建函数 有两种格式:name 是函数名 1) function name { ...
- 网络上面好用的webserice方法【公开】
原文发布时间为:2009-07-27 -- 来源于本人的百度文章 [由搬家工具导入] 本帖转自 http://www.webxml.com.cn/zh_cn/web_services.aspx?off ...
- Python 类的重写
#!/usr/bin/env python # -*- coding:utf-8 -*- # @Time : 2017/11/7 22:46 # @Author : lijunjiang # @Fil ...
- vSphere虚拟化ESXI6.0+vclient安装部署
知识部分:一.什么是vSphere?vSphere是VNware公司在2001年基于云计算推出的一套企业级虚拟化解决方案.核心组件为ESXi.如今,经历了5个版本的改进,已经实现了虚拟化基础架构. ...
- Implement Trie (Prefix Tree) - LeetCode
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...