735. Replace With Greatest From Right【medium】
2024-08-28 16:20:43
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;
}
}
};
本题比较简单,就是从尾开始往前遍历处理。注意题目中的题意是某个元素右边所有元素中的最大值,而不涉及该元素和最大值之间再比较。
最新文章
- [笔记]linux下和windows下的 创建线程函数
- VB.NET中Form窗体运行时,按ESC退出全屏状态
- [转]asp.net webform 与mvc 共享session
- 解读Unity中的CG编写Shader系列七(不透明度与混合)
- 修改Netbeans默认使用UTF-8编码
- magento设置订单状态
- 282.	Expression Add Operators
- sublime3配置Quick-X+自动错误提示
- wikioi 3038 3n+1问题
- Photoshop CS6的安装
- H264中的SPS、PPS提取与作用
- mongodb 简单部署方案及实例
- geoserver扫盲 openlayers相关
- xlslib安装, aclocal-1.13: command not found, 安装升级autoconf-2.65.tar.gz, automake-1.13.tar.gz两个文件
- 【转】Ubuntu14.04搭建安装svnserver
- js submit的問題
- UESTC_In Galgame We Trust CDOJ 10
- SSH框架之Hibernate(1)——映射关系
- 火狐兼容window.event.returnValue=false;
- Python--day12(三元表达式、函数对象、名称空间与作用域、函数嵌套定义)