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.
Related problem: Reverse Words in a String II
class Solution {
public void rotate(int[] nums, int k) {
k%=nums.length;
if(k==0) return; reverce(nums,0,nums.length-1);
reverce(nums,0,k-1);
reverce(nums,k,nums.length-1);
}
private void reverce(int[] a,int i,int j){
for(int k = 0;k <= (j-i)/2;k++){
swap(a,i+k,j-k);
}
}
private void swap(int[] a,int i ,int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
189. Rotate Array(两次反转)的更多相关文章
- 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 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 ...
- [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 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,.. ...
- 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 ...
- 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 ...
- 【easy】189. Rotate Array
题目标签:Array 题目给了我们一个数组 和 k. 让我们 旋转数组 k 次. 方法一: 这里有一个很巧妙的方法: 利用数组的length - k 把数组 分为两半: reverse 左边和右边的数 ...
- LeetCode Array Easy 189. Rotate Array
---恢复内容开始--- Description Given an array, rotate the array to the right by k steps, where k is non-ne ...
随机推荐
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 4496(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...
- Xmanager连接图形界面
1.编辑gnome配置文件vim /etc/gdm/custom.conf # GDM configuration storage [daemon]RemoteGreeter= /usr/libexe ...
- Java知识点梳理——集合
1.定义:Java集合类存放于java.util包,是存放对象的容器,长度可变,只能存放对象,可以存放不同的数据类型: 2.常用集合接口: a.Collection接口:最基本的集合接口,存储不唯一, ...
- 一、Android Studio入门——Eclipse快捷键配置
[Studio总体介绍] 第一个是运行. 第二个是Debug. 是Studio的设置界面. 工程的配置. Sync,更改配置.导入JAR包,都会去Sync一次. SDK Manager. ...
- DEDE的搜索页面支持arclist和channelartlist标签的操作方法
很多朋友在使用dedecms进行网站开发时都会存在这样的问题,那就是dedecms的搜索页(search.php)与我们网站页面的模板的头 部.底部不一样,并且还不支持在搜索页调用其他某一栏目的文档. ...
- 全链路追踪spring-cloud-sleuth-zipkin
微服务架构下 多个服务之间相互调用,在解决问题的时候,请求链路的追踪是十分有必要的,鉴于项目中采用的spring cloud架构,所以为了方便使用,便于接入等 项目中采用了spring cloud s ...
- CentOS7.2安装配置FTP服务器VSFTP
1,查看系统版本 2,yum安装vsftpd yum -y install vsftpd 3,修改配置文件 vim /etc/vsftpd/vsftpd.conf local_enable=YES w ...
- CentOS7.2编译配置LNMP环境(MySQL5.7.20,PHP7.0.24)
一, 查看系统版本及内核版本 二, 编译安装nginx 1, 新建nginx用户 useradd -s /sbin/nologin -M nginx 2, ...
- APNS/苹果推送服务
Apple Push Notification Service Google Cloud Message/Google 云 消息 Firebase Cloud Messaging