【LeetCode】238. Product of Array Except Self
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.)
就是用减法实现除法。
注意零的处理。
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int size = nums.size();
vector<int> ret(size, );
long long product = ;
int countZero = ;
int ind = -; // 0-index
for(int i = ; i < size; i ++)
{
if(nums[i] == )
{
countZero ++;
ind = i;
}
}
//special case for 0
if(countZero == )
{//no zero
for(int i = ; i < size; i ++)
product *= nums[i];
for(int i = ; i < size; i ++)
ret[i] = mydivide(product, nums[i]);
}
else if(countZero == )
{//1 zero
for(int i = ; i < size; i ++)
{
if(i != ind)
product *= nums[i];
}
ret[ind] = product; //others are 0s
}
else
{//2 or more zeros
; //all 0s
}
return ret;
}
int mydivide(long long product, int divisor)
{// guaranteed that divisor is not 0
int sign = ;
if((product < ) ^ (divisor < ))
sign = -;
if(product < )
product = -product;
if(divisor < )
divisor = -divisor;
//to here, product and divisor are positive
int ret = ;
while(true)
{
int part = ; //part quotient
int num = divisor;
while(product > num)
{
num <<= ;
part <<= ;
}
if(product == num)
{
ret += part;
return sign * ret;
}
else
{
num >>= ;
part >>= ;
ret += part;
product -= num;
}
}
}
};

【LeetCode】238. Product of Array Except Self的更多相关文章
- 【LeetCode】238. Product of Array Except Self 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 两次遍历 日期 题目地址:https://leetcode.c ...
- 【刷题-LeetCode】238. Product of Array Except Self
Product of Array Except Self Given an array nums of n integers where n > 1, return an array outpu ...
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
- 【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)
题目描述:两句话发人深思啊.... Given an array of 2n integers, your task is to group these integers into n pairs o ...
- leetcode:238. Product of Array Except Self(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...
- 【Leetcode】Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- 【LeetCode】912. Sort an Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...
- 【LeetCode】941. Valid Mountain Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- Android 使用 SVG 矢量图
android svg矢量图 可能包含的操作有: SVG图还包括改变颜色,透明度,大小,矩阵操作(平移.旋转.缩放),selector, (图标,背景,按钮),动画,等 setTint(int Col ...
- join 关键字
参考:http://www.blogjava.net/vincent/archive/2008/08/23/223912.html
- BZOJ5045 打砖块 2017年9月月赛 其他
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ5045 题意概括 有一堵墙. 现在挖掉某些砖.如果有相邻的某两个砖没有了,那么他们中上方的那块也没了 ...
- python实现用户登陆(sqlite数据库存储用户信息)
python实现用户登陆(sqlite数据库存储用户信息) 目录 创建数据库 数据库管理 简单登陆 有些地方还未完善. 创建数据库 import sqlite3 #建一个数据库 def create_ ...
- 【java并发核心一】Semaphore 的使用思路
最近在看一本书<Java并发编程 核心方法与框架>,打算一边学习一边把学习的经验记下来,所粘贴的代码都是我运行过的,大家一起学习,欢迎吐槽. 估计也没多少人看我的博客,哈哈,那么我还是会记 ...
- sql注入总结(一)--2018自我整理
SQL注入总结 前言: 本文和之后的总结都是进行总结,详细实现过程细节可能不会写出来~ 所有sql语句均是mysql数据库的,其他数据库可能有些函数不同,但是方法大致相同 0x00 SQL注入原理: ...
- 启动Azure模拟器出错解决方案
错误弹窗: 输出控制台: Microsoft Azure Tools: Warning: Overriding public port 80 to 2888 in role 'WebRole1'. M ...
- C/C++指针参数赋值问题
今天遇到一个问题,即在C/C++中,关于在函数里对指针赋值的问题.首先可以看到如下现象: void test(int *p) { p = NULL; } int main(int argc, char ...
- BZOJ.4543.[POI2014]Hotel加强版(长链剖分 树形DP)
题目链接 弱化版:https://www.cnblogs.com/SovietPower/p/8663817.html. 令\(f[x][i]\)表示\(x\)的子树中深度为\(i\)的点的个数,\( ...
- COGS.1272.[AHOI2009]行星序列(线段树 区间加、乘、求和)
题目链接 //注意取模! #include<cstdio> #include<cctype> using namespace std; const int N=1e5+5; i ...