【力扣hot100】day4

文章介绍了使用回溯算法解决组合总和、接雨水和全排列问题,以及利用哈希表解决字母异位词分组的策略。对于每个问题,都提供了执行效率较高的代码实现,包括排序、条件判断和递归等关键步骤。
摘要由CSDN通过智能技术生成

39.组合总和【中】

39. 组合总和
方法一:回溯

//执行用时: 1 ms
//内存消耗: 41.9 MB
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates); //排序
        backTracing(candidates, target, 0, 0);
        return result;
    }

    public void backTracing(int[] candidates, int target, int sum, int index) {
        if(sum == target) {
            result.add(new ArrayList<>(path));
            return ;
        }
        for(int i = index; i < candidates.length; i++) {
            if (sum + candidates[i] > target) break;//这一步差一些就忽略了!!!
            path.add(candidates[i]);
            backTracing(candidates, target, sum + candidates[i], i);
            path.remove(path.size() - 1);
        }
    }
}

42.接雨水【难】

42. 接雨水

46.全排列【中】

46. 全排列
方法一:回溯

//执行用时: 1 ms
//内存消耗: 41.8 MB
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> permute(int[] nums) {
        backTracing(nums, 0);
        return result;
    }

    public void backTracing(int[] nums, int len) {
        if(path.size() == nums.length) {
            result.add(new ArrayList<>(path));
            return ;
        }
        for(int i = 0; i < nums.length; i++) {
            if(path.contains(nums[i])) continue ; //这一步挺重要的!!!
            path.add(nums[i]);
            backTracing(nums, i + 1);
            path.remove(path.size() - 1);
        }
    }
}

48.旋转图像【中】

48. 旋转图像

49.字母异位词分组【中】

49. 字母异位词分组
方法一:哈希表

//执行用时: 6 ms
//内存消耗: 44.3 MB
class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
       //判断是否为空字符串数组
        if(strs == null || strs.length == 0){
            return new ArrayList();
        }
        //1.创建一个哈希表
        Map<String,List> map = new HashMap<String, List>();
        for (String s: strs) {
            //将字符串转化为字符数组
            char[] chars = s.toCharArray();
            //对字符数组按照字母顺序排序
            Arrays.sort(chars);
            //将排序后的字符串作为哈希表中的key值
            String key = String.valueOf(chars);
            //2.判读哈希表中是否有该key值
            if (!map.containsKey(key)){
                //若不存在,则为新的异位词语,在map中创建新的键值对
                map.put(key,new ArrayList());
            }
            //3.将该字符串放在对应key的list中
            map.get(key).add(s);
        }
        //返回map中所有键值对象构成的list
        return new ArrayList(map.values());
    }
}
### 关于 LeetCode Hot 100 题目刷题策略 #### 制定学习计划 为了高效完成 LeetCode Hot 100 的练习,制定合理的学习计划至关重要。可以根据不同类型的题目来安排每天的任务量,比如先集中攻克某一类问题如数组操作或是链表处理等[^1]。 #### 掌握基础数据结构与算法概念 深入理解和掌握基本的数据结构(例如 数组、链表、哈希表 和 字符串)以及常用算法(例如 双指针法、栈与队列的应用、二叉树遍历),这有助于更轻松地解决复杂度较高的挑战性问题。 #### 注重实践并反复练习 对于每一种类型的问题都应多做几次类似的实例,尤其是那些被标记为困难级别的习题;通过不断重复加深记忆,并尝试不同的解法提高灵活性和创造性思维能力[^2]。 #### 学会总结归纳 每次做完一道新题之后都要认真思考其背后的原理是什么?有没有更好的方法去实现它呢?将遇到过的难题整理成笔记形式保存下来以便日后查阅复习之用。 ```python def solve_problem(problem_id, solution_approach): """ A function to simulate the process of solving a problem with given approach. Args: problem_id (int): The ID or index number associated with each specific coding challenge on platforms like LeetCode. solution_approach (str): Description about how this particular issue will be tackled including any relevant algorithms used. Returns: str: Confirmation message indicating successful completion along with brief explanation regarding chosen methodology applied during resolution phase. Example Usage: >>> print(solve_problem(55,"Using Greedy Algorithm")) Problem No.55 solved using greedy algorithm which ensures optimal choice at every step leading towards overall best outcome possible within constraints provided by question statement itself. """ return f"Problem No.{problem_id} solved {solution_approach}." ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值