力扣刷题笔记。

数组

题号:485 最大连续一的个数

在这里插入图片描述
自己的解法:

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int num=0; int max = 0 ;
        for(int now : nums){
            if(now == 1){
                num ++;
                if(num > max){
                    max = num;
                }
            }else if(now == 0){
                num = 0;
            }
        }
        return max;
    }
}

官方答案解法:

class Solution {
  public int findMaxConsecutiveOnes(int[] nums) {
    int count = 0;
    int maxCount = 0;
    for(int i = 0; i < nums.length; i++) {
      if(nums[i] == 1) {
        // Increment the count of 1's by one.
        count += 1;
      } else {
        // Find the maximum till now.
        maxCount = Math.max(maxCount, count);
        // Reset count of 1.
        count = 0;
      }
    }
    return Math.max(maxCount, count);
  }
}

总结:要学会使用常用的一些API来简化代码。(改后代码变动不大不展示)
扩展: Math常用的一些API
PI圆周率 ,abs()取绝对值,ceil()向上取整, floor向下取整,round()四舍五入取整, max()取一组数字的最大值, min()取一组数字的最小值, pow(x,y)求x的y次幂, random()取随机数>=0 <1

题号:495 提莫攻击

在这里插入图片描述
在这里插入图片描述
自己的解法:

class Solution {
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        int time = 0; 
        if(timeSeries.length == 1){
            time = duration;
        }else if(timeSeries.length > 1){
             for(int i = 0; i < timeSeries.length-1 ; i++){
                 int now = timeSeries[i+1] - timeSeries[i];
                 if( now < duration){
                     time = time + now;
                 }else if(now >= duration){
                     time = time +duration;
                 }
             }
             time = time + duration;
        }
        return time ;
    }
}

官方的解法:

class Solution {
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        int n = timeSeries.length;
        if (n == 0) return 0;

        int total = 0;
        for(int i = 0; i < n - 1; ++i)
          total += Math.min(timeSeries[i + 1] - timeSeries[i], duration);
        return total + duration;
    }
}

总结:分析时考虑的长度为1的情况有些多余,应该考虑长度为0的情况,并且直接在为0情况的判断中返回会比在结尾返回更有效率一些。(你是猪嘛!!API啊)清除掉多余的if判断和变量声明。

题目:414 第三大的数

在这里插入图片描述
自己的解法:

class Solution {
    public int thirdMax(int[] nums) {
        Arrays.sort(nums);
        long MIN = Long.MIN_VALUE;
        long first = MIN; long sec = MIN; long end = MIN;
        if(nums.length < 3){
            return nums[nums.length - 1] ;
        }
        for(int i = nums.length -1 ; i>=0 ;i--){
            if(nums[i] != first && nums[i] != sec && nums[i] !=end){
                if(nums[i] > first){
                    end = sec;
                    sec = first;
                    first = nums[i];
                }else if (nums[i] > sec){
                    end = sec;
                    sec = nums[i];
                }else if (nums[i] >end){
                    end = nums[i];
                }
            }
        }
        if(end == MIN){
            return (int)first;
        }else{
            return (int)end ;
        }
    }
}

借鉴网友(happy_yuxuan)解法:
两种方法,其中一种和自己解法一样;

class Solution {
    public int thirdMax(int[] nums) {
        if (nums == null || nums.length == 0) throw new RuntimeException("error");

        TreeSet<Integer> set = new TreeSet<>();
        for (Integer elem : nums) {
            set.add(elem);
            if (set.size() > 3) set.remove(set.first());
        }
        return set.size() < 3 ? set.last() : set.first();   // set.last() 里面最大的元素
    }
}。

总结:根据这个场景可以使用TreeSet集合,因为它有不可重复和默认升序的特性,我们只要将set集合中的个数控制在三个就可以直接在数据存入集合后直接获取前三大的数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值