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 ...
随机推荐
- 以Java属性文件的格式创建Hibernate的配置文件和DTD特殊符号作用
演示样例代码 hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.my ...
- 简单了解eMMC
以下只是个人看法,有不妥之处,请批评指出. 参考资料:http://www.veryarm.com/1200.html 一.eMMC的发展 ROM→NorFlash→NandFlash→eMMC→UF ...
- Android开发之自己定义Spinner样式的效果实现(源码实现)
android系统自带的Spinner样式是远远满足不了我们实际开发过程中对Spinner UI风格的要求,因此我们肯定须要为了切合整个应用的风格,改动我们的Spinner样式.系统给我们提供了两种常 ...
- 3 微信开发本地代理环境的搭建--实现将内网ip映射到外网
微信公众号的开发,要搭建网站,并且随时都有可能修改网站内容进行调试,这就需要临时外网能返回本地开发环境搭建的项目进行测试,即内网映射到公网,但是好多开发者没有自己的域名和服务器,这里我们先来搭建一个本 ...
- Android ListView分页,动态添加数据
1.ListView分页的实现,重点在于实现OnScrollListener接口,判断滑动到最后一项时,是否还有数据可以加载, 我们可以利用listView.addFootView(View v)方法 ...
- iOS开发——高级篇——多线程dispatch_apply
我们知道遍历数组是一个相对耗时的操作,而同时手机的核是越来越多,所以我们需要充分利用iOS多核的作用. 特别是在遍历操作中还有其他耗时操作.像我们平时直接遍历数组的操作 ,i< ,i++){ / ...
- mysql 5.5安装不对容易出现问题
按照正常步骤安装完了mysql 5.5之后,再运行一下bin目录中的mysqlinstanceconfig.exe,重置一下密码!!!! 重置密码的地方:modify security setting ...
- create python project steps
Setting Up Your First Project You don't have to manually create the structure above, many tools will ...
- codeforces 682D D. Alyona and Strings(dp)
题目链接: D. Alyona and Strings time limit per test 2 seconds memory limit per test 256 megabytes input ...
- iOS7 push/pop转场动画
前言 iOS 7之后,苹果提供了自定义转场动画的API,我们可以自己去定义任意动画效果.本篇为笔者学习push.pop自定义转场效果的笔记,如何有任何不正确或者有指导意见的,请在评论中留下您的宝贵意见 ...