LeetCode 238
Product of Array Except Self
Given an array of n integers where n > 1, nums,
return an array output such that output[i] is equal to the product
of all the elements of nums except nums[i].
Solve it without division and in O(n).
For example, given [1,2,3,4], return [24,12,8,6].
Follow up:
Could you solve it with constant space complexity?
(Note: The output array does not count as extra space
for the purpose of space complexity analysis.)
/*************************************************************************
> File Name: LeetCode238.c
> Author: Juntaran
> Mail: Jacinthmail@gmail.com
> Created Time: 2016年04月28日 星期四 23时40分01秒
************************************************************************/ /************************************************************************* Product of Array Except Self Given an array of n integers where n > 1, nums,
return an array output such that output[i] is equal to the product
of all the elements of nums except nums[i]. Solve it without division and in O(n). For example, given [1,2,3,4], return [24,12,8,6]. Follow up:
Could you solve it with constant space complexity?
(Note: The output array does not count as extra space
for the purpose of space complexity analysis.) ************************************************************************/ #include<stdio.h> /**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* productExceptSelf(int* nums, int numsSize, int* returnSize)
{
if( numsSize == )
{
return ;
} int *result = malloc(numsSize*sizeof(int));
*returnSize = numsSize; /* 从左往右 */
int i;
int leftProduct = ;
int rightProduct = ; for( i=; i<numsSize; i++ ){
result[i] = leftProduct;
leftProduct *= nums[i];
}
/* 从右往左 */
for( i = numsSize - ; i>=; i-- ){
result[i] *= rightProduct;
rightProduct *= nums[i];
} return result;
} int main(){ int nums[] = {,,,,,,};
int numsSize = ;
int returnSize[numsSize];
productExceptSelf( nums, numsSize, returnSize);
return ;
}
LeetCode 238的更多相关文章
- 剑指offer 66. 构建乘积数组(Leetcode 238. Product of Array Except Self)
剑指offer 66. 构建乘积数组 题目: 给定一个数组A[0, 1, ..., n-1],请构建一个数组B[0, 1, ..., n-1],其中B中的元素B[i] = A[0] * A[1] * ...
- [LeetCode] 238. Product of Array Except Self 除本身之外的数组之积
Given an array nums of n integers where n > 1, return an array output such that output[i] is equ ...
- LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)
238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...
- LeetCode 238. Product of Array Except Self (去除自己的数组之积)
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- LN : leetcode 238 Product of Array Except Self
lc 238 Product of Array Except Self 238 Product of Array Except Self Given an array of n integers wh ...
- Java实现 LeetCode 238 除自身以外数组的乘积
238. 除自身以外数组的乘积 给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元 ...
- leetcode 238 Product of Array Except Self
这题看似简单,不过两个要求很有意思: 1.不准用除法:最开始我想到的做法是全部乘起来,一项项除,可是中间要是有个0,这做法死得很惨. 2.空间复杂度O(1):题目说明了返回的那个数组不算进复杂度分析里 ...
- (medium)LeetCode 238.Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- 【LeetCode 238】Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- Java [Leetcode 238]Product of Array Except Self
题目描述: Given an array of n integers where n > 1, nums, return an array output such that output[i] ...
随机推荐
- homework-02 "最大子数组之和"的问题进阶
代码编写 这次的作业瞬间难了好多,无论是问题本身的难度或者是单元测试这一原来没接触过的概念或者是命令行参数的处理这些琐碎的问题,都使得这次作业的完成说不上轻松. 最大子数组之和垂直水平相连的拓展问题解 ...
- WScript中调用js方法
http://zhidao.baidu.com/question/484374074.html ———————————————————————————————————————————————— Sub ...
- JSF 2 hidden value example
In JSF, you can use the <h:inputHidden /> tag to render a HTML hidden value field. For example ...
- POJ 1064 Cable master (二分答案)
题目链接:http://poj.org/problem?id=1064 有n条绳子,长度分别是Li.问你要是从中切出m条长度相同的绳子,问你这m条绳子每条最长是多少. 二分答案,尤其注意精度问题.我觉 ...
- HDU 4438 Hunters (数学,概率计算)
题意:猎人A和B要进行一场比赛.现在有两个猎物老虎和狼,打死老虎可以得X分,打死狼可以得Y分.现在有两种情况: (1)如果A与B的预定目标不同,那么他们都将猎到预定的目标. (2)如果A与B的预定目标 ...
- [置顶] 文件和目录(一)--unix环境高级编程
普通文件和目录linux中最多的两类文件,linux中一共有七种类型的文件,如下: 1.普通文件 2.目录 3.字符特殊设备 4.块特殊设备 5.FIFO,又叫命名管道 6.Socket,即套接字 7 ...
- How Much Work Does it Take to be a Successful Mathematician?
http://mathoverflow.net/questions/9799/how-much-work-does-it-take-to-be-a-successful-mathematician# ...
- JdbcTemplate增删改查
1.使用JdbcTemplate的execute()方法执行SQL语句 jdbcTemplate.execute("CREATE TABLE USER (user_id integer, n ...
- jquery 绑定省份和城市
前台代码: <asp:DropDownList runat="server" ID="ddlProvince"></asp:DropDownL ...
- Vue2.0表单校验组件vee-validate的使用
vee-validate使用教程 *本文适合有一定Vue2.0基础的同学参考,根据项目的实际情况来使用,关于Vue的使用不做多余解释.本人也是一边学习一边使用,如果错误之处敬请批评指出* 一.安装 n ...