Given an array of integers, replace every element with the next greatest element (greatest element on the right side) in the array. Since there is no element next to the last element, replace it with -1. For example, if the array is [16, 17, 4, 3, 5, 2], then it should be modified to [17, 5, 5, 5, 2, -1].

Example

Give nums = [16, 17, 4, 3, 5, 2], change nums to [17, 5, 5, 5, 2, -1]
You should do it in place.

解法一:

 class Solution {
public:
/*
* @param : An array of integers.
* @return: nothing
*/
void arrayReplaceWithGreatestFromRight(vector<int> &nums) {
int size = nums.size();
int max = nums[size - ];
nums[size - ] = -; for (int i = size - ; i >= ; --i) {
int temp = nums[i];
nums[i] = max;
max = max > temp ? max : temp;
}
}
};

本题比较简单,就是从尾开始往前遍历处理。注意题目中的题意是某个元素右边所有元素中的最大值,而不涉及该元素和最大值之间再比较。

735. Replace With Greatest From Right【medium】的更多相关文章

  1. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  2. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  3. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  4. 61. Search for a Range【medium】

    61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...

  5. 62. Search in Rotated Sorted Array【medium】

    62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...

  6. 74. First Bad Version 【medium】

    74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...

  7. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  8. 159. Find Minimum in Rotated Sorted Array 【medium】

    159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...

  9. Java for LeetCode 207 Course Schedule【Medium】

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. VMware 8安装Mac OS X 10.7

    (Windows 7 X64环境下,VMware 8.0正式版)虚拟机首尝MAC OS X 10.7 Lion系统成功,特将此好消息分享.2年了,终于我也装上了Mac,我也成功的尝到了苹果味道,看着那 ...

  2. 简单nginx+tomca负载均衡

    Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性: 作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 ...

  3. 用google mock模拟C++对象

    google mock是用来配合google test对C++项目做单元测试的.它依赖于googletest(参见我上篇文章<如何用googletest写单元测试>: http://blo ...

  4. 【文件监控】之一:理解 ReadDirectoryChangesW part1

    理解 ReadDirectoryChangesW 原作者:Jim Beveridge 原文:http://qualapps.blogspot.com/2010/05/understanding-rea ...

  5. Delphi 资源文件( .res)

    一.    现在的Windows应用程序几乎都使用图标.图片.光标.声音等,我们称它们为资源(Resource).最简单的使用资源的办法是把这些资源的源文件打入软件包,以方便程序需要的时候调用.资源是 ...

  6. centos7 keepalived以及防火墙配置

    安装和配置keepalived,网上有很多资料,但是都没有提到防火墙这块,而且很多都是互相抄袭的,就算配置对了也会遇到很多问题 在查阅资料的时候配置好了keepalived,但是出现了裂脑,即两台服务 ...

  7. VMware安装报VT-x未开启的解决办法

    摘自: http://www.bloomylife.com/?p=650 前段时间MSDN上放出WIN8最终版的ISO文件,心里一直痒痒.最近闲来无事,想体验下WIN8的魅力.考虑到新系统刚面世,在驱 ...

  8. 【实践】用 js 封装java shuffle函数(打乱数组下标方法)

    此方法返回的会是一个全新的数组 所以并不会像java里的shuffle函数一样返回一个引用一样的数组 思路如下: 1.新建一个函数传入需要打乱下标的数组 2.获取数组的长度 3.新建一个用来保存并且返 ...

  9. dubbo-monitor安装监控中心,管理控制台安装网页一直访问不到,解决bug的方式记录

    问题再现 第一步,重启机器 第二步,按照dubbo-monitor需要开启的服务,顺序逐一进行启动,之前的推翻全部重新再走一遍就通了,千万不能死磕...很浪费时间.... 通过打印日志追踪问题的所在. ...

  10. Win7如何关闭操作中心的图标

    运行gpedit.msc,打开组策略编辑器   双击"删除操作中心图标",将其启用即可(重启可见)