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].

最初的思路代码(超时)

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

从后往前的方法:

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

目前一直是79%数据通过。。。一直没有到最后结果。可能是网络有问题,晚上再试。

lintcode735. Replace With Greatest From Right的更多相关文章

  1. 735. Replace With Greatest From Right【medium】

    Given an array of integers, replace every element with the next greatest element (greatest element o ...

  2. <JavaScript语言精粹>--<读书笔记三>之replace()与正则

    今天有人问我repalce(),他那个题目很有意思.我也不会做,于是我就去查,结果发现就是最基础的知识的延伸. 所以啊最基础的知识才是很重要的,千万不能忽略,抓起JS就写代码完全不知到所以然,只知道写 ...

  3. StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

    StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...

  4. js的replace函数入参为function时的疑问

    近期在写js导出excel文件时运用到replace方法,此处详细的记录下它各个参数所代表的的意义. 定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式 ...

  5. ORACLE 利用 REPLACE函数替换字段字符串

    REPLACE(string,s1,s2) string 希望被替换的字符或变量 s1 被替换的字符串 s2 要替换的字符串 SQL> select replace(he love you,he ...

  6. js 页面刷新location.reload和location.replace的区别小结

    reload 方法,该方法强迫浏览器刷新当前页面. 语法: location.reload([bForceGet]) 参数: bForceGet, 可选参数, 默认为 false,从客户端缓存里取当前 ...

  7. replace和translate的用法

    select replace ('111222333444','222','888') from dual;with tmp as(select 'aabb/123\:cde工人' s from du ...

  8. JavaScript replace() 方法

    参考:http://www.w3school.com.cn/jsref/jsref_replace.asp 需要有一点注意的是:可以是函数的形式做为返回值,如下: "test{0}" ...

  9. replace实现正则过滤替换非法字符

    html+js结构如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...

随机推荐

  1. HDU 2048 神、上帝以及老天爷(错排概率问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2048 神.上帝以及老天爷 Time Limit: 2000/1000 MS (Java/Others) ...

  2. C# 对WinForm应用程序的App.config的加密

    默认情况下,我们需要对App.config文件里的connectionStrings片断进行加密处理,ASP.NET IIS 注册工具 (Aspnet_regiis.exe)可以胜任这个工作,但这个工 ...

  3. easy-im:一款基于netty的即时通讯系统

    介绍 easy-im是面向开发者的一款轻量级.开箱即用的即时通讯系统,帮助开发者快速搭建消息推送等功能. 基于easy-im,你可以快速实现以下功能: + 聊天软件 + IoT消息推送 基本用法 项目 ...

  4. CSS之元素

    CSSS书写位置 内嵌式 <head> <style type = "text/css"> **** </style> </head> ...

  5. DB数据源之SpringBoot+MyBatis踏坑过程(五)手动使用Hikari连接池

    DB数据源之SpringBoot+MyBatis踏坑过程(五)手动使用Hikari连接池 liuyuhang原创,未经允许禁止转载  系列目录连接 DB数据源之SpringBoot+Mybatis踏坑 ...

  6. 用 crontab 实现开机自动运行脚本

    开发「bufpay.com 个人即时到账收款平台」的时候,订单状态和支付二维码的状态如果过期了要实时修改状态,最大效率利用支付二维码. 过期脚本需要开机启动,并且 deamon 运行,有很多办法可以开 ...

  7. PHP中级程序员常见面试题

    1).写一个函数,从一个标准url里取出文件的扩展名,需要取出php或.php <?php $a="http://www.test.com.cn:88/abc/de/fg.php?id ...

  8. vue的实例

    vue的实例 创建一个vue实例的写法和创建一个变量一样 var vm = new Vue{{ //我们一般用vm来接收vue的实例,vm是 ViewModel的缩写 }} 然后,我们就可以给vue实 ...

  9. 关于python的菜鸟教程

    1.正则表达式 http://www.runoob.com/regexp/regexp-metachar.html 2.python正则表达死 http://www.runoob.com/python ...

  10. sublime解决gbk中文乱码包括Package Control: Install Package 无法使用

    最近喜欢上了sublime,打算抛弃notepad,但是发现sublime居然不支持gbk编码,再上网查找资料之后,总结了一套解决方法,目前为止是行之有效的. 日期:2019年3月14日 第一步:到G ...