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. 【剑指offer】输入一颗二叉树的根节点,求二叉树的深度,C++实现

    原创博文,转载请注明出处! # 题目 # 举例        下图二叉树的深度为4,最长路径为1-2-5-7. # 思路(递归)       如果一个树只有一个节点,它的深度为1: 如果根节点只有左子 ...

  2. [转载][QT][SQL]sq]学习记录1_模糊搜索

    转载自:sql学习网站: http://www.w3school.com.cn/sql/index.asp 用于模糊搜索数据库的数据 语句:http://www.w3school.com.cn/sql ...

  3. CocoaPods(pod install一直不动)

    CocoaPods安装和使用教程 如何在Mac 终端升级ruby版本 RubyGems 镜像 cocoapods无法使用(mac os 10.11升级导致pod: command not found)

  4. python中的字典两种遍历方式

    dic = {"k1":"v1", "k2":"v2"} for k in dic: print(dic[K]) for ...

  5. 利用ajax完成项目图册上传删除【实际项目】

    [项目页面效果] [前台jsp页面] jsp的js代码 <script type="text/javascript"> //上传项目图片 function upload ...

  6. Flask第三篇——安装Flask

    现在我们来安装Flask: Windows系统安装Flask 如果你的系统是Windows,那安装起来非常方便——pip install flask Mac系统安装Flask Mac安装Flask一般 ...

  7. CMDB

    一.CMDB CMDB --Configuration Management Database 配置管理数据库, CMDB存储与管理企业IT架构中设备的各种配置信息,它与所有服务支持和服务交付流程都紧 ...

  8. lapis docker 运行说明

    1. lapis docker 镜像制作 因为openresty 新版本一个json 库的问题,我们使用的是 openresty:1.11.2.1 基础镜像 FROM openresty/openre ...

  9. smarty 模板编译和变量调节器 模板引入

    <?php require './smarty/Smarty.class.php'; $sm = new Smarty; //$sm->force_compile = true; $sm- ...

  10. vuejs2.0的生命周期解读

    每个 Vue 实例在被创建之前都要经过一系列的初始化过程.例如,实例需要配置数据观测(data observer).编译模版.挂载实例到 DOM ,然后在数据变化时更新 DOM .下图展示的就是一个v ...