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.

我的代码,运行时间488ms:

public class Solution {
public void Rotate(int[] nums, int k) {
int[] zong=new int[nums.Length+k];
int j=;
k=k%nums.Length;
int[] temp=new int[k];
if (k!=)
{
for(int i=nums.Length-k;i<nums.Length;i++)
{
temp[j]=nums[i];
j++;
}
temp.CopyTo(zong, ); //将temp数组拷贝到zong数组中,其实位置为zong[0]
nums.CopyTo(zong, temp.Length);//将nums数组拷贝到zong数组中,其实位置为zong[temp.Length]
Array.ConstrainedCopy(zong, , nums, ,nums.Length ); //将总的数组拷贝到nums数组中,其中zong的起始位置为zong[0],nums的起始位置为nums[0],拷贝的长度为nums.Length
}
}
}

非常可惜的是,虽然题目要求是用3中做法做出来,但是我只能想出来一种。

本题注意点:

1 可能出现k>nums.Length的情况,此时k%nums.Length即可排除一个轮换周期

2 刚开始用了两个嵌套for循环,可是提示time error,可见这种模式太耗时间,应该少用

在Disacuss中见到了一个非常简洁的算法:

public class Solution {
public void Rotate(int[] nums, int k) {
k=k%nums.Length;
int[] numsCopy=new int[nums.Length];
if(k!=)
{
for(int i=;i<nums.Length;i++)
{
numsCopy[i]=nums[i];
} for(int i=;i<nums.Length;i++)
{
nums[(i+k)%nums.Length]=numsCopy[i];
}
}
}
}

其中nums[(i+k)%nums.Length]=numsCopy[i];这句非常巧妙的利用取余运算!

看了上面的代码,我意识到数组的传值需要用一个for循环,不应该直接传地址。因此将我原来的代码稍作修改:将

 Array.ConstrainedCopy(zong, 0, nums,0 ,nums.Length );修改为一个for循环:
 for (int i=;i<nums.Length;i++)
{
nums[i]=zong[i];
}

这样也可以Accepted!

这里还有一个我至今没有看懂的方法,先放在这里,期待以后可以看懂:

public class Solution {
public void Rotate(int[] nums, int k) {
int sz,n,temp;
sz=n=nums.Length;
k%=n;
if(n< || k<) return;
for(int i=k;n>;++i)
{
int j=i, prev=nums[(i-k)%k];
while(n-->)
{
//Interlocked.Exchange(prev,nums[j]);
temp=nums[j];
nums[j]=prev;
prev=temp;
j=(j+k)%sz;
if(j==i) break;
}
}
}
}

很高兴 今天把它看懂了,这里就贴上源代码以及我的理解:

public class Solution {
public void Rotate(int[] nums, int k) {
int sz,n,temp;
sz=n=nums.Length;
k%=n;
if(n< || k<) return;
for(int i=k;n>;++i)
{
int j=i, prev=nums[(i-k)%k];
while(n-->)
{
//Interlocked.Exchange(prev,nums[j]);
temp=nums[j];
nums[j]=prev;
prev=temp;
j=(j+k)%sz; //如果把这个数组看成一个循环列表,这样每进行完一次j=(j+k)%sz,指针就会前移k格,将该位置的数更新
if(j==i) break; //如果某一次前进k格又到了初始位置,此时在循环的话和上次循环的效果一样,所以应该跳出循环,同时利用下一个位置的数字作为初始的值,进行第二次循环,一直如此往复,知道所有的值都更新完成。由于条件限制,一共只会自行n次,每次更新一个数字,正好全部更新完毕
}
}
}
}

C#解leetcode 189. Rotate Array的更多相关文章

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

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

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

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

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

  5. Leetcode 189 Rotate Array stl

    题意:将数组旋转k次,如将数组[1,2,3,4,5]旋转1次得到[2,3,4,5,1],将数组[1,2,3,4,5]旋转2次得到[3,4,5,1,2]..... 本质是将数组分成两部分a1,a2,.. ...

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

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

  7. 189. Rotate Array - LeetCode

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

  8. 189. Rotate Array【easy】

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

  9. 【LeetCode】Rotate Array

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

随机推荐

  1. 03:计算(a+b)/c的值

    总时间限制:  1000ms 内存限制:  65536kB 描述 给定3个整数a.b.c,计算表达式(a+b)/c的值,/是整除运算. 输入 输入仅一行,包括三个整数a.b.c, 数与数之间以一个空格 ...

  2. Js使用word书签填充内容

    Js使用word书签填充内容 1.在模板文件中需要填充的地方插入书签 填充内容为:(|光标所在处) 填写书签名,点击添加完成: 2.使用js打开模板,获取书签位置,填充数据: function pri ...

  3. Unity3d 跑酷游戏 之Character Controller篇

    unity3d  Character Controller @by  广州小龙 做3D跑酷游戏,也慢慢的学习了一些东西,从开发过程中积累了一些小的知识点跟大家分享一下! 1. 这个Revert按钮的意 ...

  4. "Redis客户端连接数一直降不下来"的有关问题解决

    [线上问题] "Redis客户端连接数一直降不下来"的问题解决 前段时间,上线了新的 Redis缓存(Cache)服务,准备替换掉 Memcached. 为什么要将 Memcach ...

  5. window7 64位安装Python

    Python下载地址:https://www.python.org/download/releases/2.7.8/ 选择64位的安装,然后双击打开下载的文件,默认一步步安装. 其中有一个步骤如下图: ...

  6. [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

    Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...

  7. bzoj3438

    很容易想到是最小割模型首先对于一个点i,从s到i连一条容量为ai的边,再从i连一条容量为bi的边到t然后就是处理附加权的问题了一开始受到之前的思维定势的影响,一直在思考怎么在作物之间连边由于每种额外收 ...

  8. 2015 CCC - 02 找不匹配

    照例传送门CNUOJ - 0385:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=355 题目分析:首先感谢”数据结构与算法“群群友 ...

  9. 图论(floyd算法):NOI2007 社交网络

    [NOI2007] 社交网络 ★★   输入文件:network1.in   输出文件:network1.out   简单对比 时间限制:1 s   内存限制:128 MB [问题描述] 在社交网络( ...

  10. 服务端调用js:javax.script

    谈起js在服务端的应用,大部分人的第一反应都是node.js.node.js作为一套服务器端的 JavaScript 运行环境,有自己的独到之处,但不是所有的地方都需要使用它. 例如在已有的服务端代码 ...