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的更多相关文章

  1. 倒转数组 Leetcode189

    倒转数组 Leetcode189 记录调整数组顺序而不需要另加内存的一种方法: 题目 189. 旋转数组 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [ ...

  2. 理解JavaScript中的参数传递 - leetcode189. Rotate Array

    1.关于leetcode 这是第一篇关于leetcode的题解,就先扯点关于leetcode的话. 其实很早前就在博客园看到过leetcode一些题解,总以为跟一般OJ大同小异,直到最近点开了一篇博文 ...

  3. 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  ...

  4. Leetcode-189 Rotate Array

    #189.    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 ...

  5. [Swift]LeetCode189. 旋转数组 | Rotate Array

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

  6. 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 ...

  7. leetcode189. 旋转数组

    方法 4:使用反转算法 这个方法基于这个事实:当我们旋转数组 k 次, k\%nk%n 个尾部元素会被移动到头部,剩下的元素会被向后移动. 在这个方法中,我们首先将所有元素反转.然后反转前 k 个元素 ...

  8. 面试题(9)之 leetcode-189

    题目描述 解法一: /** * @param {number[]} nums * @param {number} k * @return {void} Do not return anything, ...

  9. 2017-3-6 leetcode 118 169 189

    今天什么都没发生 ================================================= leetcode118 https://leetcode.com/problems ...

随机推荐

  1. day4(带)

    int is_his_file(char*filename,char *username) { int ret; struct passwd *user_info;// struct stat sta ...

  2. iOS UIWebView 中 js调用OC 打开相册 获取图片, OC调用js 将图片加载到html上

    线上html <!DOCTYPE html> <html> <head> <title>HTML中用JS调用OC方法</title> < ...

  3. 实现同时提交多个form(基础方法) 收集(转)

    方法一: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4 ...

  4. save the transient instance before flushing错误解决办法 【待完善】

    近日在项目中遇到以下错误,着实郁闷了一把: org.hibernate.TransientObjectException: object references an unsaved transient ...

  5. FastJson的一些使用

    前言 最近经常使用json的一些转换,使用的是fastjson,所以就对fastjson进行了一些汇总,记录下来. 正文 主要的api 首先是一些类库的说明: SerializeWriter:相当于S ...

  6. 为iPhone 6设计自适应布局(iOS8)

    Apple从iOS 6加入了Auto Layout后开始就比较委婉的开始鼓励.建议开发者使用自适应布局,但是到目前为止,我感觉大多数开发者一直在回避这个问题,不管是不是由于历史原因造成的,至少他们在心 ...

  7. JavaScript 定时任务多事件冲突问题

    var iCount = setInterval(ChangeTime, 3500);//3.5秒请求一次函数 function ChangeTime() { $.ajax({ type: " ...

  8. 【angularJS】启动(bootstrap)机制

    Angular的启动分为手动和自动两种. 自动启动 定义模块的例子中,采用的就是自动的方式:通过内置的指令ngApp 来指定启动时加载的模块.<html ng-app="myApp&q ...

  9. simulink生成hdl的几个理解

    1,simulink生成hdl的话需要用支持的模块,否则不能生成,支持的模块组成一个库,这个库需要自己生成,用hdllib命令生成,官方原话: The hdllib function creates ...

  10. visibility:hidden和display:none的区别

    大家知道,如果想让某一段代码在前台不显示,最简单的方法是用css的display:none即可,这样下边的内容就自动上移或右侧的左移来填补这个空隙. 但特殊情况下我们只需要隐藏这个元素,但它的位置不能 ...