leetcode189
public class Solution {
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--;
}
}
public void Rotate(int[] nums, int k)
{
k %= nums.Length;
reverse(nums, , nums.Length - );
reverse(nums, , k - );
reverse(nums, k, nums.Length - );
}
}
https://leetcode.com/problems/rotate-array/#/description
补充一个python的实现:
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
part1 = nums[n-k:]
part2 = nums[:n-k]
nums.clear()
nums.extend(part1)
nums.extend(part2)
leetcode189的更多相关文章
- 倒转数组 Leetcode189
倒转数组 Leetcode189 记录调整数组顺序而不需要另加内存的一种方法: 题目 189. 旋转数组 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [ ...
- 理解JavaScript中的参数传递 - leetcode189. Rotate Array
1.关于leetcode 这是第一篇关于leetcode的题解,就先扯点关于leetcode的话. 其实很早前就在博客园看到过leetcode一些题解,总以为跟一般OJ大同小异,直到最近点开了一篇博文 ...
- LeetCode189——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
#189. Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 ...
- [Swift]LeetCode189. 旋转数组 | Rotate Array
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- LeetCode189:Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- leetcode189. 旋转数组
方法 4:使用反转算法 这个方法基于这个事实:当我们旋转数组 k 次, k\%nk%n 个尾部元素会被移动到头部,剩下的元素会被向后移动. 在这个方法中,我们首先将所有元素反转.然后反转前 k 个元素 ...
- 面试题(9)之 leetcode-189
题目描述 解法一: /** * @param {number[]} nums * @param {number} k * @return {void} Do not return anything, ...
- 2017-3-6 leetcode 118 169 189
今天什么都没发生 ================================================= leetcode118 https://leetcode.com/problems ...
随机推荐
- socket创建TCP服务端和客户端
看情况选择相对应的套接字*面向连接的传输--tcp协议--可靠的--流式套接字(SOCK_STREAM)*面向无连接的传输--udp协议--不可靠的--数据报套接字(SOCK_DGRAM) 在liun ...
- bootstrap+Ajax+SSM(maven搭建)表单增删改查
前后端分离,前端利用ajax调用后端API接收json数据,进行表单的增删改查 软件架构 IDE:IDEA 数据库:mysql jdk:1.8 tomcat:9 后端:springmvc,mybati ...
- github上对一些名词的理解(之如fork)
fork: Fork 的本义是 叉子(名词) . 比较自然的引申成 分叉(动词) ,就像上面叉子,从左到从右,一条线变成多条了. Git/GitHub 用户下面的图 来表达 Fork:分叉.克隆 出一 ...
- 《DSP using MATLAB》示例Example 8.3
- Scrapy源码研究前戏
一.Twisted的使用 在 Twisted 中,有一种特殊的对象用于实现事件循环.这个对象叫做 reactor.可以把反应器(reactor)想象为 Twisted 程序的中枢神经.除了分发事件循环 ...
- 记一次 FastAdmin CMS 内容提示空的问题
记一次 FastAdmin CMS 内容提示空的问题 有小伙伴反馈 FastAdmin CMS 安装后出现内容有文字,但提示错误 的问题. 我在本地重新安装测试并没有发现这个问题,一切正常,编辑器也可 ...
- 在CentOS上把MySQL从5.5升级到5.6(转)
http://www.th7.cn/db/mysql/201408/66064.shtml 在CentOS上把MySQL从5.5升级到5.6 摘要:本文记录了在CentOS 6.3上,把MySQL从5 ...
- Spring Boot 入门之缓存和 NoSQL 篇(四)
原文地址:Spring Boot 入门之缓存和 NoSQL 篇(四) 博客地址:http://www.extlight.com 一.前言 当系统的访问量增大时,相应的数据库的性能就逐渐下降.但是,大多 ...
- logcat调试系统
日志存放位置:/dev/log shell@xxx:/ $ ls /dev/log -l crw-rw-rw- root log , -- : events crw-rw-rw- root log , ...
- linux中日志文件查找,根据关键字,vi命令,awk和wc
参考: http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html 当时需求:查看系统日志,统计系统的处理时间(从请求进去系统到系 ...