虽说周末要早起来着,但是日子过得有点奇怪,一不小心就忘掉了。。。

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的更多相关文章

  1. C#版 - Leetcode 414. Third Maximum Number题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  2. 2017/11/22 Leetcode 日记

    2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...

  3. 2017/11/21 Leetcode 日记

    2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...

  4. 2017/11/13 Leetcode 日记

    2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...

  5. 2017/11/20 Leetcode 日记

    2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...

  6. 2017/11/9 Leetcode 日记

    2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...

  7. 2017/11/7 Leetcode 日记

    2017/11/7 Leetcode 日记 669. Trim a Binary Search Tree Given a binary search tree and the lowest and h ...

  8. 2017/11/6 Leetcode 日记

    2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns ...

  9. 2017/11/5 Leetcode 日记

    2017/11/5 Leetcode 日记 476. Number Complement Given a positive integer, output its complement number. ...

随机推荐

  1. Centos 6 搭建邮箱服务器教程

    Centos 6 搭建邮箱服务器主要是是包括了Postfix, Dovecot和 MySQL了,下文我们详细的为各位介绍Centos 6 搭建邮箱服务器教程(Postfix, Dovecot和 MyS ...

  2. C++数字图像处理(1)-伽马变换

    https://blog.csdn.net/huqiang_823/article/details/80767019 1.算法原理    伽马变换(幂律变换)是常用的灰度变换,是一种简单的图像增强算法 ...

  3. KAFKA 调优

    KAFKA 调优 最近要对kafka集群做调优,就在网上看了些资料,总结如下. 我们的kafka版本是0.10.1.0. 机器配置是40G内存,300G硬盘. 一共有3台机器组成一个小的集群. Kak ...

  4. WordPress瀑布流主题PinThis中文版v1.6.8

    PinThis主题来源于英语网站http://pinthis.pixelbeautify.com/的汉化(语言文件+控制面板),中文版采用的是翻译器手工核对,并不完美,只对主题中文化,其他没做任何更改 ...

  5. Android入门知识

    1.Android开发环境 Android常用的开发环境包括两个:Eclipse + ADT 和Android Studio,Android Studio作为google官方推出的开发环境自然有得天独 ...

  6. Teamwork-六月上旬心得体会

    六月上旬心得体会 在五月末的时候,老师针对我们团队的状况提出了几点建议和解决方案,而这半个月里,我们尝试性地运用了其中的几件工具与方法. 1.燃尽图与每日总结 我们采用的是<构建之法>书中 ...

  7. win 7环境下java环境变量的配置

    http://www.cnblogs.com/zhj5chengfeng/archive/2013/01/01/2841253.html %Java_Home%\bin;%Java_Home%\jre ...

  8. codeforces 468B two set(并查集)

    链接 B. Two Sets 题意 给两个集合A B,两个数a b,n个数x,分配n个数到两个集合,要求x , a-x在同一个集合,x , b-x在同一个集合,属于A集合的数输出0,B的输出1,无解输 ...

  9. 打包c++项目

    InstallShield Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序) InstallShield 2015 Limited E ...

  10. IOS - NSDate 自己挖的坑,自己跳

    NSDate:5是坑啊啊! NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDat ...