【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.
[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相关推荐
- 【LeetCode从零单排】No26.Remove Duplicates from Sorted Array
题目 题目要求:去除sort int数组中的重复项. Given a sorted array, remove the duplicates in place such that each ...
- 【LeetCode从零单排】No88.Merge Sorted Array
题目 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume ...
- leetcode python3 简单题189. Rotate Array
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百八十九题 (1)题目 英文: Given an array, rotate t ...
- 【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)
1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a stree ...
- 【LeetCode从零单排】No121 Best Time to Buy and Sell Stock
题目 Say you have an array for which the ith element is the price of a given stock on day i. If you we ...
- 【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.169 Majority Element(hashmap用法)
题目 Given an array of size n, find the majority element. The majority element is the element that app ...
- 【LeetCode从零单排】No70.ClimbingStairs
题目 爬楼梯问题,这是一道很有趣的问题.首先看题目: You are climbing a stair case. It takes n steps to reach to the top. Each ...
- 【LeetCode从零单排】No27.Remove Element
题目 Given an array and a value, remove all instances of that value in place and return the new length ...
最新文章
- myeclipse中,项目上有个叉报错,文件没有错误
- java圆柱的底面积体积,计算圆柱的底面积和体积
- uboot 与系统内核中 MTD分区的关系
- 专访联想谢政维:功耗和价格是天蝎项目最大障碍!
- mybatis 拼接_关于 Mybatis的 $ 和 # , 你真的知道他们的细节吗?
- python画简便的图-如何用python画M集(1)——M集原始图的画法
- Python入门--列表的查询操作--获取列表知道那个元素的索引、获取列表中的单个元素、获取列表中的多个元素
- HTML - 元素/标签和属性基础
- C++11 random库
- 转载 --关于增量编译
- 【转】基于知识图谱的推荐系统(KGRS)综述
- 你都用 Python 来做什么?
- 什么是 AngularJS?
- 欠钱不还直接打,打到还钱才停
- mysql 存储树形结构
- 计算机软件防干扰,抗干扰技术
- 电信业服务流程设计--用例图
- 2000年数模B题 钢管订购和运输
- 到2030年,联网智能机器人将成为人们生活的主要组成部分
- python学习笔记8
热门文章
- PAT甲级1054 The Dominant Color:[C++题解]哈希表、水题
- 数据结构_栈和队列的区别
- java十进制输出_JAVA输入一个十进制数N,输出r进制的数
- mysql中存储过程另存为_转: MySQL中的存储过程
- xampp php源码的路径,php – XAMPP中的根路径
- mysql批量修改http为https,墨涩网 - typecho系统升级全站https数据库批量替换网址/内容——墨涩网...
- auto_ptr使用介绍
- 张宁北大计算机系,同是北大出身,差距悬殊!张宁在山西坐冷板凳,祝铭震已坐稳首发...
- vim 变成只读了_Vim 强制保存只读类型文件的方法
- 把mysql部署在局域网的服务器上,远程连接mysql时报错误代码1130 Host ‘***.***.***.***’is not allowed to connect to this MySQL