189. Rotate Array

Total Accepted: 55073 Total
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

&lt;LeetCode OJ&gt; 189. Rotate Array的更多相关文章

  1. 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  ...

  2. 【LeetCode】189. Rotate Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...

  3. 【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  ...

  4. 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  ...

  5. 189. Rotate Array - LeetCode

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

  6. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

  7. [LeetCode] 189. Rotate Array 旋转数组

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

  8. 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  ...

  9. 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 ...

随机推荐

  1. 0/1 knapsack problem

    Problem statement Given n items with size Ai and value Vi, and a backpack with size m. What's the ma ...

  2. Python之数据结构:集合

    一.set集合 1.集合是一个无序不重复元素集,有去重的作用 set集合类需要的参数必须是迭代器类型的,如:序列.字典等,然后转换成无序不重复的元素集.由于集合是不重复的,所以可以对字符串.列表.元组 ...

  3. jquery 实践操作:div 动态嵌套页面

    此篇记录如何在指定 div 中嵌套一个页面 load() 方法: 1. 使用 $.load() 直接导入一个页面 $('#addPage_div').load("temp/handle.ht ...

  4. webpack-dev-server 支持其他设备访问配置

    webpack-dev-server 打开的服务默认是只能localhost访问的,当有时候需要让别人访问的时候可以配置 --host 192.168.84 这样在同一个局域网下面就可以通过该ip来访 ...

  5. BZOJ 3897: Power

    3897: Power Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 218  Solved: 83[Submit][Status][Discuss] ...

  6. 教你怎么使用Windows7系统自带的备份与还原的方法

    原文发布时间为:2010-09-09 -- 来源于本人的百度文章 [由搬家工具导入] 继续单击“下一步”按钮,在其后界面中检查上述备份设置是否正确,如果不正确的话可以直接单击“取消”按钮,重新设置备份 ...

  7. Servlet乱码解决

    后端收前端 1.post乱码 可以通过 request.setCharacterEncoding("utf-8");  这样在后端可以接收中文 2.get乱码(手动解决) 可以通过 ...

  8. Linux/Unix编程中的线程安全问题【转】

    转自:http://blog.csdn.net/zhengzhoudaxue2/article/details/6432984 在目前的计算机科学中,线程是操作系统调度的最小单元,进程是资源分配的最小 ...

  9. Codeforces 898 A. Rounding

      A. Rounding   time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  10. CentOS 6.9编译安装Python-2.7.14(python升级)

    参考 Python官网:https://www.python.org/ 阿里云 https://www.aliyun.com/jiaocheng/517192.html 一.查看CentOS版本和系统 ...