【leetcode 100】贪心Java版本

发布于:2025-04-09 ⋅ 阅读:(37) ⋅ 点赞:(0)

划分字母区间

题目
我的思路:第一次没有一点思路,第二次看了官网思路后,写的以下答案,没有搞明白循环遍历,
//是不对的以下:

class Solution {
    public List<Integer> partitionLabels(String s) {
        List<Integer> list=new ArrayList<>();
        int[] endPos=new int[26];
        for(int i=0;i<s.length();i++)
        {
            endPos[s.charAt(i)-'a']=i;
        }
        int end=0,start=0;
        while(start!=s.length()-1)
        {
        for(int i=start;i<=end;i++)
        {
            end=Math.max(end,endPos[i]);//这样结束的不对!,不跳出来怎么能加到list结果里呢
            
        }
        list.add(end-start+1);
        start=end+1;
        }
        return list;
        
    }
}

官方答案:

class Solution {
    public List<Integer> partitionLabels(String s) {
        int[] last = new int[26];
        int length = s.length();
        for (int i = 0; i < length; i++) {
            last[s.charAt(i) - 'a'] = i;
        }
        List<Integer> partition = new ArrayList<Integer>();
        int start = 0, end = 0;
        for (int i = 0; i < length; i++) {
            end = Math.max(end, last[s.charAt(i) - 'a']);
            if (i == end) {
                partition.add(end - start + 1);
                start = end + 1;
            }
        }
        return partition;
    }
}

作者:力扣官方题解
链接:https://leetcode.cn/problems/partition-labels/solutions/455703/hua-fen-zi-mu-qu-jian-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

55. 跳跃游戏

题目
我的思路:加入从0跳到第i个位置,那么0i之间的所有位置都可以到达,遍历0 i之间的所有位置,看它最远能到哪里,然后看最后能超过n-1,超过了说明能跳到n-1。但是又是很模糊的感觉,不知道具体怎么去“遍历”。
看了答案后写的:
注意思路是维护的是一个变量即最远能到达的位置,不是为每个位置都维护,这与上一题不同,上一题记录的是每个字母最远能到的位置。

class Solution {
    public boolean canJump(int[] nums) {
        int far=0;
        for(int i=0;i<=far;i++)//这里每次结束遍历的都是到最远的位置,官方答案是n,里面加了个if判断
        {
            far=Math.max(far,i+nums[i]);
            if(far>=nums.length-1)
                return true;
        }
        return false;
    }
}

45. 跳跃游戏II

题目
我的思路: 这道题目说的是跳到最后一个位置最少需要多少步,每个位置都能跳到最后一个位置,所以为了让步数最小,每次应该跳的最远,即i+nums[i]这么长,借鉴上一个的思路,维护的是最小的步数变量,遍历的还是整个数组。后来写出来不对,感觉可以用动态规划来做?
我写的有问题:超出时间限制(待解决)


//错误!!!!!
class Solution {
    public int jump(int[] nums) {
        int n=nums.length;
        int res=Integer.MAX_VALUE;
        for(int i=0;i<n;i++)
        {
            int count=0;
            for(int j=i;j<n;)
            {
                j=j+nums[j];      
                count++;         
            }
             res=Math.min(res,count+1); //最小是一步?
        }
        return res;
    }
}

官方答案思路:
方法1:倒着往前推。此处贪心的思路是:有多个可以跳到最后一个位置的情况时,也就是对应下标最小的那个。

class Solution {
    public int jump(int[] nums) {
        int position=nums.length-1;
        int step=0;
        while(position>0) //等于0就没必要跳了
        {
            for(int i=0;i<position;i++)
            {
                if(i+nums[i]>=position)
                {
                    position=i;
                    step++;
                    break;
                }
            }

        }
        
        return step;
    }
}

方法2:正着往前推(待解决)


网站公告

今日签到

点亮在社区的每一天
去签到