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. iis服务器php环境 failed to open stream: No such file or directory解决办法

    项目主机用的windows系统,iis服务器:远程连接桌面—>本地资源->映射D盘驱动器,将本地d盘修改后的文件放在远程主机项目目录里,访问报出failed to open stream: ...

  2. 如何在html中插入图片

    HTML内容元素中图片元素 使用img元素:src属性:图片路径. alt属性:图片无法显示的时候使用替代文本,title属性:鼠标悬停时显示文本内容. 在同一张图片上点击不同的位置链接到不同的页面上 ...

  3. 【2017年最新】 iOS面试题及答案

    设计模式是什么? 你知道哪些设计模式,并简要叙述? 设计模式是一种编码经验,就是用比较成熟的逻辑去处理某一种类型的事情. 1). MVC模式:Model View Control,把模型 视图 控制器 ...

  4. php中的引用

    $var1 = 'zhuchunyu'; $var2 = ""; function foo($vaa){ global $var1,$var2; if (!$vaa){ $var2 ...

  5. NoSQL数据库浅析

    NoSQL(NoSQL = Not Only SQL ):非关系型的数据库.NoSQL有时也称作Not Only SQL的缩写,是对不同于传统的关系型数据库的数据库管理系统的统称. 今天我们可以通过第 ...

  6. hibernate的报错信息a different object with the same identifier value was already associated with the session解决办法

    废话不多说,直接说原因,这是在hibernate中,有2个相同类型的实体类具有同样的主键标识符,然后调用update或者调用saveOrUpdate,我朋友出这个错的由于他想要update一条数据时, ...

  7. jQuery关于复选框的基本小功能

    这里是我初步学习jquery后中巨做的一个关于复选框的小功能: 点击右边选项如果勾上,对应的左边三个小项全部选中,反之全不选, 左边只要有一个没选中,右边大项就取消选中,反之左边全部选中的话,左边大项 ...

  8. jquery闭包概念

    //闭包:有参数的加载事件(空参数形式)(function($){ alert("123");})(jQuery); //有参数的加载事件(function($){ alert($ ...

  9. nodejs的安装配置

    Node.js安装及环境配置   一.安装环境 1.本机系统:Windows 10 Pro(64位)2.Node.js:v6.9.2LTS(64位) 二.安装Node.js步骤 1.下载对应你系统的N ...

  10. python新手学习之文件读写之修改

    文件除r.w.a方式打开外,还可以有多种组合方式如r+ w+ a+等多种方式 1.r+ 读写模式介绍,开始读是从一行开始读,写永远从最后开始写(类似于追加) # f = open("test ...