• 494.力扣每日一题6/30 Java(三种解法)


    • 博客主页:音符犹如代码
    • 系列专栏:算法练习
    • 关注博主,后期持续更新系列文章
    • 如果有错误感谢请大家批评指出,及时修改
    • 感谢大家点赞👍收藏⭐评论✍

    目录

    思路

    解题方法

    时间复杂度

    空间复杂度

    Code


    解法一:递归

    思路

    通过递归的方式遍历数组中的每个数字,对于每个数字,都有加上或减去两种选择。在递归过程中,当遍历完数组时,判断当前的总和是否等于目标值,如果相等则表示找到了一种满足条件的表达式组合。

    解题方法

    使用了深度优先搜索(DFS)的思想,通过递归地探索所有可能的符号组合来计算满足目标值的表达式数量。

    时间复杂度

    O(n²)

    空间复杂度

    O(n)

    Code

    1. import java.util.Arrays;
    2. public class Solution {
    3. public int findTargetSumWays(int[] nums, int target) {
    4. return helper(nums, 0, target, 0);
    5. }
    6. public int helper(int[] nums, int index, int target, int currentSum) {
    7. if (index == nums.length) {
    8. if (currentSum == target) {
    9. return 1;
    10. }
    11. return 0;
    12. }
    13. int count = 0;
    14. count += helper(nums, index + 1, target, currentSum + nums[index]);
    15. count += helper(nums, index + 1, target, currentSum - nums[index]);
    16. return count;
    17. }
    18. }

    解法二:回溯法

    思路

    使用回溯法,通过递归地尝试对数组中的每个数字进行加和减的操作,来穷举所有可能的表达式组合。

    解题方法

    定义一个回溯函数backtrack,函数参数包括数组nums、当前处理的数字索引index、当前的总和currentSum以及目标值target。当index达到数组长度时,检查currentSum是否等于target,若相等则增加计数。否则,对于当前数字,分别进行加上和减去的操作,并递归调用自身处理下一个数字。

    时间复杂度

    𝑂(2*𝑛)

    空间复杂度

    O(n)

    Code

    1. public class Solution {
    2. public int findTargetSumWays(int[] nums, int target) {
    3. int totalCombinations = 1 << nums.length;
    4. int count = 0;
    5. for (int i = 0; i < totalCombinations; i++) {
    6. int sum = 0;
    7. for (int j = 0; j < nums.length; j++) {
    8. if ((i & (1 << j))!= 0) {
    9. sum += nums[j];
    10. } else {
    11. sum -= nums[j];
    12. }
    13. }
    14. if (sum == target) {
    15. count++;
    16. }
    17. }
    18. return count;
    19. }
    20. }

    解法三:动态规划

    思路

    首先计算数组元素的总和 sum 。如果目标值 target 的绝对值大于总和,或者 (target + sum) 不能被 2 整除,说明不存在满足条件的组合,直接返回 0 。然后将问题转化为找到一个子集,使得该子集元素之和为 (target + sum) / 2 ,通过计算这样的子集个数来得到满足目标和的表达式数量。

    解题方法

    使用动态规划,定义二维数组 dp[i][j] 表示处理到数组的第 i 个元素,和为 j 的不同表达式的数目。通过两层循环遍历数组和可能的和值,根据当前数字是否能加到当前和中更新 dp 值。

    时间复杂度

    O(n×m)

    空间复杂度

    O(n×m)

    Code

    1. public class Solution {
    2. public int findTargetSumWays(int[] nums, int target) {
    3. int totalCombinations = 1 << nums.length;
    4. int count = 0;
    5. for (int i = 0; i < totalCombinations; i++) {
    6. int sum = 0;
    7. for (int j = 0; j < nums.length; j++) {
    8. if ((i & (1 << j))!= 0) {
    9. sum += nums[j];
    10. } else {
    11. sum -= nums[j];
    12. }
    13. }
    14. if (sum == target) {
    15. count++;
    16. }
    17. }
    18. return count;
    19. }
    20. }

    一个人并不是生来要被打败的,你尽可以把他消灭掉,可就是打不败他。——《老人与海》

  • 相关阅读:
    【Transformers】第 10 章 :从零开始训练 Transformer
    不想购买Web服务器?那就用cpolar发布网页吧
    封装了一个仿照抖音效果的iOS评论弹窗
    Limitの线段树题单 题解目录
    多通道反向字典模型
    重磅!flink-table-store 将作为独立数据湖项目重新加入 Apache
    2008-2020年31省农业保险保费收入相关数据/农业保险收入数据/农业保险支出数据/保险总支出数据
    redis-集群-1-主从复制:单台机器模拟主从复制,一主二从
    npm i 报错或者卡顿 range manifest for 解决
    TortoiseSVN 详细操作指南
  • 原文地址:https://blog.csdn.net/Rangsh/article/details/140087858