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;
}
}
};

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

最新文章

  1. [笔记]linux下和windows下的 创建线程函数
  2. VB.NET中Form窗体运行时,按ESC退出全屏状态
  3. [转]asp.net webform 与mvc 共享session
  4. 解读Unity中的CG编写Shader系列七(不透明度与混合)
  5. 修改Netbeans默认使用UTF-8编码
  6. magento设置订单状态
  7. 282. Expression Add Operators
  8. sublime3配置Quick-X+自动错误提示
  9. wikioi 3038 3n+1问题
  10. Photoshop CS6的安装
  11. H264中的SPS、PPS提取与作用
  12. mongodb 简单部署方案及实例
  13. geoserver扫盲 openlayers相关
  14. xlslib安装, aclocal-1.13: command not found, 安装升级autoconf-2.65.tar.gz, automake-1.13.tar.gz两个文件
  15. 【转】Ubuntu14.04搭建安装svnserver
  16. js submit的問題
  17. UESTC_In Galgame We Trust CDOJ 10
  18. SSH框架之Hibernate(1)——映射关系
  19. 火狐兼容window.event.returnValue=false;
  20. Python--day12(三元表达式、函数对象、名称空间与作用域、函数嵌套定义)

热门文章

  1. 微信自动抢红包android实现
  2. iOS: 工具栏控件UIToolBar和工具栏按钮控件UIBarButtonItem的使用
  3. Maven C盘用户文件下没有.m2
  4. 转: Android中的签名机制
  5. 常用命令(过滤、管道、重定向、ping 命令、netstat 命令、ps命令)
  6. Win7如何更改网络位置,如何加入或创建家庭组
  7. java小游戏代码
  8. 从客户端的角度来谈谈移动端IM的消息可靠性和送达机制
  9. Git学习笔记一--创建版本库、添加文件、提交文件等
  10. jQuery实现滚动栏一直处于最底部