leetcode
来源:互联网 发布:淮南安广网络营业厅 编辑:程序博客网 时间:2024/06/11 04:15
原题:
414. Third Maximum Number
DescriptionHintsSubmissionsDiscussSolution
DiscussPick One
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).
Example 1:
Input: [3, 2, 1]Output: 1Explanation: The third maximum is 1.
Example 2:
Input: [1, 2]Output: 2Explanation: The third maximum does not exist, so the maximum (2) is returned instead.
Example 3:
Input: [2, 2, 3, 1]Output: 1Explanation: Note that the third maximum here means the third maximum distinct number.Both numbers with value 2 are both considered as second maximum.
代码如下:
int thirdMax(int* nums, int numsSize) { long int first=INT_MIN; long int second=INT_MIN; long int third=INT_MIN; for(int n=0;n<numsSize;n++) { if(*(nums+n)>first) { third=second; second=first; first=*(nums+n); } else { if(*(nums+n)>second&&*(nums+n)!=first) { third=second; second=*(nums+n); } else { if(*(nums+n)>third&&*(nums+n)!=second&&*(nums+n)!=first) { third=*(nums+n); } } } } if(third==INT_MIN) return first; return third;}
这个leetcode对c真的是特别的不友好。算法很简单,这个平台对数据的处理太不友好了。这个并不能提交,因为在极值上总是出问题。
阅读全文
0 0
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- 数据库(第一范式,第二范式,第三范式)
- Golang学习之常见错误
- 控制WinForm中右键菜单
- spring 中如果bean没有id或name标志 获取bean的方法
- 简单工厂模式
- leetcode
- Python pass 语句
- 第十一篇: JAVA加密解密之Base64
- 判断浏览器是pc端还是手机端
- 一只青蛙一次可以跳上1级台阶,也可以跳上2级,也可以跳3级。求该青蛙跳上一个n级的台阶总共有多少种跳法,并且打印所有跳跃路径。
- jquery动态添加元素无法触发绑定的事件的解决方案
- [转]17个新手常见的Python运行时错误
- 数字图像处理基础:教你如何区分单色图像、灰度图像、伪彩色图像、真彩色图像
- 下拉框年月日关联实现: