LeetCode & Q189-Rotate Array-Easy
Array
Description:
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].
刷题还是习惯早上刷,现在脑子不灵光了,哎...
这题我想的很复杂,可以说和算法不沾边,看了Discuss照着写了一遍。
public class Solution {
public void rotate(int[] nums, int k) {
k %= nums.length;
reverse(nums, 0, nums.length-1);
reverse(nums, 0, k-1);
reverse(nums, k, nums.length-1);
}
public void reverse(int[]nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}
LeetCode & Q189-Rotate Array-Easy的更多相关文章
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- [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 (旋转数组)
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- Java for 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: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 [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 ...
- 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(67)-Rotate Array
题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ar ...
- 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,.. ...
- Leetcode 665. Non-decreasing Array(Easy)
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
随机推荐
- Django---视图
全过程:用户填写相关数据,提交相关请求,链接到对应的视图上,在此视图上(有用户传过来的数据[就是视图要处理的数据],在视图里面对数据进行业务处理,在数据库中crub数据,然后把对应的界面和界面显示需要 ...
- 以@Value方式注入 properties 配置文件
类中读取XML文件不是太方便,所以使用*.properties是比较好的办法 注入方式获取是最直接,最快捷的.这个操作主要涉三个部分,下面分别介绍: 首先,配置文件准备.这里文件名命名为applica ...
- Java设计模式(五)Prototype原型模式
一.场景描述 创建型模式中,从工厂方法模式,抽象工厂模式,到建造者模式,再到原型模式,我的理解是,创建对象的方式逐步从编码实现转向内存对象处理. 例如,在“仪器数据采集器”的子类/对象“PDF文件数据 ...
- LNMP+FARM+DNS
LNMP 1.安装Nginx前的环境. # yum -y install gcc gcc-c++ pcre-devel zlib-devel openssl-devel 2.添加www系统用户,在 ...
- Mysql语句的执行过程
当你希望MySQL能够以更高的性能运行查询时,最好的办法是弄清楚MySQL是如何优化和执行查询.<高性能MySQL> MySQL客户端与服务器端的通信特点 客户端与服务器之间是半双工通信, ...
- 使用python和pygame绘制繁花曲线
前段时间看了一期<最强大脑>,里面展示了各种繁花曲线组合成的非常美丽的图形,一时心血来潮,想尝试自己用代码绘制繁花曲线,想怎么组合就怎么组合. 真实的繁花曲线使用一种称为繁花曲线规的小玩意 ...
- JAVA实现双向链表的增删功能
JAVA实现双向链表的增删功能,完整代码 package linked; class LinkedTable{ } public class LinkedTableTest { //构造单链表 sta ...
- Python 自学 之 String 常见操作
这是在Python 3.5.3版本下测试的.# Author Taylor_Manitoname ="my name is alex"#capitalized 大写的print(& ...
- 1.由浅入深解析 SimpleDateFormat
一.SimpleDateFormat简介 SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类. 它允许格式化 (date -> text).语法分析 (text - ...
- 云计算--网络原理与应用--20171123--网络地址转换NAT
NAT的概述 NAT的配置 实验 一. NAT的概述 NAT(Network address translation,网络地址转换)通过将内部网络的的私有地址翻译成全球唯一的共有网络IP地址,是内部网 ...