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: ...
随机推荐
- 如何遍历Map操作总结
Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "123"); ...
- 如何在IAR工程中创建和使用模板
路径为:Edit -> Code Templates -> Edit Templates 如下图: #TEMPLATE "&FileDeclare>&Fi ...
- js判断中文字符串长度和js截取中文字符串
//获取字符串长度String.prototype.strLen = function() { var len = 0; for (var i = 0; i < this.length; i++ ...
- Process.waitFor()导致主线程堵塞问题
今日开发的时候使用jdk自带的运行时变量 RunTime.getRunTime() 去执行bash命令.因为该bash操作耗时比较长,所以使用了Process.waitFor()去等待子线程运行结束. ...
- 第三天-零基础学习python
1.回忆.列表方法,append(),extend(),insert() 2.列表获取元素: >>> member = ['HU','YU','HUYAN','HUJIAMU'] ...
- CentOS7安装.Net Core2.2
一.安装.Dotnet Core 2.2 Runtime Linux上运行Dotnet Core程序的前提是安装Dotnet Core Runtime .Net Core对不同的Linux版本提示了 ...
- JQuery制作网页—— 第六章 jQuery选择器
1.jQuery选择器:jQuery选择器类似于CSS选择器,用来选取网页中的元素. Eg:$("h3").css("background",&qu ...
- 如何给ioloop.run_sync()中调用的函数传入参数
问题 如何给tornado.ioloop.IOLoop中的run_sync方法中调用的函数添加参数 解决方案 使用functools.partial 解决示例 from tornado import ...
- yii学习笔记(7),数据库操作,联表查询
在实际开发中,联表查询是很常见的,yii提供联表查询的方式 关系型数据表:一对一关系,一对多关系 实例: 文章表和文章分类表 一个文章对应一个分类 一个分类可以对应多个文章 文章表:article 文 ...
- 关于一个flask的服务接口实战(flask-migrate,flask-script,SQLAlchemy)
前言 最近接到一个接收前端请求的需求,需要使用python编写,之前没有写过python,很多技术没有用过,在这里做一个学习记录,如有错误,请不了赐教. Flask Api文档管理 使用Falsk A ...