小编bha*_*kti的帖子

在整数数组中查找具有最大乘积的连续序列

我已经提出了下面的代码但是并不满足所有情况,例如:

  1. 包含全0的数组

  2. 具有负值的数组(它有点棘手,因为它是关于将产品看作是两个负的整数给出正值)

    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)

.net c# arrays algorithm

5
推荐指数
1
解决办法
4028
查看次数

如何加倍给定字符串中的所有字符实例.使用c#

例如,字符串"ABCXYZ"中的所有'X'实例将变为"ABCXXYZ"

我想解决这个问题:

  1. 将字符串复制到char数组
  2. 转到整个数组,找出字符串中'X'的出现次数
  3. 用出现次数*2找出新的长度
  4. 迭代char数组并为每次出现'X'存储'X'两次
  5. 将char数组复制回字符串

有没有一种有效的方法在c#中执行此操作,请建议一个

谢谢

c# string

2
推荐指数
1
解决办法
205
查看次数

标签 统计

c# ×2

.net ×1

algorithm ×1

arrays ×1

string ×1