C#解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,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的更多相关文章
- [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 ...
- 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 ...
- 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,.. ...
- <LeetCode OJ> 189. Rotate Array
189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...
- 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】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
随机推荐
- 《学习OpenCV》 第四章 习题六
实现的是一个图像标签编辑器,其间遇到了些问题还未解决或者可能解决方法上不是最优,若你有更好的思路可以提供给我,大恩不言谢啦!!☆⌒(*^-゜)v. #include "stdafx.h&qu ...
- C++实现base64编码(1)
下面的代码是php里面的base64编码逻辑,确实比我之前的要美观很多,我只是简单的用C++的类进行了一下封装,删除了一些没用的逻辑,基本上还是原来PHP的代码: #include <iostr ...
- Swift中KIF测试的特点-b
我最近在忙着回归到过去测试代码的老路子,使用KIF和XCTest框架,这样会使得iOS中的测试变得简单.当我开始捣鼓KIF的时候,我用Swift写的应用出了点小问题,不过最终还是很机智的搞定了.在我写 ...
- Node.js全局对象
Node.js的全局对象是具有全局性的,它们可在所有的模块中应用.我们并不需要包括这些对象在应用中,而可以直接使用它们.这些对象的模块,函数,字符串和对象本身,如下所述. __filename __f ...
- BZOJ 1296 粉刷匠
Description windy有\(N\)条木板需要被粉刷.每条木板被分为\(M\)个格子. 每个格子要被刷成红色或蓝色. windy每次粉刷,只能选择一条木板上一段连续的格子,然后涂上一种颜色. ...
- js plugin
http://site518.net/javascript-date-handle/ http://developer.51cto.com/art/201212/374902.htm http://e ...
- exceptions.IOError: decoder jpeg not available
1.确保安装PIL所需的系统库 yum -y install zlib yum -y install zlib-devel yum -y install libjpeg yum -y install ...
- 【转】Android的onCreateOptionsMenu()创建菜单Menu详解
原文网址:http://www.linuxidc.com/Linux/2012-02/55500.htm Android一共有三种形式的菜单: 1.选项菜单(optinosMen ...
- 例6.1:学生选课系统设计(界面设计、类图、数据库ER图)
- 怎么用copy关键字
出题者简介: 孙源(sunnyxx),目前就职于百度 整理者简介:陈奕龙,目前就职于滴滴出行. 转载者:豆电雨(starain)微信:doudianyu 用途: NSString.NSArray.NS ...