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

解题思路:

JAVA实现如下:

    public void rotate(int[] nums, int k) {
k%=nums.length;
k=nums.length-k;
int[] nums2=new int[k];
for(int i=0;i<k;i++)
nums2[i]=nums[i];
for(int i=0;i<nums.length-k;i++)
nums[i]=nums[i+k];
for(int i=0;i<k;i++)
nums[nums.length-k+i]=nums2[i];
}

Java for LeetCode 189 Rotate Array的更多相关文章

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

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

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

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

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

  5. 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,.. ...

  6. 189. Rotate Array - LeetCode

    Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...

  7. &lt;LeetCode OJ&gt; 189. Rotate Array

    189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...

  8. 189. Rotate Array【easy】

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

  9. 【LeetCode】Rotate Array

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

随机推荐

  1. NOI题库--图论 宗教信仰

    1526:宗教信仰 总时间限制: 5000ms 内存限制: 65536kB 描述 世界上有许多宗教,你感兴趣的是你学校里的同学信仰多少种宗教. 你的学校有n名学生(0 < n <= 500 ...

  2. 洛谷P1202 [USACO1.1]黑色星期五Friday the Thirteenth

    题目描述 13号又是一个星期五.13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数.给出N年的一个周期,要求计算1900年1月1日至1900+N- ...

  3. CODEVS1995 || TYVJ1863 黑魔法师之门

    P1863 [Poetize I]黑魔法师之门 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 经过了16个工作日的紧张忙碌,未来的人类终于收集到了足够的能源 ...

  4. Linux System Reinforcement、Intrusion Detection Based On syslog

    目录 .文件系统及访问权限 . Linux Syslog . Linux日志审计 . 帐号安全管理 . 基础物理安全 . 系统编译环境安全 . 系统病毒.后门.rootkit安全 . 系统端口.服务安 ...

  5. 迪杰斯特拉(Java)

    public class Dijsktra { public static void main(String[] args) { Dijsktra d=new Dijsktra(); int[][] ...

  6. 迷宫问题(bfs)

    import java.util.LinkedList; import java.util.Queue; import java.util.Stack; public class BFS { priv ...

  7. DLUTOJ 1209 字典序和r-子集

    传送门 Time Limit: 6 Sec  Memory Limit: 128 MBSubmit: 73  Solved: 14 Description Input 多组输入数据. 每组数据: 第一 ...

  8. IOS基础之 (九) Foundation框架

    一NSNumber 类型转换 NSNumber 把基本数据类型包装成一个对象类型.NSNumber之所以可以(只能)包装基本数据类型,是因为继承了NSValue. @20 等价于 [NSNumber ...

  9. resharper安装后,一不小心点错了(选择了object browser)

    打开Resharper,选择Options,然后选择Tools中的External Sources,你的情况是选择了Navigation to Object Brower这一项了,换成第一个Defau ...

  10. ArrayList与LinkedList区别

    ArrayList 采用的是数组形式来保存对象的,这种方式将对象放在连续的位置中,所以最大的缺点就是插入删除时非常麻烦LinkedList 采用的将对象存放在独立的空间中,而且在每个空间中还保存下一个 ...