leetcode腾讯精选练习之除自身以外数组的乘积(十)

最长公共前缀

题目

给定长度为?n?的整数数组?nums,其中?n > 1,返回输出数组?output?,其中 output[i]?等于?nums?中除?nums[i]?之外其余各元素的乘积。
示例:
输入: [1,2,3,4]
输出: [24,12,8,6]
说明: 请不要使用除法,且在?O(n) 时间复杂度内完成此题。

思路

第一种思路O(n2):但是不符合题目要求
两层循环遍历,思路很简单不详细说了第一层遍历数组中中的每个数,第二层遍历求解除了本身之外的乘积。
第二种思路就是除法:也不符合题目要求
1.所有的数字相乘除去本身即可得到结果。
2.需要注意0的问题,如果有一个0,那么除了0那个为止之外的所有结果都是0,0位置处的结果是其他数字的乘积。如果有两个零,那么结果的所有值都是0。

第三种思路:
每个结果都是这个数左边的数字的乘积和右边的数字的乘积,然后在相乘的出来的。
所以先求出每个数字左遍的乘积,先后再求出右边的乘积,进行相乘即可得到结果。
举个例子:先列出数组[1,2,3,4]左边的数字的乘积如下所示:

1 2 3 4
1 1 2 6

然后在列出所有数字右遍的数的乘积如下所示:

1 2 3 4
24 12 4 1

然后将左边数字和右边数字的乘积列在一起,最后一行显示结果如果下所示:

1 2 3 4
1 1 2 6
24 12 4 1
24 12 8 6

将左边数字的乘积和右边数字的乘积相乘即可得到最终的结果。

代码

第一种思路:

vector<int> productExceptSelf(vector<int>& nums) {
    vector<int> res;
    int sz = nums.size();
    for (size_t i = 0; i < sz; i++)
    {
        int mul = 1;
        for (size_t j = 0; j < sz; j++)
        {
            if (i == j)
            {
                continue;
            }
            mul *= nums[j];
        }
        res.push_back(mul);
    }
}

第二种思路:

vector<int> productExceptSelf(vector<int>& nums) {
    vector<int> res;
    int sz = nums.size();
    int mul = 1;
    int zeroIndex = -1;
    int zeroNum = 0;
    for (size_t i = 0; i < sz; i++)
    {
        if (nums[i] != 0)
        {
            mul *= nums[i];
        }
        else
        {
            zeroNum++;
            if (zeroNum > 1)
            {
                res.resize(sz, 0);
                return res;
            }
            zeroIndex = i;
        }
    }
    if (zeroNum != 0)
    {
        res.resize(sz, 0);
        res[zeroIndex] = mul;
    }
    else
    {
        for (size_t i = 0; i < sz; i++)
        {
            res.push_back(mul / nums[i]);
        }
    }
    return res;
}

第三种思路:

vector<int> productExceptSelf(vector<int>& nums) {
    int sz = nums.size();
    vector<int> res(sz, 0);

    int tmp = 1;
    for (int i = 0; i < sz; i++) {
        res[i] = tmp;
        tmp *= nums[i];
    }

    tmp = 1;
    for (int i = sz - 1; i >= 0; i--) {
        res[i] *= tmp;
        tmp *= nums[i];
    }
    return res;
}

总结

这个题目总容易想到的就是前两种方法,第三种题目要求的方法还是需要多想想

原文地址:https://www.cnblogs.com/zh20130424/p/12289205.html

时间: 2024-08-30 11:30:14

leetcode腾讯精选练习之除自身以外数组的乘积(十)的相关文章

LeetCode 腾讯精选50题-- 买卖股票的最佳时机 II

贪心算法: 具体的解题思路如下: II 的解题思路可以分为两部分, 1. 找到数组中差值较大的两个元素,计算差值. 2. 再步骤一最大的元素的之后,继续遍历,寻找差值最大的两个元素 可以得出的是,遍历指针是一直向着数组的尾部移动的,由于要取得局部的最大差值,这一局部子数组中的元素一定是递增的,所以先找到递增结束的位置,那就是递增位置的起点,然后遍历至此轮递增结束时的位置,这样就得到了一个局部的最优解. 此思路由于指针只遍历一次,所以时间复杂度为O(N). 以下是代码: class Solutio

LeetCode 腾讯精选50题--子集

根据题意,找到几何中的所有子集,说实话子集是没有什么头绪的,因为如果采用遍历的方法,稍有遗漏不说,代码的嵌套循环层数随着数组大小的增加而增加,想了很久没有头绪后就去看了看评论,然后就被点破了解题的关键:遍历数组时,让当前这一元素构成的集合,分别去加上之前找到的所有子集,由于第一次找到的子集一定有两个,一个为空,一个带有一个元素,那么用当前元素组成的集合分别加上之前的集合,一定会有一个集合只有当前元素 如图: 由于采用遍历的方式,对于数组长度为N,指针没往前走一次,要遍历的子集数量是原来的2倍,所

leetcode腾讯精选练习(50 题)(持续更新)

1.除自身以外数组的乘积 给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: 输入: [1,2,3,4]输出: [24,12,8,6]说明: 请不要使用除法,且在 O(n) 时间复杂度内完成此题. 题解:记录前缀积和后缀积. 参考代码: class Solution { public: vector<int> productExceptSelf(vector&

leetcode腾讯精选练习之最长公共前缀(九)

最长公共前缀 题目 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串?"". 示例?1: 输入: ["flower","flow","flight"] 输出: "fl" 示例?2: 输入: ["dog","racecar","car"] 输出: "" 解释: 输入不存在公共前缀. 说明: 所有输入

腾讯精选面试重点问题:Android源码分析--应用程序启动

面试官:什么是应用程序启动 应用程序的启动,又可称为根Activity的启动.但是在讲应用程序启动之前,我们有必要对应用程序进程(AppProcess)启动有所了解,那是因为启动一个应用程序首先要保证该应用程序的进程已经被启动.AMS在启动应用程序时,会先检查应用程序进程是否存在,如果不存在就需要请求Zygote进程创建并启动应用程序进程.这里我不会贴上大段大段的代码,只是一些总结,并提供了相关源码的链接. 应用程序进程(AppProcess)启动 启动大纲 AMS发送启动应用程序进程请求. Z

[LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组

Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. You need to find the shortest such subarray and output its length. E

[LeetCode] Find All Duplicates in an Array 找出数组中所有重复项

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

LeetCode 548. Split Array with Equal Sum (分割数组使得子数组的和都相同)$

Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies following conditions: 0 < i, i + 1 < j, j + 1 < k < n - 1 Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) should be

[LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. Example 1: Given nums = [1, -1, 5, -2, 3], k = 3, return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is th