(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] ...
随机推荐
- CF893F Subtree Minimum Query 主席树
如果是求和就很好做了... 不是求和也无伤大雅.... 一维太难限制条件了,考虑二维限制 一维$dfs$序,一维$dep$序 询问$(x, k)$对应着在$dfs$上查$[dfn[x], dfn[x] ...
- 利用Pastezort渗透win7
下载Pastezort git clone https://github.com/ZettaHack/PasteZort.git 给Pastezort文件夹提升权限 /root/PasteZort/ ...
- KMP 理解
例题 以字符串 ABABACA 为例 a 前缀: 后缀: 结果为0 ab 前缀:a 后缀: b 结果为0 aba 前缀:a ab 后缀: ba a 结果为1,此时 i=2,j=1 abab 前缀:a ...
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts bfs
B. Mike and Shortcuts 题目连接: http://www.codeforces.com/contest/689/problem/B Description Recently, Mi ...
- Python学习笔记(一):Python基础学习
总结的内容: 1.变量的命名 2.脚本的注释 3.运算符 4.用户输入语句 一.变量的命名 1.变量用于引用在程序中可能会变化的值.它们被称为变量是因为它们可能引用存储在内存中的不同的值. 2.变量的 ...
- MyBatis连接SQL Server的关键点
一.Maven包配置 <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc --> < ...
- 通过 ssh 登录到手机 Termux
通过ssh登录到手机 Termux 测试环境 电脑: macOS Mojave 手机: Huawei Mate10Pro Termux是Android上的一个非常强大的终端模拟器. 强大之处在于支持使 ...
- c#分页工具类,完美实现List分页
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Proje ...
- js Date 函数方法
var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...
- tomcat 的缓存机制
事出做项目时一个jsp页面修改后一直没有读出来,后来仔细研究了下tomcat才发现 当请求jsp页面时,Tomcat会分派给JspServlet来处理,在jspServlet的方法 service() ...