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 ...
随机推荐
- Office WORD如何简繁转换
选中要转换的文字,工具-语言,中文简繁转换.
- jsoncpp的api简要说明
1 jsoncpp的api简要说明 1,解析(json字符串转为对象) std::string strDataJson; Json::Reader JReader; Json::Value JObj ...
- 【Android数据存储】- File
个人学习整理.如有不足之处,请不吝不吝赐教. 转载请注明:@CSU-Max 读写本应用程序数据目录中的文件 此种方法读写的文件在/data/data/<应用程序包名>中 ...
- 我的第一个开源控件-DragGridView
我的第一个开源控件出炉了,希望各个小伙伴给个star,支持下.项目地址 1. 前言 因为项目须要,要做一个相似腾讯视频.频道管理.拖拽排序的效果.这个控件是在原地址 之上改造出来的.先看下效果图. 1 ...
- 1.5.4 HAVING子句
1.5.4 HAVING子句正在更新内容.请稍后
- Oracle11g表空间导入dmp数据
如果你的表数据没有附带表空间和用户名,那么只要一句话 Imp {u_name}/{u_pwd}@{local_svrname} fromuser={from_user} touser={u_name} ...
- struts2 中 result type="stream"
Stream result type是Struts2中比较有用的一个feature.特别是在动态生成图片和文档下载的情况下 1:图片验证码: Action类,action主要要提供一个获取InputS ...
- Nginx——静态资源服务器(一)
java web的项目中,我们经常将项目部署到Tomcat或者jetty上,可以通过Tomcat或者jetty启动的服务来访问静态资源.但是随着Nginx的普及,用Nginx来作为静态资源服务器,似乎 ...
- 原生ajax请求和jsonp
1.原生ajax请求 var obj = new XMLHttpRequest(); obj.open("POST", url, true); obj.setRequestHead ...
- UVA11324 The Largest Clique —— 强连通分量 + 缩点 + DP
题目链接:https://vjudge.net/problem/UVA-11324 题解: 题意:给出一张有向图,求一个结点数最大的结点集,使得任意两个结点u.v,要么u能到达v, 要么v能到达u(u ...