力扣打卡(10):国庆躺平乐~~~ 写个每日抑题

18 篇文章 0 订阅
16 篇文章 0 订阅
博主在国庆躺平的日子里坚持LeetCode每日一题,分享了解决27. 移除元素、482. 密钥格式化和45. 跳跃游戏 II的思路,其中涉及双指针和贪心算法的应用。
摘要由CSDN通过智能技术生成

10.4lc

今天是躺平的一天~~~
在游乐园玩了一天~~

玩了一天 写了个每日一题: 看了几个二分

482. 密钥格式化 - 力扣(LeetCode) (leetcode-cn.com)

class Solution {
    public String licenseKeyFormatting(String s, int k) {
 StringBuffer sb = new StringBuffer(s.replaceAll("-", "").toUpperCase());
        //从后往前遍历 进行 k的替换
        for (int i = sb.length() - k; i > 0; i -= k) {
            sb.insert(i, '-');
        }
        return sb.toString();
    }
}

27. 移除元素 - 力扣(LeetCode) (leetcode-cn.com)

//双指针

class Solution {
    public int removeElement(int[] nums, int val) {
       int i=0,j=0;
       while(i<nums.length){
           if(nums[i] != val){
               nums[j] =nums[i];
               j++;
           }
         i++;

       }
       return j;
    }

}

45. 跳跃游戏 II - 力扣(LeetCode) (leetcode-cn.com)

:思路:

每次都在可行的 nums[i] 的范围内 去找到每个遍历的最大的

贪心

class Solution {
    public int jump(int[] nums) {
     
       int res=0,start=0,end=1;
       int len= nums.length;
       while (end < len){
           int maxPos=0;
           for (int i = start; i <end ; i++) {
                maxPos=Math.max(maxPos,i+nums[i]);
           }
           start=end;
           end=maxPos+1;
           res++;
       }
       return  res;
    }
    
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值