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] ...
随机推荐
- 【转】Maven实战(七)---传递依赖
原博文出自于:http://blog.csdn.net/liutengteng130/article/details/47000069 感谢! 假设A-->C B-->A ...
- Java设计模式系列之状态模式
状态模式(State)的定义 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新.允许一个对象在其内部状态改变时改变它的行为.对象看起来似乎修改了它 ...
- Gym 100507L Donald is a postman (水题)
Donald is a postman 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/L Description Donald ...
- (转)UML实践----用例图、类图、对象图、顺序图、协作图、状态图、活动图、组件图、配置图
面向对象的问题的处理的关键是建模问题.建模可以把在复杂世界的许多重要的细节给抽象出.许多建模工具封装了UML(也就是Unified Modeling Language™),这篇课程的目的是展示出UML ...
- [iOS 多线程 & 网络 - 1.2] - 多线程GCD
A.GCD基本使用 1.GCD的概念 什么是GCD全称是Grand Central Dispatch,可译为"牛逼的中枢调度器"纯C语言,提供了非常多强大的函数GCD的优势GCD是 ...
- HDU 2647 Reward (拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...
- github 坑爹的仓库初始化设置
一段时间没有使用 github,奇妙地发现自己连仓库都不会建了,汗一个... 话说上次我在 github 上面建了一个仓库,在创建仓库的设置表单中勾上了自动生成 README.md 选项, ok,创建 ...
- mono-project
http://download.mono-project.com/sources/libgdiplus/libgdiplus-4.2.tar.gz http://download.mono-proje ...
- phpStudy 2014的Apache虚拟主机配置
安装phpStudy直接百度下载,傻瓜式安装很简单,一直点击下一步即可,中途根据个人爱好设置WWW目录,我的设置在D盘根目录里. 打开虚拟主机配置,打开D:\phpStudy\Apache\conf下 ...
- C#.net 之货币转换
利用string.format 和cultureInfo 来进行转换 /// <summary> /// 输入Float格式数字,将其转换为货币表达方式 /// </summary& ...