【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 ...
随机推荐
- springbank 开发日志 springbank是如何执行一个handler的requestMapping对应的方法的
占位 从dispatcher说起,方法doDispatch(Map request)的参数request是一个通过解析来报报文新城的map //获取HandlerExecutionChain,其中封装 ...
- MySQL索引失效的几种情况
1.索引不存储null值 更准确的说,单列索引不存储null值,复合索引不存储全为null的值.索引不能存储Null,所以对这列采用is null条件时,因为索引上根本 没Null值,不能利用到索引, ...
- 「BZOJ2882」工艺
题解: 就是个最小表示法 大概做法就是扩大2倍原串 然后双指针比较,如果不相同了直接跳 原理随便画画就知道了
- P2484 [SDOI2011]打地鼠
差分 代码: #include <bits/stdc++.h> using namespace std; #define INF 1999999999 ][],b[][],c[][]; i ...
- 039 hive中关于数据库与表等的基本操作
一:基本用法 1.新建数据库 2.删除数据库 3.删除非空的数据库 4.指定数据库的位置 LOCATION:指定数据库的位置,不会在系统的默认文件下. 5.在指定数据库中新建表(验证在指定的数据库中可 ...
- Scrapy项目结构分析和工作流程
新建的空Scrapy项目: spiders目录: 负责存放继承自scrapy的爬虫类.里面主要是用于分析response并提取返回的item或者是下一个URL信息,每个Spider负责处理特定的网站或 ...
- (转)java代码发送JSON格式的httpPOST请求
import Java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import j ...
- 学机器学习,不会数据处理怎么行?—— 二、Pandas详解
在上篇文章学机器学习,不会数据处理怎么行?—— 一.NumPy详解中,介绍了NumPy的一些基本内容,以及使用方法,在这篇文章中,将接着介绍另一模块——Pandas.(本文所用代码在这里) Panda ...
- JavaScript学习总结(六)——前端模块化开发
早期的javascript版本没有块级作用域.没有类.没有包.也没有模块,这样会带来一些问题,如复用.依赖.冲突.代码组织混乱等,随着前端的膨胀,模块化显得非常迫切. 前端模块化规范如下: 一.前端模 ...
- 团队作业第六次——团队Github实战训练
作业格式 课程名称:软件工程1916|W(福州大学) 作业要求:团队作业第六次-团队Github实战训练 团队名称:葫芦娃队 作业目标:确定和分析选题,绘制评审表 github地址:https://g ...