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 ...
随机推荐
- 【java】随机生成6位的数字 /生成例如010 045这样的三位数
int radomInt = new Random().nextInt(999999) @org.junit.Test public void testName() throws Exception ...
- Linux上安装使用SSH(ubuntu&&redhat)
转自:http://blog.csdn.net/zxs9999/article/details/6930736 Ubuntu安装使用SSH ubuntu默认并没有安装ssh服务,如果通过ssh链接ub ...
- 在 Ubuntu 14.04 上安装 Ubuntu Tweak 0.8.8
转自:http://linux.cn/article-3335-1.html 关于 Ubuntu Tweak,Ubuntu 老用户再熟悉不过了,Ubuntu tweak 相当于 windows 的优化 ...
- Javascript时间字符串比较
//startdate和enddate的格式为:yyyy-MM-dd hh:mm:ss //当date1在date2之前时,返回1;当date1在date2之后时,返回-1:相等时,返回0 funct ...
- [转]SSIS包的调用方式
本文转自:http://www.cnblogs.com/lijun4017/archive/2008/12/04/1347701.html 编写简单SSIS包光看MSDN应该就问题不大了,最近几天几个 ...
- VLAN讲解
VLAN(Virtual Local Area Network)的中文名为"虚拟局域网".VLAN是一种将局域网设备从逻辑上划分成一个个网段,从而实现虚拟工作组的新兴数据交换技术. ...
- mysql重置密码和mysql error 1044(42000)错误
#mysql错误:(密码不正确,需要重置密码) ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor ...
- TensorFlow------TFRecords的分析与存储实例
TensorFlow------TFRecords的分析与存储实例: import os import tensorflow as tf # 定义cifar的数据等命令行参数 FLAGS = tf.a ...
- Web Services 指南之:Web Services 综述
在本系列指南中.我们学习了怎样使用 Web Services.可是一个 web service 还包含可以使它活跃的组件.诸如 WSDL.UDDI 以及 SOAP.接下来我们了解一下 WSDL.UDD ...
- java泛型介绍
一.泛型初衷 Java集合不会知道我们需要用它来保存什么类型的对象,所以他们把集合设计成能保存任何类型的对象,只要就具有很好的通用性.但这样做也带来两个问题: –集合对元素类型没有任何限制,这样可能引 ...