2017-3-4 leetcode 414 485 495
虽说周末要早起来着,但是日子过得有点奇怪,一不小心就忘掉了。。。
leetcode414 https://leetcode.com/problems/third-maximum-number/?tab=Description
leetcode485 https://leetcode.com/problems/max-consecutive-ones/?tab=Description
leetcode495 https://leetcode.com/problems/teemo-attacking/?tab=Description
======================================
414说的是
给你n个数字(无序,有重复),输出第三大的数,如果没有,输出最大的,使用O(n)的算法。注意,你的排序中不能有相同的,也就是说如果输入2,3,2,3,我们要输出3,因为没有第三大的数字。
我的思路
这题目,一下子就会想到堆priority_queue,但是堆是nlogn的,好吧,手动维护三个数字分别表示最大、次大、次次大好了,但是初始化设成什么值好呢,用0x80 memset一下好了,得到的是-1e9,感觉 够大了。
class Solution {
public:
int thirdMax(vector<int>& nums) {
int aim[];
int n=nums.size();
memset(aim,0x80,sizeof(aim));
for(int i=;i<n;i++){
if(nums[i]>aim[]){
aim[]=aim[];
aim[]=aim[];
aim[]=nums[i];
}
if(aim[]>nums[i]&&nums[i]>aim[]){
aim[]=aim[];
aim[]=nums[i];
}
if(aim[]>nums[i]&&nums[i]>aim[]){
aim[]=nums[i];
}
}
return aim[]==aim[]?aim[]:aim[];
}
};
WA
结果提交了一次,返回了WA,人家有数据是-2e9的。。。。好吧,那就写标记,用给的元素维护赋初值好了。
class Solution {
public:
void swap(int &x,int &y){
x^=y;y^=x;x^=y;
}
int thirdMax(vector<int>& nums) {
int aim[],flag=;
int n=nums.size();
memset(aim,0x80,sizeof(aim));
for(int i=;i<n;i++){
if(flag==){
aim[]=nums[i];
flag++;
continue;
}
if(flag==){
if(nums[i]==aim[])continue;
aim[]=nums[i];
if(aim[]<aim[])swap(aim[],aim[]);
flag++;
continue;
}
if(flag==){
if(nums[i]==aim[]||nums[i]==aim[])continue;
if(nums[i]>aim[]){
aim[]=aim[];
aim[]=aim[];
aim[]=nums[i];
}else if(aim[]>nums[i]&&nums[i]>aim[]){
aim[]=aim[];
aim[]=nums[i];
}else aim[]=nums[i];
flag++;
continue;
}
if(nums[i]>aim[]){
aim[]=aim[];
aim[]=aim[];
aim[]=nums[i];
}
if(aim[]>nums[i]&&nums[i]>aim[]){
aim[]=aim[];
aim[]=nums[i];
}
if(aim[]>nums[i]&&nums[i]>aim[]){
aim[]=nums[i];
}
}
return flag!=?aim[]:aim[];
}
};
AC
完成是完成了,这也太丑了吧。。。去学习学习别人的写法。结果发现了这个。。。。
int thirdMax(vector<int>& nums) {
set<int> top3;
for (int num : nums)
if (top3.insert(num).second && top3.size() > )
top3.erase(top3.begin());
return top3.size() == ? *top3.begin() : *top3.rbegin();
}
(很优美。。。。不想折叠),虽然set确实提供了logn的复杂度用来插入和删除,但是我们只需要前3大的数据,所以set的size只需要有3就好,log3算是常数.。。。。。。。心悦诚服
=======================================
485说的是
给你一个数组,只由01组成,问你最长的连续的1有多长
我的思路
没有思路,暴力出奇迹,扫一遍就好。
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int n=nums.size(),temp=,aim=;
for(int i=;i<n;i++){
if(nums[i])temp++;
else{
aim=max(aim,temp);
temp=;
}
}
aim=max(aim,temp);
return aim;
}
};
485
========================================
495说的是
有一个人的攻击可以让敌人中毒,毒会持续duration个时间单位,他会攻击很多次(不超过1e4),告诉你每次攻击的时间(非负,不超过1e7),问你敌人一共中毒中多久。
比如输入[1,2] 2 输出3
表示1秒2秒各攻击一次,每次中毒持续2秒,敌人一共中毒3秒
我的思路:
因为数据没有保证有序,先排序一下,然后扫一遍,记录每一个中毒区间的头尾,在出区间的时候统一计算时间加到答案里。复杂度O(nlogn)
class Solution {
public:
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
vector<int> nums(timeSeries);
int n=nums.size();
sort(nums.begin(),nums.end(),less<int>());
int aim=,st=-,et=-;
for(int i=;i<n;i++){
if(nums[i]>et){
aim+=et-st;
st=nums[i];
et=st+duration;
}else{
et=nums[i]+duration;
}
}
aim+=et-st;
return aim;
}
};
495
很不开心啊。。。发现居然(75ms)只击败了27.6%不开心啊,看了看讨论版,发现别人的代码好像没有排序,我去题面看了看,人家加粗了一个单词“ascending ”,我当时不明白,以为是攻击力会提升,,,,现在明白了,人家说的是升序给出攻击时间序列2333333
好吧,去掉sort后63ms,击败了67%。。。。就这样吧,毕竟评测机不稳定。。。
2017-3-4 leetcode 414 485 495的更多相关文章
- C#版 - Leetcode 414. Third Maximum Number题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 2017/11/22 Leetcode 日记
2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...
- 2017/11/21 Leetcode 日记
2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...
- 2017/11/13 Leetcode 日记
2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...
- 2017/11/20 Leetcode 日记
2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...
- 2017/11/9 Leetcode 日记
2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...
- 2017/11/7 Leetcode 日记
2017/11/7 Leetcode 日记 669. Trim a Binary Search Tree Given a binary search tree and the lowest and h ...
- 2017/11/6 Leetcode 日记
2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns ...
- 2017/11/5 Leetcode 日记
2017/11/5 Leetcode 日记 476. Number Complement Given a positive integer, output its complement number. ...
随机推荐
- 使用数组实现ArrayList的效果
package day04.d2.shuzu; /** * 通过数组实现类似于集合的功能 * 包含功能有: * * 动态添加元素 * 在指定位置添加元素 * * 删除指定下标的元素 * 删除指定内容的 ...
- MyEclipse设置默认注释的格式
首先选菜单windows-->preferenceJava-->Code Style-->Code Templates code-->new Java files 然后选中点编 ...
- Android 用Handler和Message实现计时效果及其中一些疑问
本来是打算继续做天气预报的优化的,但因为某些原因,我要先把之前做的小应用优化一下.所以今天就插播一下用Handler和Message实现计时效果. 首先,简要说明一下,这个应用有两个显示数字的Text ...
- 「Redis 笔记」常用命令
编号 命令 描述 1 DEL key 此命令删除一个指定键(如果存在). 2 DUMP key 此命令返回存储在指定键的值的序列化版本. 3 EXISTS key 此命令检查键是否存在. 4 EXPI ...
- Warning:关于_CRT_SECURE_NO_WARNINGS
Warning 1 warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s in ...
- 11.05 选择前n个记录
select ename,salfrom(select (select count(distinct b.sal)from emp bwhere a.sal<=b.sal) as rnk,a.s ...
- Robot Framework(四)创建测试套件
2.3.1测试用例文件 Robot Framework测试用例是使用测试用例文件中的测试用例表创建的.这样的文件会自动从它包含的所有测试用例中创建一个测试套件.可以有多少测试用例没有上限,但建议少于1 ...
- Java中 对象的创建于调用
Main方法是程序的主入口,想要用某个方法必须在main方法中调用 创建对象: 类名 对象名 = new 类名(); 使用对象访问类中的成员: 对象名.成员变量: 对象名.成员方法(); 成员变量的默 ...
- 路飞学城Python-Day107
88-Ajax简介 Ajax是前端的JS技术,目前向服务器发送请求是通过1.向浏览器的地址栏发送请求的方式:2.form表单的请求方式是两种get和post方式:3.a标签的href属性对接地址 是一 ...
- 树(6)-----DFS
1.二叉树的反向层次遍历 def levelOrderBottom1(self, root): res = [] self.dfs(root, 0, res) return res def dfs(s ...