leetcode485、448、414

例如414,用java做的话好多坑,以后就用java刷leetcode了。

//414
public class Solution {
    public int thirdMax(int[] nums) {
        Integer max1 = null; //不能使用Integer.MIN_VALUE,因为题目里会有Integer.MIN_VALUE作输入
        Integer max2 = null; //使用null时不能使用基本类型,因为null不能被自动装箱为基本类型
        Integer max3 = null;
        for(Integer n : nums) { //对象之间的想等判断,所以使用Integer来做循环
            if(n.equals(max1) || n.equals(max2) || n.equals(max3)) { //对象之间的相等用equals来做判断
                continue;
            }
            if(max1==null || n > max1) {
                max3 = max2;
                max2 = max1;
                max1 = n;
            } else if(max2 == null || n > max2) {
                max3 = max2;
                max2 = n;
            } else if(max3 == null || n > max3) {
                max3 = n;
            }
        }
        return max3 == null ? max1 : max3;
    }
}
//485
class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
        int size = nums.size();
        int flag = 1;
        int count = 0;
        int max = INT_MIN;
        for(int i=0; i<size;) {
            if(nums[i] != flag) { //如果flag与当前值不同
                count = 0;
                i++; //递增
            }
            while(nums[i] == flag) {
                count++;
                i++;
            }
            if(max < count)
                max = count;
        }
        return max;
    }
};
//448
class Solution {
public:
    vector<int> findDisappearedNumbers(vector<int>& nums) {

        for(int i=0; i<nums.size(); i++) {
            int m = abs(nums[i]) - 1; //index start from 0
            nums[m] = nums[m] > 0 ? -nums[m] : nums[m];
        }

        vector<int> rst;
        for(int i=0; i<nums.size(); i++) {
            if(nums[i] > 0) rst.push_back(i+1);
        }
        return rst;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值