189. Rotate Array - LeetCode
Question

Solution
题目大意:数组中最后一个元素移到第一个,称动k次
思路:用笨方法,再复制一个数组
Java实现:
public void rotate(int[] nums, int k) {
int[] numsCopy = Arrays.copyOf(nums, nums.length);
for (int i=0; i<nums.length; i++) {
nums[(i+k)%nums.length] = numsCopy[i];
}
}
别人的实现:
public void rotate(int[] nums, int 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[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
189. Rotate Array - LeetCode的更多相关文章
- <LeetCode OJ> 189. Rotate Array
189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- 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 ...
- 【LeetCode】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- Java for 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 ...
- 【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 ...
- 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 ...
- 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 ...
随机推荐
- 7_根轨迹_Part1_“根”的作用
这里的渐近线,应该是e^[**wn]/wd,忘记除wd了
- 腾讯云+社区开发者大会开启报名,WeGeek 邀你一起聊聊小程序
刚满 2 岁的微信小程序,正给我们带来一种全新轻便的生活方式. 内测时的青涩还历历在目,到现在,小程序生态已日渐成熟.超过 150 万开发者在这里找到了自己的新天地,打磨出超过 100 万个小程序. ...
- testview属性之详解
安卓开发当中TextView是最常用的组件之一了,那么现在就来详细的了解下TextView的属性: Android:autoLink设置是否当文本为URL链接/email/电话号码/map时,文本显示 ...
- vue后台管理系统组件弹窗
//addFormVisibleIcon可在data中设置true与falsehttps://element.eleme.io/#/zh-CN/component/installation <e ...
- 3.SRE.操作手册:基础篇
SRE的根基起码应该包括:SLO.监控.告警.减少琐事和简单化. SLO(服务质量目标):用于描述服务可靠性的程度. SRE的职责并不只是将"所有工作"都自动化,并保持" ...
- Java语言学习day04--7月1日
###09数据类型转换_自动转换 * A: 自动类型转换 * a:表示范围小的数据类型转换成范围大的数据类型,这种方式称为自动类型转换 自动类型转 ...
- RecyclerView + SQLite 简易备忘录-----中(2)
(3)RecyclerView的实现 ---中间的内容 RecyclerView是一个比ListView更加强大的滚动控件.要使用这个控件需要先在项目的build.gradle中添加RecyclerV ...
- 『现学现忘』Git基础 — 10、配置Git用户签名说明
目录 1.为什么要创建用户签名 2.为什么要在Git中配置这些信息 3.创建用户签名的方式 4.总结 1.为什么要创建用户签名 作为版本控制系统的客户端,每台客户机对版本库的所有提交操作,都需要注明操 ...
- Vue2响应式原理
vue2响应式原理 vue的特性:数据驱动视图和双向数据绑定.vue官方文档也提供了响应式原理的解释: 深入响应式原理 Object.defineProperty() Object.definePro ...
- Apache Flink系列-④有状态函数
有状态函数:独立于平台的有状态无服务器堆栈 这是一种在现代基础设施上创建高效.可扩展且一致的应用程序的简单方法,无论规模大小. 有状态函数是一种API,它通过为无服务器架构构建的运行时简化了分 ...