称号

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]

Related problem: Reverse Words in a String II

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.

代码

public class Solution {
public void rotate(int[] nums, int k) {
int len=nums.length;
if(k==0 || k==len) return;
if(k>len) k=k-len;
int[] result=new int[len];
int result_index=0;
int j=0;
for(int i=k;i>=1;i--){
result[result_index]=nums[len-i];
result_index++;
}
while(result_index<len){
result[result_index]=nums[j];
result_index++;
j++;
}
for(int m=0;m<len;m++){
nums[m]=result[m];
}
}
}

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

版权声明:本文博客原创文章,博客,未经同意,不得转载。

【LeetCode从零单排】No189 .Rotate Array的更多相关文章

  1. LeetCode 189:旋转数组 Rotate Array

    公众号:爱写bug(ID:icodebugs) 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. Given an array, rotate the array to the ...

  2. 【LeetCode从零单排】No15 3Sum

    称号 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...

  3. 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters

    题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...

  4. 【LeetCode从零单排】No.135Candy(双向动态规划)

    1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  5. 【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List

    题目 Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 ...

  6. LeetCode: Reverse Words in a String && Rotate Array

    Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...

  7. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  8. 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

    Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...

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

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

随机推荐

  1. java该HashTable,HashMap和HashSet

    同一时候我们也对HashSet和HashMap的核心方法hashcode进行了具体解释,见<探索equals()和hashCode()方法>. 万事俱备,那么以下我们就对基于hash算法的 ...

  2. JDBC连接数据库经验技巧(转)

    Java数据库连接(JDBC)由一组用 Java 编程语言编写的类和接口组成.JDBC 为工具/数据库开发人员提供了一个标准的 API,使他们能够用纯Java API 来编写数据库应用程序.然而各个开 ...

  3. Red Gate系列之三 SQL Server 开发利器 SQL Prompt 5.3.4.1 Edition T-SQL智能感知分析器 完全破解+使用教程

    原文:Red Gate系列之三 SQL Server 开发利器 SQL Prompt 5.3.4.1 Edition T-SQL智能感知分析器 完全破解+使用教程 Red Gate系列之三 SQL S ...

  4. JavaScript实现的购物车效果-效果好友列表

    JavaScript实现的购物车效果.当然,可以在许多地方使用这种效果.朋友的.例如,在选择.人力资源模块,工资的计算,人才选拔等..下面来看一下班似有些车效果图: watermark/2/text/ ...

  5. iOS 获取联系人,并调用系统地址簿UI

    1.加入 AddressBook库 推断授权状态 -(bool)checkAddressBookAuthorizationStatus { //取得授权状态 ABAuthorizationStatus ...

  6. 修改系统环境变量 cmd命令

    详细大家对cmd的使用都有了一些简单的了解,但是困扰大家的主要的问题就是: cmd命令修改环境变量有两种方式:1. 短期内有效,在关闭dos窗口后就自动失效 2.长期有效,关闭dos窗口后还有效 下面 ...

  7. asp.net在用户控件中使用ClientScript

    在用户空间中调用ClientScript.RegisterClientScriptBlock方法 ClientScript的命名空间是System.Web.UI.Page,并且要实例化之后的Page才 ...

  8. android性能测试内存泄漏

    1.什么是内存泄漏?     适用于该系统的内存使用内存泄漏,未回复(释放),该内存可以没有事业,也不能被其他人使用使用自己. 2.出有什么差别?    内存泄漏是分配出去的内存无法回收.    内存 ...

  9. AND信号灯和信号灯集-----同步和互斥解决面向对象(两)

    AND信号 互斥的上述处理,它是针对仅在进程之间共享的一个关键资源方面.在一些应用.这是一个过程,需要在为了自己的使命后,获得两个或多个其他共享资源运行. 个进程A和B.他们都要求訪问共享数据D和E. ...

  10. SICP-2锻炼.34

    [锻炼2.34] 为x给定值,找到一个多项式x的值,它也可以被形式化为累积. 下多项式的值: an*x^n + an-1*x^n-1 + .... + a1*x + a0 採用著名的Horner规则, ...