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-06
现代程序设计homework-06 1) 把程序编译通过, 跑起来. 加入了倒退的功能,程序已经能跑起来了(见代码). 不过倒退功能有些bug,不过这是由于原本程序的主逻辑就有点问题(对于不可走的格子 ...
- java多线程中的volatile和synchronized
package com.chzhao; public class Volatiletest extends Thread { private static int count = 0; public ...
- SMTP邮件服务器配置
QQ个人邮箱使用: smtp.qq.com端口为25 密码是个人邮箱密码 QQ企业邮箱使用: smtp.exmail.qq.com端口为25 密码是邮箱密码 163邮箱使用 smtp.163.com端 ...
- c++结束进程的程序
//#include <winbase.h> #include <windows.h> #include <process.h> #include <Tlhe ...
- AfxGetMainWnd()函数用法
CWnd* AfxGetMainWnd( ); 使用AfxGetMainWnd函数获取MFC程序中的主框架类指针是一个常用作法. 就是获得应用程序主窗口的指针,AfxGetMainWnd()-> ...
- python crawler0723.py
#!/usr/env python #-*- coding: utf-8 -*- import urllib import urllib2 import random import request ...
- MVC神韵---你想在哪解脱!(十四)
修正票价字段的精度 前面我们追加数据的时候遗留下来一个问题,就是在追加数据的时候,票价(Price)字段中输入的是9.99元,但是电影清单显示画面中该数据的票价字段显示为10元,这是为什么?这个问题发 ...
- 8天玩转并行开发——第一天 Parallel的使用
转自:http://www.cnblogs.com/huangxincheng/archive/2012/04/02/2429543.html 随着多核时代的到来,并行开发越来越展示出它的强大威力,像 ...
- cloudstack 修改显示名称
http://192.168.153.245:8900/client/api?command=updateVirtualMachineid=922d15e1-9be0-44ac-9494-ce5afc ...
- Linux学习笔记----(2)
闲着无事,就敲起了Linux 命令,熟悉一下.记得昨天在书上看到了 find命令的用法,觉得挺神奇的. 其中 find 能够确定文件的查找深度 于是 敲了如下命令: #mkdir father #to ...