189 Rotate Array 旋转数组
将包含 n 个元素的数组向右旋转 k 步。
例如,如果 n = 7 , k = 3,给定数组 [1,2,3,4,5,6,7] ,向右旋转后的结果为 [5,6,7,1,2,3,4]。
注意:
尽可能找到更多的解决方案,这里最少有三种不同的方法解决这个问题。
详见:https://leetcode.com/problems/rotate-array/description/
Java实现:
方法一:
class Solution {
public void rotate(int[] nums, int k) {
int n=nums.length;
k%=n;
reverseArray(nums,0,n-1);
reverseArray(nums,0,k-1);
reverseArray(nums,k,n-1);
}
private void reverseArray(int[] nums,int start,int end){
while(start<end){
nums[start]=nums[start]+nums[end];
nums[end]=nums[start]-nums[end];
nums[start]=nums[start]-nums[end];
++start;
--end;
}
}
}
方法二:
class Solution {
public void rotate(int[] nums, int k) {
int n=nums.length;
int[] copy=Arrays.copyOf(nums,n);
for(int i=0;i<n;++i){
nums[(i+k)%n]=copy[i];
}
}
}
方法三:
class Solution {
public void rotate(int[] nums, int k) {
int n = nums.length;
int start = 0;
while (n != 0 && (k %= n) != 0) {
for (int i = 0; i < k; ++i) {
swap(nums, i + start, n - k + i + start);
}
n -= k;
start += k;
}
} private void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
参考:https://www.cnblogs.com/grandyang/p/4298711.html
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] Rotate Array 旋转数组
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- Rotate Array 旋转数组 JS 版本解法
Given an array, rotate the array to the right by k steps, where k is non-negative. 给定一个数组,并且给定一个非负数的 ...
- rotate array 旋转数组
class Solution {public: void rotate(vector<int>& nums, int k) { int n=nums.size(); int i=0 ...
- 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> 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 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 ...
随机推荐
- ZOJ 2859 二维线段树
思路:自己写的第二发二维线段树1A.哈哈,看来对二维的push操作比較了解了:可是还没遇到在两个线段树中同一时候进行push操作的,事实上这题我是想在x维和y维同一时候进行push操作的.可是想了好久 ...
- adb端口被占用情况下如何杀掉进程
1.CMD命令窗口输入:adb nodaemon server .然后就会提示你哪个端口被占用了. 2.输入netstat -ano | findstr "5037" .然后会弹出 ...
- VMnet1和VMnet8 未识别的网络的解决方法
我的系统是win7 64位,它居然不能识别VMnet1和VMnet8,在网上找了些资料,发现所有资料都是一样的.不过事实证明是正确的. 解决办法: 1,在运行中输入regedit 2,进入注册表[HK ...
- C从控制台(stdin)输入带空格的字符串到字符数组中
用scanf("%s",array); 的话遇到空格就停止接收后面的字符了,那怎么才能接收带空格的字符串呢? 1.用 gets() ,它可以接收带空格的字符串, 直到回车才结束输入 ...
- hexSHA1散列加密解密(不可逆)
1.maven引入codec和commons依赖: <dependency> <groupId>commons-codec</groupId> <artifa ...
- FOUNDATION OF ASYNCHRONOUS PROGRAMMING
The async and await keywords are just a compiler feature. The compiler creates code by using the Tas ...
- POJ1458 Common Subsequence —— DP 最长公共子序列(LCS)
题目链接:http://poj.org/problem?id=1458 Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Tot ...
- IIS application pool access desktop denied
https://stackoverflow.com/questions/5437723/iis-apppoolidentity-and-file-system-write-access-permiss ...
- silverlight DataGrid 显示篇
silverlight DataGrid 显示篇 分类: Silverlight2012-05-12 21:55 693人阅读 评论(0) 收藏 举报 datagridsilverlightbindi ...
- DWR(Direct Web Remoting)是什么
DWR可以用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助你开发包含AJAX技术的网站.它可以允许在浏览器里的代码使用运行在WEB服务器上的JAVA函数,就像它就在浏览器里一 ...