(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 [1,2,3,4,5,6,7]
is rotated to [5,6,7,1,2,3,4]
.
题目:
给一数组,n个元素,将数组向右移动循环移动k个元素。
思路:
注意题目的要求是循环移动,所以k可以是任意非负整数,但数组元素个数为n,因此k=k%n。
一种通用的思路就是:
1、翻转整个数组 :A[0…n-1];如[7,6,5,4,3,2,1]
2、翻转数组前k个:A[0,k-1];如[5,6,7,4,3,2,1]
3、翻转数组后n-k个:A[k,n-1];如[5,6,7,1,2,3,4]
类似的应用:翻转句子中的全部单词,单词内容不变。
代码:
class Solution {
public:
void reverse(int nums[],int i,int j){
while(i<j){
int tmp=nums[i];
nums[i]=nums[j];
nums[j]=tmp;
i++;
j--;
}
} void rotate(int nums[], int n, int k) {
k=k%n;
if(k>= && n> && k<=n){
reverse(nums,,n-);
reverse(nums,,k-);
reverse(nums,k,n-);
}
}
};
(LeetCode 189)Rotate 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 arr ...
- LeetCode算法题-Rotate Array(Java实现)
这是悦乐书的第184次更新,第186篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第43题(顺位题号是189).给定一个数组,将数组向右旋转k步,其中k为非负数.例如: ...
- (LeetCode 78)SubSets
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- (LeetCode 72)Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- LeetCode OJ: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 153)Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- 剑指offer 66. 构建乘积数组(Leetcode 238. Product of Array Except Self)
剑指offer 66. 构建乘积数组 题目: 给定一个数组A[0, 1, ..., n-1],请构建一个数组B[0, 1, ..., n-1],其中B中的元素B[i] = A[0] * A[1] * ...
- 算法学习笔记(LeetCode OJ)
================================== LeetCode的一些算法题,都是自己做的,欢迎提出改进~~ LeetCode:http://oj.leetcode.com == ...
- (LeetCode 41)First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
随机推荐
- Kolibri v2.0-Buffer Overflow成功复现
Kolibri v2.0-Buffer Overflow成功复现及分析 文件下载地址:http://pan.baidu.com/s/1eS9r9lS 正文 本次讲解用JMP ESP的方法溢出 关于网上 ...
- CentOS下重新安装yum的方法
不小心误删除了VPS下面的yum,大家都知道yum在linux中是很重要的一个功能,软件的下载,系统的更新都要靠他.没有yum,系统基本处于半残废状态. yum的安装操作: 在SSH里面依次输入下面的 ...
- leetcode659. Split Array into Consecutive Subsequences
leetcode659. Split Array into Consecutive Subsequences 题意: 您将获得按升序排列的整数数组(可能包含重复项),您需要将它们拆分成多个子序列,其中 ...
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- MYSQL分段统计
产品表 CREATE TABLE `product` ( `product_id` int(11) NOT NULL AUTO_INCREMENT, `product_model` varchar(2 ...
- Set常用子类特点
HashSet: 重写 hashCode和equals方法 特点:无序,唯一 底层结构是: ...
- JavaScript Promises
上篇文章介绍了JavaScript异步机制,请看这里. JavaScript异步机制带来的问题 JavaScript异步机制的主要目的是处理非阻塞,在交互的过程中,会需要一些IO操作(比如Ajax请求 ...
- Notepad++源代码阅读——窗口元素组织与布局
1.1 前言 这两天在看notepad++ 1.0版本的源代码.看了许久终于把程序的窗口之间的关系搞清楚了现在把其组织的要点写于此,希望对大家有所帮助. 1.2 窗口元素之间的关系 Notepad++ ...
- 选择问题(selection problem)
/* 本文是选择问题: 选择一组N个数当中的第k小的数(第k大的数类似) 集中方法的实现代码 */ #include "sorting.h" #incl ...
- 关于ClickOnce的一些技术文章
程序自动升级是我们经常遇到的需求,对于.Net程序来说,一个简单易用的方案是它内置的ClickOnce技术.ClickOnce出现的比较早,网上相应的教程还是比较丰富的,我这里就简单的整理一下相关的文 ...