Leetcode日记(11)

本文解析了四个经典算法问题:组合总和 II、字符串相乘、排列与排列 II 的解决方案,详细介绍了如何使用回溯法和其他高效算法来解决这些挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Combination Sum II

问题描述

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
    Note:
    • All numbers (including target) will be positive integers.
    • The solution set must not contain duplicate combinations.
    For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
    A solution set is:

[
  [1, 7],
  [1, 2, 5],
  [2, 6],
  [1, 1, 6]
]

分析

    采用回溯算法,注意每个值只能使用一次,并且所有记过都是唯一的。

解答

class Solution {
public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        sort(candidates.begin(), candidates.end());
        vector<vector<int>> result;
        vector<int> combination;
        combinationsum(result, combination, candidates, target, 0);
        return result;
    }

    void combinationsum(vector<vector<int>> &res, vector<int> &com, vector<int> &arr, int tar, int begin)
    {
        if (!tar)
        {
            res.push_back(com);
            return ;
        }
        for (int i = begin; i < arr.size() && tar >= arr[i]; i++)
        {
            com.push_back(arr[i]);
            combinationsum(res, com, arr, tar - arr[i], i+1);
            com.pop_back();
            while (i + 1 < arr.size() && arr[i] == arr[i + 1])
                i++;
        }
    }
};

Multiply Strings

问题描述

     Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.
    Note:

  1. The length of both num1 and num2 is < 110.
  2. Both num1 and num2 contains only digits 0-9.
  3. Both num1 and num2 does not contain any leading zero.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

分析

    由于字符串的长度未知,且可能超出长整型的长度,所以我们运用手动乘法的方式来解决。

解答

class Solution {
public:
    string multiply(string num1, string num2) {
        string sum(num1.size() + num2.size(), '0');
        for (int i = num1.size() - 1; 0 <= i; i--) 
        {
            int carry = 0;
            for (int j = num2.size() - 1; 0 <= j; j--) 
            {
                int tmp = (sum[i + j + 1] - '0') + (num1[i] - '0') * (num2[j] - '0') + carry;
                sum[i + j + 1] = tmp % 10 + '0';
                carry = tmp / 10;
            }
            sum[i] += carry;
        }
        size_t startpos = sum.find_first_not_of("0");
        if (string::npos != startpos) 
        {
            return sum.substr(startpos);
        }
        return "0";
    }
};

Permutations

问题描述

     Given a collection of distinct numbers, return all possible permutations.
    For example,
    [1,2,3] have the following permutations:

[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

分析

    使用回溯算法设计,比较经典的题目。

解答

class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> result;
        getres(result, nums, 0);
        return result;
    }
    void getres(vector<vector<int>> &res, vector<int> &nums, int start)
    {
        if (start >= nums.size())
        {
            res.push_back(nums);
            return ;
        }
        for (int i = start; i < nums.size(); i++)
        {
            swap(nums[start], nums[i]);
            getres(res, nums, start + 1);
            swap(nums[start], nums[i]);
        }
    }
};

Permutations II

问题描述

     Given a collection of numbers that might contain duplicates, return all possible unique permutations.
    For example,
    [1,1,2] have the following unique permutations:

[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]

分析

    和上一问题相同,需要注意数字出现重复的情况。

解答

class Solution {
public:
    vector<vector<int> > permuteUnique(vector<int> &num) {
        sort(num.begin(), num.end());
        vector<vector<int> >res;
        getres(res, num, 0, num.size());
        return res;
    }
    void getres(vector<vector<int>> &res, vector<int> num, int start, int end) 
    {
        if (start == end - 1) {
            res.push_back(num);
            return;
        }
        for (int k = start; k < end; k++) 
        {
            if (start != k && num[start] == num[k]) 
            continue;
            swap(num[start], num[k]);
            getres(res, num, start + 1, end);
        }
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值