【LeetCode从零单排】No189 .Rotate Array
称号
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.
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的更多相关文章
- LeetCode 189:旋转数组 Rotate Array
公众号:爱写bug(ID:icodebugs) 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. Given an array, rotate the array to the ...
- 【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 ...
- 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters
题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...
- 【LeetCode从零单排】No.135Candy(双向动态规划)
1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【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 ...
- 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 ...
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- 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 = ...
- LeetCode 189. 旋转数组(Rotate Array)
189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...
随机推荐
- hdu 5017 Ellipsoid(西安网络赛 1011)
Ellipsoid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- hdu 2191 悼念512四川汶川大地震遇难者——如今宝,感恩生活
悼念512四川汶川大地震遇难者--如今宝,感恩生活 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- Python3.4 邮件(包含附件与中国)
import smtplib import os from email.mime.text import MIMEText from email.mime.multipart import MIMEM ...
- lua学习笔记11:lua中的小技巧
lua中的小技巧,即基础lua语言本身的特种,进行一个些简化的操作 一. 巧用or x = x or v 等价于: if not x then x = v end 假设x为nil或false,就给他赋 ...
- JS window.open()财产
一. Window 对象 Window 对象是 JavaScript 顶级对象层次. Window 对象表示一个浏览器窗口或帧. Window 在对象 <body> 或 <frame ...
- Google Maps Android API v2 (3)- 地图添加到Android应用程序
添加地图的基本步骤是: (一旦)按照以下步骤[入门] [开始],获得API,获取密钥所需的属性,并添加到您的Android清单. 添加一个碎片对象 要处理地图的活动.做到这一点最简单的方法是增加一个 ...
- Blend4精选案例图解教程(四):请给我路径指引
原文:Blend4精选案例图解教程(四):请给我路径指引 路径在界面设计中,可以起到很好的辅助作用,我常常使用它来对元素进行规则排列和非规则排列控制. 本次教程将演示,Blend中路径的常规用法. 1 ...
- Windows Phone – 裁剪图片 (Crop Image)
原文:Windows Phone – 裁剪图片 (Crop Image) 最近在处理图像的功能,对於图像的比例我也不是非常的清楚,因此,在编辑图片上花了不少时间. 该篇文章主要说明的是:如何对图片选择 ...
- 【原创】《算法导论》链表一章带星习题试解——附C语言实现
原题: 双向链表中,需要三个基本数据,一个携带具体数据,一个携带指向上一环节的prev指针,一个携带指向下一环节的next指针.请改写双向链表,仅用一个指针np实现双向链表的功能.定义np为next ...
- 修改系统环境变量 cmd命令
详细大家对cmd的使用都有了一些简单的了解,但是困扰大家的主要的问题就是: cmd命令修改环境变量有两种方式:1. 短期内有效,在关闭dos窗口后就自动失效 2.长期有效,关闭dos窗口后还有效 下面 ...