LeetCode的medium题集合(C++实现)六

1 Multiply Strings

Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.

该题实际就是利用字符串来解决大数的乘法问题。为了计算方便先将两组数翻转,将字符串转化为整数利用两个循环逐位相乘,将结果保存。然后逐位解决进位问题。

string multiply(string num1, string num2) {
    int flag = 0, len1 = num1.length(), len2 = num2.length();
    reverse(num1.begin(), num1.end());
    reverse(num2.begin(), num2.end());
    vector<int> mid(len1 + len2, 0);
    string result;
    for (int i = 0; i<len1; i++)
    {
        for (int j = 0; j<len2; j++)
        {
            int a = num1[i] - ‘0‘;
            int b = num2[j] - ‘0‘;
            mid[i + j] += a*b;
        }
    }
    for (int m = 0; m<len1 + len2; m++)
    {
        int c = (mid[m] + flag) % 10;
        flag = (mid[m] + flag) / 10;
        mid[m] = c;
    }
    int n = len1 + len2 - 1;
    while (mid[n] == 0)
    {
        n--;   //去掉前面为0的元素
    }
    if (n<0) return result += ‘0‘;
    for (; n >= 0; n--)
    {
        result += (‘0‘ + mid[n]);
    }
    return result;
    }

2 Permutations

Given a collection of 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], and [3,2,1].

前面我们已经解决过nextPermutation问题,即可以求出一组数集的下一个排列,我们可以先将这组数按升序排列,然后循环求解下一个排列直到无解为止,此时可得到这组数全部的排列组合。该方法同时解决了Permutations II.

 bool nextPermutation(vector<int>& nums) {
        bool flag=true;
        if (nums.size() < 2) return false;
          int i, k;
          for (i = nums.size() - 2; i >= 0; --i) if (nums[i] < nums[i+1]) break;
          for (k = nums.size() - 1; k > i; --k) if (nums[i] < nums[k]) break;
          if(i<0) flag=false;
          if (i >= 0) swap(nums[i], nums[k]);
          reverse(nums.begin() + i + 1, nums.end());
          return flag;
    }
    vector<vector<int>> permute(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        vector<vector<int> > res;
        bool flag=true;
        while(flag)
        {
           res.push_back(nums);
           flag=nextPermutation(nums);
        }
        return res;
    }
时间: 2024-07-31 01:48:31

LeetCode的medium题集合(C++实现)六的相关文章

LeetCode的medium题集合(C++实现)十六

1 Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of

LeetCode的medium题集合(C++实现)十一

1 Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked

LeetCode的medium题集合(C++实现)五

1 Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. 可以采用递归的方法解决这个问题,当找到一组数之和

LeetCode的medium题集合(C++实现)四

1 Next Permutation Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending or

LeetCode的medium题集合(C++实现)十五

1 Subsets Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If nums = [1,2,3], a solution is: [ [3], [1],

LeetCode的medium题集合(C++实现)九

1 Jump Game Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index. 利用两指针法

LeetCode的medium题集合(C++实现)八

1 Pow(x, n) 该题采用二分法进行递归 double myPow(double x, int n) { if(n==0) return 1; if(n<0) { n=(-n); x=1/x; } double res=myPow(x,n/2); if(n%2==0) { return res*res; } else { return res*res*x; } } 2 Maximum Subarray Find the contiguous subarray within an array

LeetCode的medium题集合(C++实现)十

1 Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations.Given n and k, return the kth permutation sequence. 使用Next Permutation循环k次可以得到序列,但leetcode上提交会出现时间超过限制.下面采用数学法: 在n!个排列中,第一位元素相同的排列总是有(n?1)!个,如果p=k/(n?1)!,那么排列的第一位元素

LeetCode的medium题集合(C++实现)十七

1 Partition List Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the two partitions.For example: Give