LeetCode题解之Rotate Array
1、题目描述
2、代码
void rotate(vector<int>& nums, int k) { if( k == )
return ;
if( k % nums.size() == && (k / nums.size()) % == )
reverse( nums.begin(), nums.end() ); k %= nums.size() ; vector<int> ans;
auto rb = nums.rbegin();
stack<int> s;
for( int i = k; i > ; i--,rb++)
{
s.push(*rb);
} while( !s.empty() )
{
int tmp = s.top();
ans.push_back(tmp);
s.pop(); }
auto b = nums.begin();
for( int j = ; j < nums.size() - k; j++,b++)
{
ans.push_back(*b);
}
copy(ans.begin(),ans.end(),nums.begin()); }
LeetCode题解之Rotate Array的更多相关文章
- 【LeetCode题解】数组Array
1. 数组 直观地看,数组(Array)为一个二元组<index, value>的集合--对于每一个index,都有一个value与之对应.C语言中,以"连续的存储单元" ...
- 【LeetCode】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- 【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 OJ 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算法题-Rotate Array(Java实现)
这是悦乐书的第184次更新,第186篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第43题(顺位题号是189).给定一个数组,将数组向右旋转k步,其中k为非负数.例如: ...
- LeetCode题解:Rotate List
Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For exa ...
- (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 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 题解之Rotate List
1.题目描述 2.题目分析 首先数出链表中的元素个数count,然后对k = k % count 求余数.接着将链表形成一个环,从最后一个元素开始,运动 count - k 步,此时节点的下一个节点 ...
随机推荐
- 下拉菜单;手风琴;九宫格的Jquery的使用实例
下拉菜单;手风琴;九宫格的Jquery的使用实例 1.下拉菜单 效果如图: 代码如下: <!DOCTYPE html> <html lang="en"> & ...
- sql_auoload_regiester() 解释(转载)
在了解这个函数之前先来看另一个函数:__autoload. 一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数.看下面例子: 运行index.P ...
- 前端通信:ajax设计方案(三)--- 集成ajax上传技术
在此之前让我感慨一下现在的前端开发的氛围.我遇到好多人,给我的观念都是,这个东西这个框架有了,那个东西那个框架做了,前端嘛,学几个框架,这个拼凑一下那个拼凑一下就好了.其实我想问,东西都框架做了,那你 ...
- Chapter 3 Phenomenon——7
"Bella? Are you all right?" “贝拉?你还好吗?” "I'm fine." My voice sounded strange. “我还 ...
- Nodejs学习笔记(十六)—Pomelo介绍&入门
前言&介绍 Pomelo:一个快速.可扩展.Node.js分布式游戏服务器框架 从三四年前接触Node.js开始就接触到了Pomelo,从Pomelo最初的版本到现在,总的来说网易出品还算不错 ...
- ASP .NET Core 基本知识点示例 目录
ASP.NET Core 的 运行机制 文章 ASP.NET Core 中的 配置 文章 源代码 ASP.NET Core 中的 依赖注入 文章 源代码 ASP.NET Core 中的 日志 文章 源 ...
- 微服务 + Docker + Kubernetes 入门实践 目录
微服务 + Docker + Kubernetes 入门实践: 微服务概念 微服务的一些基本概念 环境准备 Ubuntu & Docker 本文主要讲解在 Ubuntu 上安装和配置 Dock ...
- elasticdump
elasticdump备份elasticsearch里面的某个索引数据 1. 安装环境 需要node.npm.yarn # 去官方下载最新版本的nodejs #wget https://nod ...
- iOS 微信支付SDK与微信友盟分享两者同时集成时,出现的问题与解决之路。
这两天改版一个旧的APP,要旧貌换新颜,拿到app后进行编译,一直报下面的错误. 报不认识的符号名PayReq错误.奇怪,啥也没动就这样,真不知道给的包是不是本来就是个报错的工程. 不管怎样,要对它修 ...
- CPU结构与指令执行过程简介
CPU(Central Processing Unit)是计算机中进行算术和逻辑计算处理指令的主要部件. CPU结构 CPU由通用寄存器组,运算器,控制器和数据通路等部件组成. 寄存器包括 数据寄存器 ...