leet code - Third Maximum Number
来源:互联网 发布:一键获取淘宝联盟 编辑:程序博客网 时间:2024/06/05 02:54
题目描述
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).
返回数组中第三大的数字,可能会包含重复。
思路:
直接遍历过程中存最大值求出结果。
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).
返回数组中第三大的数字,可能会包含重复。
思路:
直接遍历过程中存最大值求出结果。
public class Solution { public int ThirdMax(int[] nums) {if(nums == null || nums.Length == 0){throw new ArgumentException();}if(nums.Length == 1){return nums[0];}if(nums.Length == 2){return Math.Max(nums[0],nums[1]);}int max = nums[0];for(var i = 1;i < nums.Length; i++){if(nums[i] > max){max = nums[i];}}int? second = null;for(var i = 0;i < nums.Length; i++){if(nums[i] < max && (second == null || nums[i] > second)){second = nums[i];}}if(!second.HasValue){return max;}int? third = null;for(var i = 0;i < nums.Length; i++){if(nums[i] < second && (third == null || nums[i] > third)){third = nums[i];}}if(third.HasValue){return third.Value;}else{return max;}}}
1 0
- leet code - Third Maximum Number
- [leet code] Maximum Subarray
- Leet code Maximum Gap
- 414. Third Maximum Number
- LeetCode Third Maximum Number
- Leetcode414. Third Maximum Number
- 414. Third Maximum Number
- 414. Third Maximum Number
- LeetCode-Third Maximum Number
- leetcode414. Third Maximum Number
- 414. Third Maximum Number
- 414. Third Maximum Number
- Third Maximum Number
- 414. Third Maximum Number
- LeetCode414 Third Maximum Number
- 414. Third Maximum Number
- 414. Third Maximum Number
- 414. Third Maximum Number
- python SQLite3 插入数据问题
- usaco Combination Lock
- 换硬币问题
- python学习笔记 filter和sorted
- 数据库(3):MySQL优化GROUP BY-松散索引扫描与紧凑索引扫描
- leet code - Third Maximum Number
- hdu 2065 指数型生成函数
- html5笔记之classList
- [整理]VC编译程序时出现the source files are both configured to produce the output file....
- codeforces732E Sockets(贪心)
- C++虚函数与纯虚函数
- 重建二叉树
- 关于浮动知识的总结
- C++ try catch的问题