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 ...
随机推荐
- Random Forest 与 GBDT 的异同
曾经在看用RF和GBDT的时候,以为是非常相似的两个算法,都是属于集成算法,可是细致研究之后,发现他们根本全然不同. 以下总结基本的一些不同点 Random Forest: bagging (你懂得. ...
- 战五渣系列之八(绝杀AOP)
开发不用aop.程序猿的人生该会浪费多少时间.我想是时候让程序猿打败alpha狗了.程序猿解救世界. 1.概念 面向切面编程.这意味着,一切不在流水线上的东西.包含权限.日志.缓存.校验.资源.事物. ...
- Python中的列表,元组,字符串之间的相互转化
Python中的列表元组和字符串之间的相互转化需要利用,tuple(),list(),str(). 示例如下: >>> the_string = "hello I'am x ...
- 【java】itoo项目实战之hibernate 懒载入优化性能
在做itoo 3.0 的时候,考评系统想要上线,就開始导入数据了,仅仅导入学生2万条数据,可是导入的速度特别的慢.这个慢的原因是由于导入的时候进行了过多的IO操作.可是导入成功之后,查询学生的速度更加 ...
- 扩展HtmlHelper
eg3:扩展HtmlHelper 扩展方法类 1 public static class HtmlExtension 2 { 3 /// ...
- sshclientCRT连接linux使用技巧
设置仿真和回滚缓冲区 字体外观设置 日志文件设置 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fi ...
- SSH三大框架整合配置详细步骤(2)
4 配置Hibernate Hibernate MySql连接配置 在Hibernate中,可以配置很多种数据库,例如MySql.Sql Server和Oracle,Hibernate MySql连接 ...
- JSON with Java
work with json-lib.jar import net.sf.json.JSONArray;import net.sf.json.JSONException;import net.sf.j ...
- JDBC连接数据库查询信息的步骤(提取成配置文件方式)
硬编码格式的弊端:数据库发生改变时,要重新修改代码,重新编译和部署 解决方法:将数据库信息写在配置文件当中,让程序通过读取配置文件来获得这些信息 jdbc.driver.class=com.mysql ...
- RMQ(Range Minimum Query)问题(转)
问题描述 RMQ问题是求给定区间中的最值问题.对于长度为n的数列A,回答若干查询RMQ(A, i, j).返回数组A中下标在[i,j]里的最小值的下标. 比如数列 5,8,1,3,6,4,9,5,7 ...