当前位置: 首页 > news >正文

LeetCode -- 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].


输入数组Nums,返回一个数组output,要求output[i]为,nums[0]...nums[i-1],nums[i+1]....nums[n]的乘积。


思路:
1.区分是否包含0的情况,存0的索引。
2.考虑越界的情况


实现代码:




public class Solution {
    public int[] ProductExceptSelf(int[] nums) {
    if(nums == null || nums.Length == 1){
		return nums;
	}
	
	var output = new int[nums.Length];
	
	var zeroIndexes = new List<int>();
	for(var i =0 ;i < nums.Length; i++){
	    if(nums[i] == 0){
	        zeroIndexes.Add(i);
	    }
	    output[i] = 0;
	}
	
	if(zeroIndexes.Count > 1){
	    return output;
	}
	else if(zeroIndexes.Count == 1){
	    var s = 1;
	    for(var i =0 ;i < nums.Length ;i++){
	        if(i == zeroIndexes[0]){
	            continue;   
	        }
	        s *= nums[i];
	    }   
	    output[zeroIndexes[0]] = s;
	    return output;
	}
	else{
	    output[0] = 1;
	    for(var i = 1;i < nums.Length; i++){
		output[0] *= nums[i];
	}
	
	for(var i = 1; i < nums.Length; i++){
	    try{
			output[i] = output[0] * nums [0] % nums[i] == 0 ? output[0] * nums[0] / nums[i] : nums[i] * output[0]/ nums[0];
		}
		catch(System.OverflowException ex)
		{
			output[i] = 0;
		}
	}
	
	}
	
	
	return output;
    }
}


相关文章:

  • 不知道为什么我的一oracle的sql调优文章笔记无法发表,提示“文章中出现禁止的词语,系统不予接受。”...
  • LeetCode -- Remove Duplicates From Sorted Array 2
  • 好人陈虻
  • LeetCode -- Reverse Bits
  • LeetCode -- Rotate Array
  • SQL2005CLR函数扩展-天气服务
  • LeetCode -- String to Integer (atoi)
  • JavaScript 读写文件
  • LeetCode -- Subsets
  • 也谈实体验证(Entity Validation)
  • LeetCode -- Symmetric Tree
  • 越狱 第五季 Microsoft复活
  • LeetCode -- Trap Water Rain
  • leetcode -- Unique Binary Search Trees II
  • WCF中神秘的“8731“端口和“Design_Time_Addresses”
  • 345-反转字符串中的元音字母
  • Bytom交易说明(账户管理模式)
  • CSS 提示工具(Tooltip)
  • Elasticsearch 参考指南(升级前重新索引)
  • If…else
  • JavaScript标准库系列——Math对象和Date对象(二)
  • Java小白进阶笔记(3)-初级面向对象
  • mysql常用命令汇总
  • mysql外键的使用
  • Phpstorm怎样批量删除空行?
  • React-Native - 收藏集 - 掘金
  • Vue.js-Day01
  • 百度地图API标注+时间轴组件
  • 大主子表关联的性能优化方法
  • 记录一下第一次使用npm
  • 模仿 Go Sort 排序接口实现的自定义排序
  • 前言-如何学习区块链
  • 深度学习中的信息论知识详解
  • 数据科学 第 3 章 11 字符串处理
  • 学习JavaScript数据结构与算法 — 树
  • CMake 入门1/5:基于阿里云 ECS搭建体验环境
  • 说说我为什么看好Spring Cloud Alibaba
  • #DBA杂记1
  • #LLM入门|Prompt#3.3_存储_Memory
  • #我与Java虚拟机的故事#连载10: 如何在阿里、腾讯、百度、及字节跳动等公司面试中脱颖而出...
  • (zhuan) 一些RL的文献(及笔记)
  • (附源码)springboot金融新闻信息服务系统 毕业设计651450
  • (四)linux文件内容查看
  • (算法)Game
  • .net 程序发生了一个不可捕获的异常
  • .NET/C# 异常处理:写一个空的 try 块代码,而把重要代码写到 finally 中(Constrained Execution Regions)
  • .net反混淆脱壳工具de4dot的使用
  • .Net环境下的缓存技术介绍
  • .NET基础篇——反射的奥妙
  • .Net中的集合
  • @modelattribute注解用postman测试怎么传参_接口测试之问题挖掘
  • @serverendpoint注解_SpringBoot 使用WebSocket打造在线聊天室(基于注解)
  • [ JavaScript ] JSON方法
  • [ 云计算 | AWS 实践 ] 基于 Amazon S3 协议搭建个人云存储服务
  • [120_移动开发Android]008_android开发之Pull操作xml文件