我已经提出了下面的代码但是并不满足所有情况,例如:
包含全0的数组
具有负值的数组(它有点棘手,因为它是关于将产品看作是两个负的整数给出正值)
public static int LargestProduct(int[] arr)
{
//returning arr[0] if it has only one element
if (arr.Length == 1) return arr[0];
int product = 1;
int maxProduct = Int32.MinValue;
for (int i = 0; i < arr.Length; i++)
{
//this block store the largest product so far when it finds 0
if (arr[i] == 0)
{
if (maxProduct < product)
{
maxProduct = product;
}
product = 1;
}
else
{
product *= arr[i];
}
}
if (maxProduct …Run Code Online (Sandbox Code Playgroud)例如,字符串"ABCXYZ"中的所有'X'实例将变为"ABCXXYZ"
我想解决这个问题:
有没有一种有效的方法在c#中执行此操作,请建议一个
谢谢