原体描写叙述例如以下:

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.

[show hint]

Hint:

Could you do it in-place with O(1) extra space?

Related problem: Reverse Words in a String II

我的解答例如以下:

public class Solution {
public void rotate(int[] nums, int k) {
if(nums==null||k==0)
{
return;
}
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[] a ,int start, int end)
{
while(start<end)
{
int temp = a[start];
a[start] = a[end];
a[end] = temp;
start++;
end--;
}
}
}

(LeetCode)旋转数组的更多相关文章

  1. leetcode旋转数组查找 二分查找的变形

    http://blog.csdn.net/pickless/article/details/9191075 Suppose a sorted array is rotated at some pivo ...

  2. [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  ...

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

  4. 【Leetcode】【简单】【189. 旋转数组】【JavaScript】

    189. 旋转数组 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3输出: [5,6,7,1,2,3,4]解释 ...

  5. LeetCode初级算法--数组02:旋转数组

    LeetCode初级算法--数组02:旋转数组 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/ ...

  6. [LeetCode] 189. Rotate Array 旋转数组

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

  7. 前端与算法 leetcode 189. 旋转数组

    目录 # 前端与算法 leetcode 189. 旋转数组 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 189. 旋转数组 题目描述 189. 旋转数组 概要 把他当做一到简单 ...

  8. LeetCode 189. 旋转数组(Rotate Array)

    189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...

  9. 【LeetCode每天一题】Search in Rotated Sorted Array(在旋转数组中搜索)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., ...

随机推荐

  1. BNU Concentric Rings

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=16030 Concentric Rings   There are several different ...

  2. c++ 编译时检测结构体大小的的宏定义写法

    一种写法: template <bool> struct CompileAssert { }; #define COMPILE_ASSERT(expr, msg) \ typedef Co ...

  3. easyui form validate总是返回false原因

    最近做表单验证用了easyui form组件.又一次发现在测试表单都填写正确了但是调试表单的代码监测到调用form的"validate"方法总是返回false 最后查了一下原因在h ...

  4. Fusioncharts的导出图片訪问官网问题

    Fusioncharts3.5使用自带的导出功能,须要訪问官网 问题描写叙述:使用fusioncharts自带的exportchart方法来导出图片的时候.要訪问export.api3.fusionc ...

  5. Web Service 的工作原理(转载)

    Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的 ...

  6. 互斥锁属性PTHREAD_MUTEX_RECURSIVE

    四.互斥锁属性 线程和线程的同步对象(互斥量,读写锁,条件变量)都具有属性.在修改属性前都需要对该结构进行初始化.使用后要把该结构回收.我们用pthread_ mutexattr_init函数对pth ...

  7. 微信小程序 - 实现购物车结算

    示例源码下载:小程序-实现购物车结算

  8. 学习中遇到的c++问题,持续更新

    原文请訪问我的博客:http://xiaoshig.sinaapp.com/ 向上取整 使用ceil函数.ceil(x)返回的是大于x的最小整数.如: ceil(2.5) = 3 ceil(-2.5) ...

  9. 【Oracle】迁移含有BLOG类型的字段的表

    日常数据库迁移过程中,发现dmp文件中的表,如果表含有blog类型的字段,无法进行迁移. 以下是我迁移含有blog类型字段表的经验分享' 使用PLSQL Developer进行导出 接着选择需要导出的 ...

  10. 使用JDK自带的Stax操作XML

    操作的books.xml <?xml version="1.0" encoding="UTF-8"?> <bookstore> < ...