414. Third Maximum Number

Given an array of integers, return the 3rd Maximum Number in this array, if it doesn't exist, return the Maximum Number. The time complexity must be O(n) or less.

思路:设置n1=第3大的数,n2=第2大的数,n3=最大数,最后返回n1。

 int thirdMax(vector<int>& nums) {int n=nums.size();if(!n) return 0;if(n==1) return nums[0];else if(n==2) return max(nums[0],nums[1]);int n1=min(nums[0],min(nums[1],nums[2])),n2=max(nums[0],min(nums[1],nums[2])),n3=max(nums[0],max(nums[1],nums[2]));for(int i=3;i<n;i++){if(nums[i]>n3) {n1=n2;n2=n3;n3=nums[i];}else if(nums[i]>=n2) {n2=nums[i];}else if(nums[i]>n1)n1=nums[i];}return n1;  }

414. Third Maximum Number相关推荐

  1. LeetCode 414. Third Maximum Number

    题目: Given a non-empty array of integers, return the third maximum number in this array. If it does n ...

  2. LeetCode | 414. Third Maximum Number

    . 题目 Given an integer array nums, return the third distinct maximum number in this array. If the thi ...

  3. openstack创建实例报错Exceeded maximum number of retries

    Error: 实例 "vm2" 执行所请求操作失败,实例处于错误状态.: 请稍后再试 [错误: Exceeded maximum number of retries. Exceed ...

  4. Oracle 数据库设置最大进程数参数方法,oracle最大进程数满了处理方法,sysdba管理员登录报“maximum number of processes (150) exceeded“问题解决

    oracle 数据库使用 sysdba 管理员登录报: ORA-00020: maximum number of processes (150) exceeded 译:超过了最大进程数(150) 方法 ...

  5. Linux Increase The Maximum Number Of Open Files / File Descriptors (FD)

    http://www.cyberciti.biz/faq/linux-increase-the-maximum-number-of-open-files/ How do I increase the ...

  6. WCF:Maximum number of items that can be serialized or deserialized in an object graph is '65536'.

    错误 Maximum number of items that can be serialized or deserialized in an object graph is '65536'. Cha ...

  7. leetcode 330. Patching Array | 1798. Maximum Number of Consecutive Values You Can Make

    1798. Maximum Number of Consecutive Values You Can Make | 1798. 你能构造出连续值的最大数目 https://leetcode.com/p ...

  8. 321. Create Maximum Number 解题方法详解

    321. Create Maximum Number 题目描述 Given two arrays of length m and n with digits 0-9 representing two ...

  9. The number of requested virtual cores per node 3 exceeds the maximum number of virtual cores 2

    报错如下: yarn-session.sh  -tm 2048 -s 3 2020-06-08 22:24:20,317 WARN org.apache.flink.yarn.cli.FlinkYar ...

最新文章

  1. 微软亚洲研究院:“网络结构搜索和设计”论文分享专场
  2. python好学吗 小木虫-25行Python代码完成人脸识别
  3. Java为什么我们现在有时不用环境变量就可以运行了
  4. An unspecified error occurred!
  5. 剑指Offer - 面试题3. 数组中重复的数字(哈希)
  6. hive随机抽取100条数据_用Excel进行随机抽取数据
  7. C Primer Plus 第13章 文件输入/输出 13.11 编程练习答案
  8. python怎么七个数字一换行_python中怎么换行?
  9. jQuery操作DOM对象
  10. IE9兼容性视图与IE9标准视图
  11. pathlib2 Path glob rglob的最新研究成果
  12. AWS吹走了私有云天空中最后一片乌云
  13. linux重启搜狗输入法命令
  14. 计算机系统 ahci模式,老电脑安装系统忘记开启AHCI模式,教你不重装系统开启AHCI解决方法(3)...
  15. pycharm中的py版本与coda3自带版本不统一
  16. 解决msvcp120d.dll和msvcr120d.dll缺失
  17. android中计算机源代码,Android源代码查看途径
  18. 代码随想录训练营day38
  19. 戴尔服务器r510装系统,戴尔R510服务器网卡驱动安装步骤.doc
  20. CUDA kernel函数不执行、不报错的问题

热门文章

  1. 【安卓开源集合】最全最有用的第三方开源库收集整理,快速开发必备,还能提升效率
  2. Pytorch各种取样器sample
  3. java反射set_java反射
  4. python创意游戏-Python制作十款经典的童年游戏(附源码)
  5. 苹果手机文件连接服务器显示离线,查找iphone一直显示30分钟前的旧位置,是代表设备短暂接入后又离线了吗?...
  6. nativeexcel使用,Delphi 开发解决方案
  7. SVN之初恋与小三Meld优秀分支合并工具
  8. 如何用Java程序删除sql脚本中的注释
  9. 更改计算机ram,如何向计算机添加RAM
  10. Linux下制作Windows启动U盘的工具