735. Replace With Greatest From Right【medium】
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]
.
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】的更多相关文章
- 2. Add Two Numbers【medium】
2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 75. Find Peak Element 【medium】
75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...
- 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 ...
- 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 ...
随机推荐
- FIS的安装
FIS是专为解决前端开发中自动化工具.性能优化.模块化框架.开发规范.代码部署.开发流程等问题的前端工程化构建工具. 最重要的是,它是国产的!还是百度产的~~~亲切吧~~官网:http://fis.b ...
- 2014 linux
[51CTO精选译文]每年大概12月前后,人们喜欢给出种种预测,预言他们认为未来一年技术界会出现什么样的变化.本文也不例外,只不过侧重介绍2014年值得关注的十大最受关注的Linux发行版(桌面版或移 ...
- rbac控制下无法创建poddisruptionbudgets
先通过下面命令找到具体的命名空间的rbac kubectl get role --all-namespaces kubectl get role aaa -o yaml 然后倒入到yaml文件中添加 ...
- Mysql - 登录错误
来源: 问题的引出: 我 在CentOS上装完mysql后,用navicat链接的时候,抛出MySql - SQL Error (1130): Host IP is not allowed to co ...
- 二.Consumer、Producer简单例子
1.先导入jar包,我使用的是maven <dependency> <groupId>com.alibaba.rocketmq</groupId> <arti ...
- WinForm客户端调用 WebService时 如何启用Session
WinForm客户端调用 WebService时 如何启用Session 摘自: http://www.cnblogs.com/swtseaman/archive/2011/04/18/2020176 ...
- 《javascript高级程序设计》读书笔记(四)引用类型
第五章:引用类型 Object类型 创建object实例的两种方式: 1.new方式 var person = new Object(); person.name = "haozk" ...
- wampserver 下载链接没反应的解决办法
可能有很多小伙伴和我一样使用wampserver时,下载链接点击就是没有反应,当时我以为是因为网络原因,链接没有加载出来,或者是链接的请求不能得到响应,结果百度了一下才发现被“习惯”坑了一把,wamp ...
- HTTP状态码及说明
- 我的自动化测试历程(Selenium+TestNG+Java+ReportNG+Jenkins)
原地址:http://blog.csdn.net/shilinjie_8952/article/details/53380373?locationNum=11&fps=1 测试环境:Java+ ...