lintcode735. Replace With Greatest From Right
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的更多相关文章
- 735. Replace With Greatest From Right【medium】
Given an array of integers, replace every element with the next greatest element (greatest element o ...
- <JavaScript语言精粹>--<读书笔记三>之replace()与正则
今天有人问我repalce(),他那个题目很有意思.我也不会做,于是我就去查,结果发现就是最基础的知识的延伸. 所以啊最基础的知识才是很重要的,千万不能忽略,抓起JS就写代码完全不知到所以然,只知道写 ...
- 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 ...
- js的replace函数入参为function时的疑问
近期在写js导出excel文件时运用到replace方法,此处详细的记录下它各个参数所代表的的意义. 定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式 ...
- ORACLE 利用 REPLACE函数替换字段字符串
REPLACE(string,s1,s2) string 希望被替换的字符或变量 s1 被替换的字符串 s2 要替换的字符串 SQL> select replace(he love you,he ...
- js 页面刷新location.reload和location.replace的区别小结
reload 方法,该方法强迫浏览器刷新当前页面. 语法: location.reload([bForceGet]) 参数: bForceGet, 可选参数, 默认为 false,从客户端缓存里取当前 ...
- replace和translate的用法
select replace ('111222333444','222','888') from dual;with tmp as(select 'aabb/123\:cde工人' s from du ...
- JavaScript replace() 方法
参考:http://www.w3school.com.cn/jsref/jsref_replace.asp 需要有一点注意的是:可以是函数的形式做为返回值,如下: "test{0}" ...
- replace实现正则过滤替换非法字符
html+js结构如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...
随机推荐
- [转]order by 1是什么意思?
ORDER BY 1 表示 所select 的字段按第一个字段排序 ORDER BY ASC应该没有这样写法,ORDER BY 后面不是字段就是数字, 可以ORDER BY 1 ASC 或者ORDER ...
- 手机站全局的html+css加载等待效果
本文只提供思路,CSS神马的自己定制吧,JS是可以优化的,比如,输出图片的JS也可以放到showdiv()里面,我没有做优化,只是实现,别笑话我,我比较懒... 基本思路:由于Html的解析是从上到下 ...
- 基于VC++的WinCE网口通信
基于VC++的WinCE网口通信 WinCE下的网络编程与Windows下的非常类似,只是个别API函数有所不同.同样分为UDP和TCP两种,UDP就是无连接的通信,通过“用户数据报协议”(UDP)来 ...
- jquery固定位置浮动
示例: <!DOCTYPE html> <html> <head> <title>test page</title> <script ...
- CSS Variables
CSS原生变量(CSS自定义属性) 示例地址:https://github.com/ccyinghua/Css-Variables 一.css原生变量的基础用法 变量声明使用两根连词线"-- ...
- ssm调用后台oracle存储过程统计分析数据
笔者所在项目需要一个统计本机构近6月收入情况(分两种).本机构下级机构收入情况的需求,数据量为百万级. 具体需求是时间.机构都不确定,可为入参. 综合考虑后决定使用后台存储过程统计. 基础表结构如下: ...
- #leetcode刷题之路7- 整数反转
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1:输入: 123输出: 321 示例 2:输入: -123输出: -321 示例 3:输入: 120输出: 21 #i ...
- php源码建博客4--实现MVC结构微型框架
主要: 常量优化路径 自动加载类 优化入口文件 安全访问项目目录 --------------文件结构:-------------------------------------- blog├─App ...
- Android编译命令
目录 说在前面 编译流程 编译指令 代码编译 代码检索 其他指令 说在前面 从最开始接触Android系统开始,每次进行代码编译都需要网上搜索编译指令.后来大致熟悉了Android的编译体系,加深了对 ...
- C语言思维导图总结