C# 返回连续的0';在给定的二进制数中

C# 返回连续的0';在给定的二进制数中,c#,binary,C#,Binary,我正在练习C#。我想返回给定二进制数中连续0的数目。例如: int test1 = 101010; // Return 1 int test2 = 100101; // Return 2 int test3 = 100001; // Return 4 int test4 = 111111; // Return 0 我提出了以下代码,它适用于测试1、2和4,但不适用于测试3 public static int continuousZeros(int x) {

我正在练习C#。我想返回给定二进制数中连续0的数目。例如:

int test1 = 101010;  // Return 1
int test2 = 100101;  // Return 2
int test3 = 100001;  // Return 4
int test4 = 111111;  // Return 0
我提出了以下代码,它适用于测试1、2和4,但不适用于测试3

    public static int continuousZeros(int x)
    {

        char[] charArray = x.ToString().ToCharArray();

        int count = 0;
        int total = 0;

        for (int i = 0; i < charArray.Length; i++)
        {
            if (charArray[i] == '0' && charArray[i - 1] == '0')
            {
                count++; 
            }
            else if (charArray[i] == '0' && charArray[i - 1] == '1')
            {
                total = count + 1;
                count = 0;
            }
        }

        return total;
    }
公共静态整数连续零(整数x)
{
char[]charArray=x.ToString().ToCharArray();
整数计数=0;
int-total=0;
for(int i=0;i
如果我尝试添加另一个else If:
If(charArray[I]='1'&&charArray[I-1]='0')
我得到一个索引自动异常


我也确信有一种更有效的方法来实现这一点。

像这样的方法怎么样

char[] charArray = x.ToString().ToCharArray();
bool zeroControl = false;
List<int> counts = new List<int>();
int currentCount = 0;
for (int i = 0; i < charArray.Length; i++)
{
    if (charArray[i] == '0' && !zeroControl)
    {
         zeroControl = true;
         currentCount++;
    }
    else if (charArray[i] == '0')
    {
         currentCount++;
    }
    else
    {
         zeroControl = false;
         counts.Add(currentCount);
         currentCount = 0;
    }
}
counts.Add(currentCount);
var result = counts.Max();
char[]charArray=x.ToString().ToCharArray();
bool zeroControl=false;
列表计数=新列表();
int currentCount=0;
for(int i=0;i

它看起来不那么优雅,但适用于所有数字。

像这样的东西怎么样

char[] charArray = x.ToString().ToCharArray();
bool zeroControl = false;
List<int> counts = new List<int>();
int currentCount = 0;
for (int i = 0; i < charArray.Length; i++)
{
    if (charArray[i] == '0' && !zeroControl)
    {
         zeroControl = true;
         currentCount++;
    }
    else if (charArray[i] == '0')
    {
         currentCount++;
    }
    else
    {
         zeroControl = false;
         counts.Add(currentCount);
         currentCount = 0;
    }
}
counts.Add(currentCount);
var result = counts.Max();
char[]charArray=x.ToString().ToCharArray();
bool zeroControl=false;
列表计数=新列表();
int currentCount=0;
for(int i=0;i

它看起来不那么优雅,但它适用于所有数字。

正如其他人所提到的,您正在尝试访问charArray[-1]
,这会引发异常。这是我的第一直觉:

public static int continuousZeros(int x)
{
    int max = 0;
    int current = 0;

    char[] charArray = x.ToString().ToCharArray();

    for (int i = 0; i < charArray.Length; i++)
    {
        if (charArray[i] == '0')
        {
            current++;
        }
        else
        {
            if (current > max) { max = current; }
            current = 0;
        }
    }

    return max;
}
公共静态整数连续零(整数x)
{
int max=0;
int电流=0;
char[]charArray=x.ToString().ToCharArray();
for(int i=0;i最大){max=current;}
电流=0;
}
}
返回最大值;
}

正如其他人提到的,您正在尝试访问
charArray[-1]
,这会引发异常。这是我的第一直觉:

public static int continuousZeros(int x)
{
    int max = 0;
    int current = 0;

    char[] charArray = x.ToString().ToCharArray();

    for (int i = 0; i < charArray.Length; i++)
    {
        if (charArray[i] == '0')
        {
            current++;
        }
        else
        {
            if (current > max) { max = current; }
            current = 0;
        }
    }

    return max;
}
公共静态整数连续零(整数x)
{
int max=0;
int电流=0;
char[]charArray=x.ToString().ToCharArray();
for(int i=0;i最大){max=current;}
电流=0;
}
}
返回最大值;
}
我也确信有一个更有效的方法来实现这一点

您可以使用一个简单的LINQ语句:

var maxZeroesInString = myString.ToString().Split('1').Max(x => x.Length);
假设您的字符串只有
0
1
,您可以在
1
上拆分。现在,您有了一个仅由
0
组成的字符串数组,因此您可以找到最长的字符串

我也确信有一个更有效的方法来实现这一点

您可以使用一个简单的LINQ语句:

var maxZeroesInString = myString.ToString().Split('1').Max(x => x.Length);

假设您的字符串只有
0
1
,您可以在
1
上拆分。现在,您有了一个仅由
0
组成的字符串数组,因此您可以只查找最长的字符串。

您不需要查看位的组合,只需计算零位,只要找到一位,就可以将计数与目前为止的最高计数进行比较

如果你从最低到最高遍历位,最后总是有一个1位,这意味着在循环结束后不可能有一个需要检查的零位范围。当你遇到最后一点的时候,你已经注意到了

您不需要将数字转换为字符串,只需选择最低的位,直到剩余的数字为零

public static int continuousZeros(int x) {
  int count = 0;
  int highest = 0;
  while (x > 0) {
    if ((x & 1) == 0) {
      count++;
    } else {
      highest = Math.Max(count, highest);
      count = 0;
    }
    x >>= 1;
  }
  return highest;
}

注意:像
100001
这样的数字不是二进制数,而是看起来像二进制数的十进制数。此解决方案处理实际的二进制数,即二进制数
100001
的十进制表示形式为
33
您不需要查看位的组合,只需计算零位,只要找到一位,就可以将计数与迄今为止的最高计数进行比较

如果你从最低到最高遍历位,最后总是有一个1位,这意味着在循环结束后不可能有一个需要检查的零位范围。当你遇到最后一点的时候,你已经注意到了

您不需要将数字转换为字符串,只需选择最低的位,直到剩余的数字为零

public static int continuousZeros(int x) {
  int count = 0;
  int highest = 0;
  while (x > 0) {
    if ((x & 1) == 0) {
      count++;
    } else {
      highest = Math.Max(count, highest);
      count = 0;
    }
    x >>= 1;
  }
  return highest;
}

注意:像
100001
这样的数字不是二进制数,而是看起来像二进制数的十进制数。此解决方案处理实际的二进制数,即二进制数
100001
的十进制表示形式为
33

我对您的代码进行了一些修改,解决了问题,并给出了正确的结果

public static int continuousZeros(int x)
    {

        char[] charArray = x.ToString().ToCharArray();

        int count = 0;
        int total = 0;

        for (int i = 0; i < charArray.Length; i++)
        {
            if (charArray[i] == '0' )
            {
                count++;
            }
            else  
            {
                total = Math.Max(total,count);
                count = 0;
            }
        }

        return total;
    }
公共静态整数连续零(整数x)
{
char[]charArray=x.ToString().ToCharArray();
整数计数=